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

import android.content.Intent
import android.view.View
import android.widget.NumberPicker
import com.abbidot.baselibrary.constant.EventName
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.ActivityFirstSetPetHeightBinding
import com.abbidot.tracker.util.NumberPickerValueFill0Format
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class FirstSetPetHeightActivity :
    BaseActivity<ActivityFirstSetPetHeightBinding>(ActivityFirstSetPetHeightBinding::inflate),
    NumberPicker.OnValueChangeListener {

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

    override fun getTopBar() = mViewBinding.ilFirstSetPetHeightTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle("")

        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
            mPetBean = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)
        }

        var maxTensValue = Util.getPetHeightAndWeightRangeValue(
            ConstantInt.DogPetType, ConstantInt.HeightType, ConstantInt.MaxType
        )
        var minTensValue = Util.getPetHeightAndWeightRangeValue(
            ConstantInt.DogPetType, ConstantInt.HeightType, ConstantInt.MinType
        )
        var defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
            ConstantInt.DogPetType, ConstantInt.HeightType
        )
        var defaultValue2 = 0
        mPetBean?.apply {
            if (petType == ConstantInt.CatPetType) mViewBinding.ivFirstSetPetHeightType.setImageResource(
                R.drawable.icon_cat_height_tip_svg
            )
            defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
                petType, ConstantInt.HeightType
            )
            minTensValue = Util.getPetHeightAndWeightRangeValue(
                petType, ConstantInt.HeightType, ConstantInt.MinType
            )
            maxTensValue = Util.getPetHeightAndWeightRangeValue(
                petType, ConstantInt.HeightType, ConstantInt.MaxType
            )
            if (height > 0) {
                val values =
                    Util.metricConvertUnits(height, ConstantInt.Type0).toString().split(".")
                defaultValue1 = values[0].toInt()
                defaultValue2 = values[1].toInt()
            }
        }

        mViewBinding.apply {
            root.setBackgroundResource(R.color.white)

            tvFirstSetPetHeightUnit.text = Util.getMetricUnits(mContext, ConstantInt.Type0)
            npFirstSetPetHeightTens.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext,
                    it,
                    minTensValue,
                    maxTensValue,
                    defaultValue1,
                    20,
                    NumberPickerValueFill0Format(),
                    this@FirstSetPetHeightActivity
                )
            }

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

            setOnClickListenerViews(btnFirstSetPetHeightNext)
        }
    }

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

    private fun saveHeight() {
        mPetBean?.apply {
            val value =
                "${mViewBinding.npFirstSetPetHeightTens.value}.${mViewBinding.npFirstSetPetHeightUnit.value}"
            height = Util.convertMetricUnits(value.toFloat(), ConstantInt.Type0)
            val intent = Intent(mContext, FirstSetPetWeightActivity::class.java)
            intent.putExtra(ConstantString.Pet, this)
            intent.putExtra(ConstantString.isFirstBind, isFirstBind)
            startActivity(intent)
        }
    }

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

    override fun onValueChange(picker: NumberPicker, oldVal: Int, newVal: Int) {
        mViewBinding.apply {
            when (picker) {
                npFirstSetPetHeightTens -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npFirstSetPetHeightUnit, btnFirstSetPetHeightNext
                )

                npFirstSetPetHeightUnit -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npFirstSetPetHeightTens, btnFirstSetPetHeightNext
                )
            }
        }
    }
}