package com.zj365.mime.act

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.zj365.mime.R
import com.zj365.mime.databinding.ActHeartRemindBinding
import com.zj365.mime.vm.WearManagerVm
import com.xty.network.model.WearManagerBean

/**
 * 心率预警提醒
 */
@Route(path = ARouterUrl.HEART_REMIND)
class HeartRemindAct : BaseVmAct<WearManagerVm>() {
    val binding by lazy { ActHeartRemindBinding.inflate(layoutInflater) }

    var bean: WearManagerBean? = null

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

    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("value", bean)
            intent.putExtras(bundle)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.title.mTvTitle.text = getString(R.string.wear_manager_08)

        bean?.heartRate?.forEach {
            val statu = if (it.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
            when(it.hrState){
                0 -> {
                    binding.mTvHeartStatu1.text = statu
                }
                1 -> {
                    binding.mTvHeartStatu2.text = statu
                }
                2 -> {
                    binding.mTvHeartStatu3.text = statu
                }
            }
        }

        binding.mLayoutSpeed.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("heartRate", getHeartInfo(0))
            RouteManager.goActRequest(
                ARouterUrl.HEART_SPEED_REMIND,
                this, 30051, bundle
            )
        }
        binding.mLayoutLow.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("heartRate", getHeartInfo(1))
            RouteManager.goActRequest(
                ARouterUrl.HEART_LOW_REMIND,
                this, 30052, bundle
            )
        }

        binding.mLayoutSport.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("heartRate", getHeartInfo(2))
            RouteManager.goActRequest(
                ARouterUrl.HEART_SPORT_REMIND,
                this, 30053, bundle
            )
        }


    }

    private fun getHeartInfo(statu: Int): WearManagerBean.HeartRate?{
        bean?.heartRate?.forEach {
            if(it.hrState == statu){
                return it
            }
        }
        return null
    }

    private fun setHeartInfo(statu: Int, data: WearManagerBean.HeartRate){
        bean?.heartRate?.forEachIndexed { index, heartRate ->
            if(heartRate.hrState == statu){
                bean?.heartRate!![index].state = data.state
            }
        }
    }


    override fun onBackPressed() {
        bundle.clear()
        bundle.putSerializable("value", bean)
        intent.putExtras(bundle)
        setResult(RESULT_OK, intent)
        finish()
    }


    override fun liveObserver() {

    }

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

        if (resultCode === RESULT_OK) {
            when (requestCode) {
                30051 -> {
                    val heartRate = data!!.extras!!.getSerializable("heartRate") as WearManagerBean.HeartRate
                    binding.mTvHeartStatu1.text = if (heartRate.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                    setHeartInfo(0, heartRate)
                }
                30052 -> {
                    val heartRate = data!!.extras!!.getSerializable("heartRate") as WearManagerBean.HeartRate
                    binding.mTvHeartStatu2.text = if (heartRate.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                    setHeartInfo(1, heartRate)
                }
                30053 -> {
                    val heartRate = data!!.extras!!.getSerializable("heartRate") as WearManagerBean.HeartRate
                    binding.mTvHeartStatu3.text = if (heartRate.state == 0) getString(R.string.wear_manager_open) else getString(R.string.wear_manager_close)
                    setHeartInfo(2, heartRate)
                }
            }
        }
    }

}