package com.zj365.mime.act

import android.content.Intent
import android.view.View
import androidx.annotation.IdRes
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.base.dialog.CustomDialog
import com.xty.base.dialog.SmsSignDialog
import com.xty.common.LogUtils
import com.xty.common.UmengEventId
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.onEventObject
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.zj365.mime.R
import com.zj365.mime.databinding.ActWearManagerBinding
import com.zj365.mime.vm.WearManagerVm
import com.zj365.mime.weight.SignDialog1
import com.xty.network.model.WearManagerBean

/**
 * 4G佩戴管理
 */
@Route(path = ARouterUrl.WEAR_MANAGER)
class WearManagerAct : BaseVmAct<WearManagerVm>() {

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

    private val smsDialog by lazy {
        CustomDialog(this, getString(R.string.dialog_title), getString(R.string.dialog_sms_content), {
            dialog.show()
        }){
            binding.mSmsSwitchBtn.isChecked = !binding.mSmsSwitchBtn.isChecked
        }
    }

    //签名
    val dialog by lazy {
        SmsSignDialog(this, {
            signDialog.show()
        }){
            binding.mSmsSwitchBtn.isChecked = !binding.mSmsSwitchBtn.isChecked
        }
    }

    private val signDialog by lazy {
        SignDialog1(this, {
            LogUtils.d(it)
            smsSetting(it, if (binding.mSmsSwitchBtn.isChecked) 1 else 0)
        }){
            binding.mSmsSwitchBtn.isChecked = !binding.mSmsSwitchBtn.isChecked
        }
    }


    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        isDarkBar = true
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = getString(R.string.wear_manager)

        binding.mLayoutDonotRemind.setOnClickListener(onClickListener)
        binding.mScreen5s.setOnClickListener(onClickListener)
        binding.mScreen10s.setOnClickListener(onClickListener)
        binding.mScreen15s.setOnClickListener(onClickListener)
        binding.mScreen30s.setOnClickListener(onClickListener)
        binding.mPowerSave.setOnClickListener(onClickListener)
        binding.mPowerEven.setOnClickListener(onClickListener)
        binding.mPowerHighlight.setOnClickListener(onClickListener)
        binding.mLayoutWater.setOnClickListener(onClickListener)
        binding.mLayoutSit.setOnClickListener(onClickListener)
        binding.mLayoutMedicine.setOnClickListener(onClickListener)
        binding.mLayoutHeart.setOnClickListener(onClickListener)
        binding.mLayoutOo.setOnClickListener(onClickListener)
        binding.mLayoutSport.setOnClickListener(onClickListener)
        binding.mLayoutTemp.setOnClickListener(onClickListener)
        binding.mTvDevice.setOnClickListener(onClickListener)

