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

/**
 * 喝水提醒
 */
@Route(path = ARouterUrl.WATER_REMIND)
class WaterRemindAct : BaseVmAct<WearManagerVm>() {
    val binding by lazy { ActWaterRemindBinding.inflate(layoutInflater) }

    var water: WearManagerBean.Water? = null

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

    override fun initView() {
        super.initView()

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

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

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