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

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityFirstMeasurementUnitsBinding
import com.abbidot.tracker.ui.activity.pet.PetHeightActivity
import com.abbidot.tracker.ui.common.UnitSelectDialog
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.UserProfileViewModel

/**
 * 默认英制
 * Imperial: in\lbs\mph\ft
 * Metric: cm\kg\km/h\m,km
 *
 * 英制范围：
身高数据范围：1.0~99.9 in；
体重数据范围：1.0~349.9 lbs。

公制范围：
身高数据范围：1.0~199.9 cm；
体重数据范围：1.0~159.9 kg。
 */
class FirstMeasurementUnitsActivity :
    BaseActivity<ActivityFirstMeasurementUnitsBinding>(ActivityFirstMeasurementUnitsBinding::inflate) {

    private val mUserProfileViewModel: UserProfileViewModel by viewModels()

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

    private lateinit var mUnitSelectDialog: UnitSelectDialog
    private var mMeasurementUnit = 1

//    private var mWeightUnitDialog: CommonListDialog? = null

    override fun getTopBar() = mViewBinding.ilFirstMeasurementUnitsTopBar.titleTopBar

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

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

        mViewBinding.apply {
            root.setBackgroundResource(R.color.white)
            ilFirstMeasurementUnitsSelect.let {
                it.tvInputNameTitle.setText(R.string.txt_set_measurement_units)
                ViewUtil.instance.setEditTextDropSelectState(it.etInputContent)
                it.ivInputRepresentImage.visibility = View.VISIBLE

                val unit = Util.getMeasureUnitName(mContext)
                it.etInputContent.setText(unit)
            }

            setOnClickListenerViews(
                btnFirstMeasurementUnitsSave,
                ilFirstMeasurementUnitsSelect.rlEtInputLayout,
                ilFirstMeasurementUnitsSelect.ivInputRepresentImage
            )

            mUnitSelectDialog = UnitSelectDialog(mContext)
        }
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshPet) {
            finish()
        }
        mUserProfileViewModel.mUpdateMeasureUnitLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        setPetUnit()
                    }
                }
            })
        }
    }


//    private fun showWeightUnitDialog() {
//        if (null == mWeightUnitDialog) {
//            val weightUnitList = mutableListOf<DataBean>()
//            DataBean().run {
//                name = getString(R.string.txt_lb)
//                weightUnitList.add(this)
//            }
//            DataBean().run {
//                name = getString(R.string.txt_kg)
//                weightUnitList.add(this)
//            }
//            val weightUnitAdapter = TextViewAdapter(mContext, weightUnitList).apply {
//                setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
//                    override fun onItemClick(itemView: View?, pos: Int) {
//                        mViewBinding.ilFirstMeasurementUnitsWeight.etInputContent.setText(
//                            weightUnitList[pos].name
//                        )
//                        mWeightUnitDialog?.dismiss()
//                    }
//                })
//            }
//            mWeightUnitDialog = CommonListDialog(mContext, weightUnitAdapter, 3, 3)
//        }
//        mWeightUnitDialog?.show()
//    }

    private fun setPetUnit() {
        mViewBinding.apply {
            MMKVUtil.putInt(MMKVKey.MeasureUnit, mMeasurementUnit)
            if (isFirstBind) {
                val intent = Intent(mContext, PetHeightActivity::class.java)
                intent.putExtra(ConstantString.Pet, mPetBean)
                intent.putExtra(ConstantString.isFirstBind, isFirstBind)
                startActivity(intent)
            } else finish()
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                ilFirstMeasurementUnitsSelect.rlEtInputLayout, ilFirstMeasurementUnitsSelect.ivInputRepresentImage -> mUnitSelectDialog.showUnitDialog(
                    object : BaseDialog.OnDialogSelectListener {
                        override fun onSelectClick(dialog: BaseDialog<*>, any: Any) {
                            if (any is Int) {
                                mMeasurementUnit = any
                                val unit =
                                    if (any == ConstantInt.Type1) getString(R.string.txt_imperial)
                                    else getString(R.string.txt_metric)
                                ilFirstMeasurementUnitsSelect.etInputContent.setText(
                                    unit
                                )
                            }
                        }
                    })

                btnFirstMeasurementUnitsSave -> mUserProfileViewModel.updateMeasureUnit(
                    mMeasurementUnit
                )
            }
        }
    }
}