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.ActDonotRemindBinding
import com.zj365.mime.vm.WearManagerVm
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 勿扰提醒
 */
@Route(path = ARouterUrl.DONOT_REMIND)
class DonotRemindAct : BaseVmAct<WearManagerVm>() {
    val binding by lazy { ActDonotRemindBinding.inflate(layoutInflater) }

    var disturb: Int = 1
    var noonDisturb: Int = 1

    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        isDarkBar = true
        disturb = intent.extras!!.getInt("disturb", 1)
        noonDisturb = intent.extras!!.getInt("noonDisturb", 1)
    }

    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.mDaySwitchBtn.isChecked = disturb == 0
        binding.mNoonSwitchBtn.isChecked = noonDisturb == 0
        binding.title.mIvBack.setOnClickListener {
            bundle.clear()
            bundle.putInt("disturb", disturb)
            bundle.putInt("noonDisturb", noonDisturb)
            intent.putExtras(bundle)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.wear_manager_01)

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

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

    }
    override fun onBackPressed() {
        bundle.clear()
        bundle.putInt("disturb", disturb)
        bundle.putInt("noonDisturb", noonDisturb)
        intent.putExtras(bundle)
        setResult(RESULT_OK, intent)
        finish()
    }

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

    override fun liveObserver() {
        mViewModel.settingLive.observe(this){
            disturb =  if ( binding.mDaySwitchBtn.isChecked) 0 else 1
            noonDisturb =if ( binding.mNoonSwitchBtn.isChecked) 0 else 1
            CommonToastUtils.showSucceedToast("设置成功")
        }
    }
}