package com.zj365.mime.act

import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.picker.CustomPickerSelect
import com.xty.common.util.CommonToastUtils
import com.zj365.mime.R
import com.zj365.mime.databinding.ActSportRemindBinding
import com.zj365.mime.vm.WearManagerVm
import com.xty.network.model.WearManagerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 运动目标提醒
 */
@Route(path = ARouterUrl.SPORT_REMIND)
class SportRemindAct : BaseVmAct<WearManagerVm>() {
    val binding by lazy { ActSportRemindBinding.inflate(layoutInflater) }

    var sport: WearManagerBean.Sport? = null

    val pickerSelect by lazy {
        CustomPickerSelect(this) {
        }
    }

    val strArray by lazy {
        mutableListOf(
            CustomPickerSelect.PickerViewData("3000步"),
            CustomPickerSelect.PickerViewData("4000步"),
            CustomPickerSelect.PickerViewData("5000步"),
            CustomPickerSelect.PickerViewData("6000步"),
            CustomPickerSelect.PickerViewData("7000步"),
            CustomPickerSelect.PickerViewData("8000步"),
            CustomPickerSelect.PickerViewData("9000步"),
            CustomPickerSelect.PickerViewData("10000步"),
            CustomPickerSelect.PickerViewData("11000步"),
            CustomPickerSelect.PickerViewData("12000步"),
            CustomPickerSelect.PickerViewData("13000步"),
            CustomPickerSelect.PickerViewData("14000步"),
            CustomPickerSelect.PickerViewData("15000步")
        )
    }

    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        isDarkBar = true
        val serializable = intent.extras!!.getSerializable("sport")
        sport = if (null == serializable) null else serializable as WearManagerBean.Sport
    }

    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.mSwitchBtn.isChecked = sport?.state == 0
        binding.mTvNum.text = "${sport?.step}步"
        binding.title.mIvBack.setOnClickListener {
            submit(false)
            bundle.clear()
            bundle.putSerializable("sport", sport)
            intent.putExtras(bundle)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.wear_manager_06)

        binding.mSwitchBtn.apply {//勿扰模式开关
            setOnCheckedChangeListener { _, _ ->
                submit()
                clickFast()
            }
        }

        binding.mTvNum.setOnClickListener {
            pickerSelect.showView(binding.mTvNum, strArray)
        }
    }

    private fun submit(showloading: Boolean = true){
        val step = binding.mTvNum.text.toString()
        sport?.state = if ( binding.mSwitchBtn.isChecked) 0 else 1
        sport?.step = step.substring(0, step.length - 1).toInt()
        mViewModel.sportSetting(showloading, if (binding.mSwitchBtn.isChecked) 0 else 1, step.substring(0, step.length - 1).toInt())
    }

    override fun onBackPressed() {
        submit(false)
        bundle.clear()
        bundle.putSerializable("sport", sport)
        intent.putExtras(bundle)
        setResult(RESULT_OK, intent)
        finish()
    }

    /**
     * 一秒之内不能点击
     */
    fun clickFast() {
        binding.mSwitchBtn.isEnabled = false
        mainScope.launch {
            delay(1000)
            binding.mSwitchBtn.isEnabled = true
        }
    }

    override fun liveObserver() {
        mViewModel.settingLive.observe(this) {
            CommonToastUtils.showSucceedToast("设置成功")
        }
    }
}