package com.abbidot.tracker.ui.activity.pet

import android.content.Intent
import android.view.View
import android.widget.NumberPicker
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.constant.ResultCode
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityPetWeightBinding
import com.abbidot.tracker.ui.activity.pet.first.FirstPetProfileActivity
import com.abbidot.tracker.util.NumberPickerValueFill0Format
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class PetWeightActivity : BaseActivity<ActivityPetWeightBinding>(ActivityPetWeightBinding::inflate),
    NumberPicker.OnValueChangeListener {

    private var mPetBean: PetBean? = null
    private var isFirstBind = false

    override fun getTopBar() = mViewBinding.ilPetWeightTopBar.titleTopBar

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

        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
            mPetBean = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)
        }
        var maxTensValue = Util.getPetHeightAndWeightRangeValue(
            ConstantInt.DogPetType, ConstantInt.WeightType, ConstantInt.MaxType
        )
        var minTensValue = Util.getPetHeightAndWeightRangeValue(
            ConstantInt.DogPetType, ConstantInt.WeightType, ConstantInt.MinType
        )
        var defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
            ConstantInt.DogPetType, ConstantInt.WeightType
        )
        var defaultValue2 = 0
        mPetBean?.apply {
            if (petType == ConstantInt.CatPetType) mViewBinding.ivSetPetWeightImage.setImageResource(
                R.drawable.icon_cat_image_svg
            )
            defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
                petType, ConstantInt.WeightType
            )
            minTensValue = Util.getPetHeightAndWeightRangeValue(
                petType, ConstantInt.WeightType, ConstantInt.MinType
            )
            maxTensValue = Util.getPetHeightAndWeightRangeValue(
                petType, ConstantInt.WeightType, ConstantInt.MaxType
            )
            if (weight > 0) {
                var convertHeight = Util.metricConvertUnits(weight, ConstantInt.Type1)
                if (convertHeight > maxTensValue) {
                    convertHeight = maxTensValue.toFloat() + 0.9f
                } else if (convertHeight < minTensValue) {
                    convertHeight = minTensValue.toFloat()
                }
                val values = convertHeight.toString().split(".")
                defaultValue1 = values[0].toInt()
                defaultValue2 = values[1].toInt()
            }
        }

        mViewBinding.apply {
            if (isFirstBind) btnSetPetWeightSave.setText(R.string.register_txt_next)
            root.setBackgroundResource(R.color.white)

            tvSetPetWeightUnit.text = Util.getMetricUnits(mContext, ConstantInt.Type1)

            npSetPetWeightTens.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext,
                    it,
                    minTensValue,
                    maxTensValue,
                    defaultValue1,
                    20,
                    NumberPickerValueFill0Format(),
                    this@PetWeightActivity
                )
            }

            val valueList = mutableListOf<String>()
            for (i in 0..9) {
                valueList.add("$i")
            }
            npSetPetWeightUnit.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext,
                    it,
                    0,
                    9,
                    defaultValue2,
                    20,
                    valueChangeListener = this@PetWeightActivity
                )
                it.displayedValues = valueList.toTypedArray()
            }
            setOnClickListenerViews(btnSetPetWeightSave)
        }
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshPet) {
            finish()
        }
    }

    private fun saveWeight() {
        val value =
            "${mViewBinding.npSetPetWeightTens.value}.${mViewBinding.npSetPetWeightUnit.value}"
        if (isFirstBind) {
            mPetBean?.apply {
                weight = Util.convertMetricUnits(value.toFloat(), ConstantInt.Type1,4)
                val intent = Intent(mContext, FirstPetProfileActivity::class.java)
                intent.putExtra(ConstantString.Pet, this)
                intent.putExtra(ConstantString.isFirstBind, isFirstBind)
                startActivity(intent)
            }
        } else {
//        val petWeight = Util.convertMetricUnits(mContext, value.toFloat(), ConstantInt.Type1,4)
            val intent = Intent()
            intent.putExtra(ConstantString.PetWeight, value.toFloat())
            setResult(ResultCode.ResultCode_3, intent)
            finish()
        }
    }

    override fun onValueChange(picker: NumberPicker?, oldVal: Int, newVal: Int) {
        mViewBinding.apply {
            when (picker) {
                npSetPetWeightTens -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npSetPetWeightUnit, btnSetPetWeightSave
                )

                npSetPetWeightUnit -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npSetPetWeightTens, btnSetPetWeightSave
                )
            }
        }
    }

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