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.ActOoRemindBinding
import com.zj365.mime.vm.WearManagerVm
import com.xty.network.model.WearManagerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 血氧预警提醒
 */
@Route(path = ARouterUrl.OO_REMIND)
class OoRemindAct : BaseVmAct<WearManagerVm>() {
    val binding by lazy { ActOoRemindBinding.inflate(layoutInflater) }

    var ooValue: WearManagerBean.OoValue? = null

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

    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.mSwitchBtn.isChecked = ooValue?.state == 0
        binding.title.mIvBack.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("ooValue", ooValue)
            intent.putExtras(bundle)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.wear_manager_10)

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

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