package com.abbidot.tracker.dialog

import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.widget.NumberPicker
import android.widget.NumberPicker.OnValueChangeListener
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.databinding.DialogSetPetWeightLayoutBinding
import com.abbidot.tracker.util.NumberPickerValueFill0Format
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

/**
 *Created  by .yzq on 2024/2/20/020.
 * @link
 * @description:
 */
class SetPetWeightDialog(
    context: Context, petBean: PetBean, onSelectListener: OnDialogSelectListener? = null
) : BaseDialog<DialogSetPetWeightLayoutBinding>(
    DialogSetPetWeightLayoutBinding::inflate, context
), OnValueChangeListener {

    private val mOnSelectListener = onSelectListener
    private var mPetBean = petBean

    override fun initView() {
        mViewBinding.apply {

            var maxTensValue: Int
            var minTensValue: Int
            var defaultValue1: Int
            var defaultValue2 = 0
            mPetBean.let {
                defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
                    it.petType, ConstantInt.WeightType
                )
                minTensValue = Util.getPetHeightAndWeightRangeValue(
                    it.petType, ConstantInt.WeightType, ConstantInt.MinType
                )
                maxTensValue = Util.getPetHeightAndWeightRangeValue(
                    it.petType, ConstantInt.WeightType, ConstantInt.MaxType
                )
                if (it.weight > 0) {
                    var convertWeight = Util.metricConvertUnits(it.weight, ConstantInt.Type1)
                    if (convertWeight < minTensValue) {
                        convertWeight = minTensValue.toFloat()
                    }
                    val values = convertWeight.toString().split(".")
                    defaultValue1 = values[0].toInt()
                    defaultValue2 = values[1].toInt()
                }
            }

            tvDialogWeightSetUnit.text = Util.getMetricUnits(context, ConstantInt.Type1)

            npDialogPetWeightTens.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    context,
                    it,
                    minTensValue,
                    maxTensValue,
                    defaultValue1,
                    20,
                    NumberPickerValueFill0Format(),
                    this@SetPetWeightDialog
                )
            }

            val valueList = mutableListOf<String>()
            for (i in 0..9) {
                valueList.add("$i")
            }
            npDialogPetWeightUnit.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    context,
                    it,
                    0,
                    9,
                    defaultValue2,
                    20,
                    valueChangeListener = this@SetPetWeightDialog
                )
                it.displayedValues = valueList.toTypedArray()
            }
            npDialogWeightUnitSwitch.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    context, it, 0, 1, 0, 20
                )
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                    it.selectionDividerHeight = 0
                }
                val unitArray =
                    arrayOf(context.getString(R.string.txt_lb), context.getString(R.string.txt_kg))
                it.displayedValues = unitArray
            }

            val value = "${npDialogPetWeightTens.value}.${npDialogPetWeightUnit.value}"
            mOnSelectListener?.onSelectClick(this@SetPetWeightDialog, value)
        }
    }

    override fun onValueChange(picker: NumberPicker?, oldVal: Int, newVal: Int) {
        var value = ""
        mViewBinding.apply {
            when (picker!!) {
                npDialogPetWeightTens -> {
                    value = "${newVal}.${npDialogPetWeightUnit.value}"
                }

                npDialogPetWeightUnit -> {
                    value = "${npDialogPetWeightTens.value}.${newVal}"
                }
            }
        }
        if (!TextUtils.isEmpty(value) && value != "0.0") {
            mOnSelectListener?.onSelectClick(this@SetPetWeightDialog, value)
        }
    }


    fun updatePetData(petBean: PetBean) {
        mPetBean = petBean
        mViewBinding.apply {
            petBean.let {
                var defaultValue1 = Util.getPetHeightAndWeightDefaultValue(
                    it.petType, ConstantInt.WeightType
                )
                var defaultValue2 = 0
                val minTensValue = Util.getPetHeightAndWeightRangeValue(
                    it.petType, ConstantInt.WeightType, ConstantInt.MinType
                )
                val maxTensValue = Util.getPetHeightAndWeightRangeValue(
                    it.petType, ConstantInt.WeightType, ConstantInt.MaxType
                )
                if (it.weight > 0) {
                    var convertWeight = Util.metricConvertUnits(it.weight, ConstantInt.Type1)
                    if (convertWeight < minTensValue) {
                        convertWeight = minTensValue.toFloat()
                    }
                    val values = convertWeight.toString().split(".")
                    defaultValue1 = values[0].toInt()
                    defaultValue2 = values[1].toInt()
                }

                npDialogPetWeightTens.let { np ->
                    ViewUtil.instance.setNumberPickerAttributes(
                        context, np, minTensValue, maxTensValue, defaultValue1
                    )
                }
                npDialogPetWeightUnit.let { np ->
                    ViewUtil.instance.setNumberPickerAttributes(
                        context, np, 0, 9, defaultValue2
                    )
                }
            }
        }
    }
}