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

import android.content.Intent
import android.os.Build
import android.view.View
import android.widget.NumberPicker
import android.widget.NumberPicker.OnValueChangeListener
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.ActivityPetHeightBinding
import com.abbidot.tracker.util.NumberPickerValueFill0Format
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class PetHeightActivity : BaseActivity<ActivityPetHeightBinding>(ActivityPetHeightBinding::inflate),
    OnValueChangeListener {

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

    override fun getTopBar() = mViewBinding.ilPetHeightTopBar.titleTopBar

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

        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.ivSetHeightPetType.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) {
                var convertHeight = Util.metricConvertUnits(height, ConstantInt.Type0)
                if (convertHeight < minTensValue) {
                    convertHeight = minTensValue.toFloat()
                }
                val values = convertHeight.toString().split(".")
                defaultValue1 = values[0].toInt()
                defaultValue2 = values[1].toInt()
            }
        }

        mViewBinding.apply {
            if (isFirstBind) btnPetHeightSave.setText(R.string.register_txt_next)
            root.setBackgroundResource(R.color.white)
            tvShowHeightUnit.text = Util.getMetricUnits(mContext, ConstantInt.Type0)
            npDialogPetHeightTens.let {
//                ReflectHelper.of(it).setField("SELECTOR_WHEEL_ITEM_COUNT", 2)
//                // 获取Class实例
//                val clazz = it.javaClass
//                // 获取Field实例
//                val field = clazz.getDeclaredField("SELECTOR_WHEEL_ITEM_COUNT")
//                // 提升访问权限
//                field.isAccessible = true
//                // 修改字段值
//                field.set(it, 2)
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext,
                    it,
                    minTensValue,
                    maxTensValue,
                    defaultValue1,
                    20,
                    NumberPickerValueFill0Format(),
                    this@PetHeightActivity
                )
            }

            val valueList = mutableListOf<String>()
            for (i in 0..9) {
                valueList.add("$i")
            }
            npDialogPetHeightUnit.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext,
                    it,
                    0,
                    9,
                    defaultValue2,
                    20,
                    valueChangeListener = this@PetHeightActivity
                )
                it.displayedValues = valueList.toTypedArray()
            }
            npDialogHeightUnitSwitch.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    mContext, it, 0, 1, 0, 20
                )
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                    it.selectionDividerHeight = 0
                }
                val unitArray = arrayOf(getString(R.string.txt_in), getString(R.string.txt_cm))
                it.displayedValues = unitArray
            }

            setOnClickListenerViews(btnPetHeightSave)
        }
    }

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

    private fun saveHeight() {
        val value =
            "${mViewBinding.npDialogPetHeightTens.value}.${mViewBinding.npDialogPetHeightUnit.value}"
        if (isFirstBind) {
            mPetBean?.apply {
                height = Util.convertMetricUnits(value.toFloat(), ConstantInt.Type0,4)
                val intent = Intent(mContext, PetWeightActivity::class.java)
                intent.putExtra(ConstantString.Pet, this)
                intent.putExtra(ConstantString.isFirstBind, isFirstBind)
                startActivity(intent)
            }
        } else {
//        val petHeight = Util.convertMetricUnits(mContext, value.toFloat(), ConstantInt.Type0,4)
            val intent = Intent()
            intent.putExtra(ConstantString.PetHeight, value.toFloat())
            setResult(ResultCode.ResultCode_2, intent)
            finish()
        }
    }

    override fun onValueChange(picker: NumberPicker, oldVal: Int, newVal: Int) {
        mViewBinding.apply {
            when (picker) {
                npDialogPetHeightTens -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npDialogPetHeightUnit, btnPetHeightSave
                )

                npDialogPetHeightUnit -> ViewUtil.instance.numberPickerLinkageValueNo0(
                    newVal, npDialogPetHeightTens, btnPetHeightSave
                )
            }
        }
    }

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