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.util.CommonToastUtils
import com.zj365.mime.R
import com.zj365.mime.databinding.ActTempRemindBinding
import com.zj365.mime.vm.WearManagerVm
import com.xty.network.model.WearManagerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 体温预警提醒
 */
@Route(path = ARouterUrl.TEMP_REMIND)
class TempRemindAct : BaseVmAct<WearManagerVm>() {

    val binding by lazy { ActTempRemindBinding.inflate(layoutInflater) }

    private var tempBean: WearManagerBean.TempData? = null

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

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.mSwitchBtn.isChecked = tempBean?.state == 0
        binding.title.mIvBack.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("temp", tempBean)
            intent.putExtras(bundle)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.wear_manager_11)

        binding.mSwitchBtn.apply {//勿扰模式开关
            setOnCheckedChangeListener { _, isChecked ->
                mViewModel.lite4GSetting(4, if (isChecked) 0 else 1)
                clickFast()
            }
        }

    }

    override fun onBackPressed() {
        bundle.clear()
        bundle.putSerializable("temp", tempBean)
        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){
            tempBean?.state = if ( binding.mSwitchBtn.isChecked) 0 else 1
            CommonToastUtils.showSucceedToast("设置成功")
        }
    }
}