        mViewModel.getWearManagerList()
        mViewModel.getSmsList()
        onEventObject(baseContext, UmengEventId.WearManager, "佩戴管理列表页面")
    }


    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.mScreen5s, R.id.mScreen10s, R.id.mScreen15s, R.id.mScreen30s -> {
                onEventObject(baseContext, UmengEventId.WearManager, "熄屏时间设置")
                setScreenState(it.id, true)
            }
            R.id.mPowerSave, R.id.mPowerEven, R.id.mPowerHighlight -> {
                onEventObject(baseContext, UmengEventId.WearManager, "亮度调节")
                setPowerState(it.id, true)
            }
            R.id.mLayoutDonotRemind -> {//勿扰提醒
                mViewModel.wearManagerLive?.value?.data?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "勿扰设置")
                    bundle.clear()
                    bundle.putInt("disturb", this.disturb)
                    bundle.putInt("noonDisturb", this.noonDisturb)
                    RouteManager.goActRequest(
                        ARouterUrl.DONOT_REMIND,
                        this@WearManagerAct, 3000, bundle
                    )
                }
            }
            R.id.mLayoutWater -> {//喝水提醒
                mViewModel.wearManagerLive?.value?.data?.water?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "喝水提醒设置")
                    bundle.clear()
                    bundle.putSerializable("water", this)
                    RouteManager.goActRequest(
                        ARouterUrl.WATER_REMIND,
                        this@WearManagerAct, 3001, bundle
                    )
                }
            }
            R.id.mLayoutSit -> {//久坐提醒
                mViewModel.wearManagerLive?.value?.data?.sit?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "久坐提醒设置")
                    bundle.clear()
                    bundle.putSerializable("sit", this)
                    RouteManager.goActRequest(
                        ARouterUrl.SIT_REMIND,
                        this@WearManagerAct, 3002, bundle
                    )
                }
            }
            R.id.mLayoutMedicine -> {//用药提醒
                mViewModel.wearManagerLive?.value?.data?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "用药提醒设置")
                    bundle.clear()
                    bundle.putSerializable("value", this)
                    RouteManager.goActRequest(
                        ARouterUrl.MEDICATION_REMIND,
                        this@WearManagerAct, 3003, bundle
                    )
                }
            }
            R.id.mLayoutHeart -> {//心率提醒
                mViewModel.wearManagerLive?.value?.data?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "心率提醒设置")
                    bundle.clear()
                    bundle.putSerializable("value", this)
                    RouteManager.goActRequest(
                        ARouterUrl.HEART_REMIND,
                        this@WearManagerAct, 3005, bundle
                    )
                }
            }
            R.id.mLayoutOo -> {//血氧提醒
                mViewModel.wearManagerLive?.value?.data?.ooValue?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "血氧提醒设置")
                    bundle.clear()
                    bundle.putSerializable("ooValue", this)
                    RouteManager.goActRequest(
                        ARouterUrl.OO_REMIND,
                        this@WearManagerAct, 3006, bundle
                    )
                }
            }
            R.id.mLayoutSport -> {//运动提醒
                mViewModel.wearManagerLive?.value?.data?.sport?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "运动提醒设置")
                    bundle.clear()
                    bundle.putSerializable("sport", this)
                    RouteManager.goActRequest(
                        ARouterUrl.SPORT_REMIND,
                        this@WearManagerAct, 3004, bundle
                    )
                }
            }
            R.id.mLayoutTemp -> {//体温预警
                mViewModel.wearManagerLive?.value?.data?.temp?.apply {
                    onEventObject(baseContext, UmengEventId.WearManager, "体温预警设置")
                    bundle.clear()
                    bundle.putSerializable("temp", this)
                    RouteManager.goActRequest(
                        ARouterUrl.TEMP_REMIND,
                        this@WearManagerAct, 3007, bundle)
                }
            }
            R.id.mTvDevice -> {
                CommonUtils.getMyUserInfo()?.let {
                    onEventObject(baseContext, UmengEventId.WearManager, "设备信息查看")
                    bundle.clear()
                    bundle.putSerializable("user", it)
                    RouteManager.goAct(ARouterUrl.DEV_INFO, bundle)
                }
            }
        }
    }

    private fun setScreenState(@IdRes id: Int, request: Boolean = false) {
        when (id) {
            R.id.mScreen5s -> {
                binding.mScreen5s.isSelected = true
                binding.mScreen10s.isSelected = false
                binding.mScreen15s.isSelected = false
                binding.mScreen30s.isSelected = false
                if (request) mViewModel.screenSetting(1)
            }
            R.id.mScreen10s -> {
                binding.mScreen5s.isSelected = false
                binding.mScreen10s.isSelected = true
                binding.mScreen15s.isSelected = false
                binding.mScreen30s.isSelected = false
                if (request) mViewModel.screenSetting(2)
            }
            R.id.mScreen15s -> {
                binding.mScreen5s.isSelected = false
                binding.mScreen10s.isSelected = false
                binding.mScreen15s.isSelected = true
                binding.mScreen30s.isSelected = false
                if (request) mViewModel.screenSetting(3)
            }
            R.id.mScreen30s -> {
                binding.mScreen5s.isSelected = false
                binding.mScreen10s.isSelected = false
                binding.mScreen15s.isSelected = false
                binding.mScreen30s.isSelected = true
                if (request) mViewModel.screenSetting(4)
            }
        }
    }

    private fun setPowerState(@IdRes id: Int, request: Boolean = false) {
        when (id) {
            R.id.mPowerSave -> {
                binding.mPowerSave.isSelected = true
                binding.mPowerEven.isSelected = false
                binding.mPowerHighlight.isSelected = false
                if (request) mViewModel.lightSetting(1)
            }
            R.id.mPowerEven -> {
                binding.mPowerSave.isSelected = false
                binding.mPowerEven.isSelected = true
                binding.mPowerHighlight.isSelected = false
                if (request) mViewModel.lightSetting(2)
            }
            R.id.mPowerHighlight -> {
                binding.mPowerSave.isSelected = false
                binding.mPowerEven.isSelected = false
                binding.mPowerHighlight.isSelected = true
                if (request) mViewModel.lightSetting(3)
            }
        }
    }


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

        mViewModel.wearManagerLive.observe(this) {
            it?.data?.apply {
                setScreenState(if (screen == 1) R.id.mScreen5s else if (screen == 2) R.id.mScreen10s else if (screen == 3) R.id.mScreen15s else R.id.mScreen30s)
                setPowerState(if (light == 1) R.id.mPowerSave else if (light == 2) R.id.mPowerEven else R.id.mPowerHighlight)
                setDontRemindText(disturb, noonDisturb)
                binding.mTvWater.text =
                    if (water?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                binding.mTvSit.text =
                    if (sit?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)

                setHeartRateText(heartRate)

                setMedicineText(medicine)

                binding.mTvOo.text =
                    if (ooValue?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                binding.mTvSport.text =
                    if (sport?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                binding.mTvTemp.text =
                    if (temp?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
            }
        }
        mViewModel.smsListLive.observe(this){
            LogUtils.d("${it.data}")
            binding.mSmsSwitchBtn.isChecked = it.data.state == 1
            binding.mSmsSwitchBtn.apply {//预警消息开关按钮
                setOnCheckedChangeListener { _, isChecked ->
                    if (!isChecked){
                        smsDialog.show()
                    }else{
                        smsSetting(null, if (binding.mSmsSwitchBtn.isChecked) 1 else 0)
                    }
                }
            }

        }

        mViewModel.smsSettingLive.observe(this){

        }
    }

    private fun smsSetting(path: String?, state: Int){
        mViewModel.smsSetting(path, 1, state)
    }


    private fun setDontRemindText(disturb: Int, noonDisturb: Int){
        binding.mTvDontRemind.text = if (disturb == 0 && noonDisturb == 0) {
            getString(R.string.wear_manager_open)
        } else if (disturb == 0){
            getString(R.string.wear_manager_open)
        } else if (noonDisturb == 0) {
            "午休勿扰已启动"
        } else {
            getString(R.string.wear_manager_close)
        }
    }

    private fun setHeartRateText(list: List<WearManagerBean.HeartRate>) {
        list?.forEach { heartRate ->//只要有一个开启了，都提示开启
            if (heartRate.state == 0) {
                binding.mTvHeart.text = getString(R.string.wear_manager_open)
                return
            }
            binding.mTvHeart.text = getString(R.string.wear_manager_close)
        }
    }

    private fun setMedicineText(list: List<WearManagerBean.Medicine>) {
        if (list == null || list.isEmpty()){
            binding.mTvMedicine.text = "未设置"
        }
        list?.forEach { medicine ->//只要有一个开启了，都提示开启
            if (medicine.state == 0) {
                binding.mTvMedicine.text = getString(R.string.wear_manager_open)
                return
            }
            binding.mTvMedicine.text = getString(R.string.wear_manager_close)
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode === RESULT_OK) {
            when (requestCode) {
                3000 -> {//勿扰模式
                    val disturb = data!!.extras!!.getInt("disturb")
                    val noonDisturb = data!!.extras!!.getInt("noonDisturb")

                    mViewModel.wearManagerLive?.value?.data?.disturb = disturb
                    mViewModel.wearManagerLive?.value?.data?.noonDisturb = noonDisturb
                    setDontRemindText(disturb, noonDisturb)
                }
                3001 -> {//喝水提醒
                    val water = data!!.extras!!.getSerializable("water") as WearManagerBean.Water
                    mViewModel.wearManagerLive?.value?.data?.water = water
                    binding.mTvWater.text =
                        if (water?.state == 0) getString(R.string.wear_manager_open) else getString(
                            R.string.wear_manager_close
                        )
                }
                3002 -> {//久坐提醒
                    val sit = data!!.extras!!.getSerializable("sit") as WearManagerBean.Sit
                    mViewModel.wearManagerLive?.value?.data?.sit = sit
                    binding.mTvSit.text =
                        if (sit?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                }

                3003 -> {//用药提醒
                    val value = data!!.extras!!.getSerializable("value") as WearManagerBean
                    mViewModel.wearManagerLive?.value?.data = value
                    setMedicineText(value.medicine)
                }

                3004 -> {//运动提醒
                    val sport = data!!.extras!!.getSerializable("sport") as WearManagerBean.Sport
                    mViewModel.wearManagerLive?.value?.data?.sport = sport
                    binding.mTvSport.text =
                        if (sport?.state == 0) getString(R.string.wear_manager_open) else getString(
                            R.string.wear_manager_close
                        )
                }

                3005 -> {//心率预警提醒
                    val value = data!!.extras!!.getSerializable("value") as WearManagerBean
                    mViewModel.wearManagerLive?.value?.data = value
                    setHeartRateText(value.heartRate)
                }

                3006 -> {//血氧预警提醒
                    val ooValue = data!!.extras!!.getSerializable("ooValue") as WearManagerBean.OoValue
                    mViewModel.wearManagerLive?.value?.data?.ooValue = ooValue
                    binding.mTvOo.text =
                        if (ooValue?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                }

                3007 -> {//体温预警提醒
                    val tempValue = data!!.extras!!.getSerializable("temp") as WearManagerBean.TempData
                    mViewModel.wearManagerLive?.value?.data?.temp = tempValue
                    binding.mTvTemp.text =
                        if (tempValue?.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                }
            }

        }
    }
}