package com.abbidot.tracker.ui.activity.data

import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.ActiveTimeBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityDataBoardBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.DataDetailViewModel

class DataBoardActivity :
    BaseActivity<ActivityDataBoardBinding>(ActivityDataBoardBinding::inflate) {

    private val mDataDetailViewModel: DataDetailViewModel by viewModels()

    private var mPetBean: PetBean? = null

    override fun getTopBar() = mViewBinding.dataBoardTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_data_board)

        intent?.extras?.apply {
            mPetBean = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)
        }
//        if (null == mPetBean) finish()

        mViewBinding.apply {
            root.setBackgroundResource(R.color.white)
            ilDataBoardTotalDistance.tvDataBoardUnitItem.text =
                Util.getMetricUnits(mContext, ConstantInt.Type4)
            ilDataBoardAvgActivityTime.let {
                it.tvDataBoardTitleItem.text = getString(R.string.txt_avg_activity_time)
                it.tvDataBoardUnitItem.text = getString(R.string.txt_min)
            }
            ilDataBoardAvgCalories.let {
                it.tvDataBoardTitleItem.text = getString(R.string.txt_avg_calories)
                it.tvDataBoardUnitItem.text = getString(R.string.txt_calorie_unit)
            }
            ilDataBoardAvgSleepTime.let {
                it.tvDataBoardTitleItem.text = getString(R.string.txt_avg_sleep_time)
                it.tvDataBoardUnitItem.text = getString(R.string.txt_min)
                it.tvDataBoardHrValueItem.visibility = View.VISIBLE
                it.tvDataBoardHrUnitItem.visibility = View.VISIBLE
            }
            setOnClickListenerViews(btnDataBoardSave)
//            rsDataBoardSetGoalView.setValue(0)
        }

        mPetBean?.apply {
            ViewUtil.instance.setPetTypeHead(
                mViewBinding.ivDataBoardPetHead.appHeadImage, imgurl, petType
            )
            mDataDetailViewModel.getPetActivityDetailInfo(this@DataBoardActivity, petId, deviceId)
        }
    }

    override fun liveDataObserve() {
        mDataDetailViewModel.apply {
            mPetActivityInfoLiveData.observe(this@DataBoardActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.let { activeTimeBean ->
                            setActivityInfoData(activeTimeBean)
                        }
                    }

                    override fun onRequestError(exceptionCode: String?) {
                        super.onRequestError(exceptionCode)
                        mViewBinding.rsDataBoardSetGoalView.setValue(0)
                    }
                })
            }

            mSetGoalLiveData.observe(this@DataBoardActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.let {
                            XEventBus.post(EventName.SetGoal)
                            MMKVUtil.putInt(
                                MMKVKey.ActivityGoal, mViewBinding.rsDataBoardSetGoalView.getValue()
                            )
                            showToast(R.string.txt_save_successful, isFinish = true)
                        }
                    }
                })
            }
        }
    }

    private fun setActivityInfoData(activeTimeBean: ActiveTimeBean) {
        mViewBinding.apply {
            activeTimeBean.let {
                ViewUtil.instance.viewNumberValueAnimator(
                    ilDataBoardTotalDistance.tvDataBoardValueItem,
                    0,
                    Util.metricConvertUnits(it.distance.toFloat(), ConstantInt.Type4, 2),
                    animatorDuration = 1500L
                )
                ViewUtil.instance.viewNumberValueAnimator(
                    ilDataBoardAvgActivityTime.tvDataBoardValueItem,
                    0,
                    it.avgActiveTime / 60,
                    animatorDuration = 1500L
                )
                ViewUtil.instance.viewNumberValueAnimator(
                    ilDataBoardAvgCalories.tvDataBoardValueItem,
                    0,
                    if (it.avgCalories == 0) 0 else 9 / it.avgCalories,
                    animatorDuration = 1500L
                )
                val avgSleepTimeHr = it.avgSleepTime / 3600
                val avgSleepTimeMin = (it.avgSleepTime % 3600) / 60
                ViewUtil.instance.viewNumberValueAnimator(
                    ilDataBoardAvgSleepTime.tvDataBoardHrValueItem,
                    0,
                    avgSleepTimeHr,
                    animatorDuration = 1500L
                )
                ViewUtil.instance.viewNumberValueAnimator(
                    ilDataBoardAvgSleepTime.tvDataBoardValueItem,
                    0,
                    avgSleepTimeMin,
                    animatorDuration = 1500L
                )
                rsDataBoardSetGoalView.setValue(it.goal)
            }
        }
    }

    private fun savePetGoal() {
        mPetBean?.apply {
            mDataDetailViewModel.setGoal(
                this@DataBoardActivity, deviceId, mViewBinding.rsDataBoardSetGoalView.getValue()
            )
        }
    }

    override fun leftBackOnClick() {
        mViewBinding.apply {
            val goal = MMKVUtil.getInt(MMKVKey.ActivityGoal, ConstantInt.DefaultSportGoal)
            if (rsDataBoardSetGoalView.getValue() == goal) super.leftBackOnClick()
            else ViewUtil.instance.showDialog(
                mContext,
                R.string.txt_weather_save,
                object : BaseDialog.OnDialogOkListener {
                    override fun onOkClick(dialog: BaseDialog<*>) {
                        dialog.dismiss()
                        savePetGoal()
                    }
                },
                okTextResId = R.string.txt_sure,
                cancelListener = object : BaseDialog.OnDialogCancelListener {
                    override fun onCancelClick(dialog: BaseDialog<*>) {
                        dialog.dismiss()
                        finish()
                    }
                })
        }
    }


    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnDataBoardSave -> savePetGoal()
            }
        }
    }
}