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.ActivityFirstSetPetWeightBinding
import com.abbidot.tracker.util.NumberPickerValueFill0Format
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class FirstSetPetWeightActivity :
    BaseActivity<ActivityFirstSetPetWeightBinding>(ActivityFirstSetPetWeightBinding::inflate),
    NumberPicker.OnValueChangeListener {

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

    override fun getTopBar() = mViewBinding.ilFirstSetPetWeightTopBar.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.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.ivFirstSetPetWeightImage.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) {
                val values =
                    Util.metricConvertUnits(weight, ConstantInt.Type1).toString().split(".")
                defaultValue1 = values[0].toInt()
                defaultValue2 = values[1].toInt()
            }
        }

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

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

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

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

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

    private fun saveWeight() {
        mPetBean?.apply {
            val value =
                "${mViewBinding.npFirstSetPetWeightTens.value}.${mViewBinding.npFirstSetPetWeightUnit.value}"
            weight = Util.convertMetricUnits(value.toFloat(), ConstantInt.Type1)
            val intent = Intent(mContext, FirstPetProfileActivity::class.java)
            intent.putExtra(ConstantString.Pet, this)
            intent.putExtra(ConstantString.isFirstBind, isFirstBind)
            startActivity(intent)
        }
    }

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

    override fun onValueChange(picker: NumberPicker?, oldVal: Int, newVal: Int) {
        mViewBinding.apply {
            when (picker) {
                npFirstSetPetWeightTens -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npFirstSetPetWeightUnit, btnFirstSetPetWeightNext
                )

                npFirstSetPetWeightUnit -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npFirstSetPetWeightTens, btnFirstSetPetWeightNext
                )
            }
        }
    }
}