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

import android.content.Intent
import android.text.TextUtils
import android.view.View
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.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityFirstSetPetTypeBinding
import com.abbidot.tracker.ui.activity.account.LoginV2Activity

class FirstSetPetTypeActivity :
    BaseActivity<ActivityFirstSetPetTypeBinding>(ActivityFirstSetPetTypeBinding::inflate) {

    private var isFirstBind = false
    private var mDeviceOutId = ""
    private var mDeviceId = ""
    private lateinit var mPetBean: PetBean

    override fun getTopBar() = mViewBinding.ilFirstSetPetTypeTopBar.titleTopBar

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

        mPetBean = PetBean()
        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
        }

        mDeviceId = MMKVUtil.getString(MMKVKey.FirstDeviceId)
        mDeviceOutId = MMKVUtil.getString(MMKVKey.FirstDeviceOutId)
        mPetBean.let {
            it.deviceId = mDeviceId
            it.deviceOutId = mDeviceOutId
            it.macID = MMKVUtil.getString(MMKVKey.FirstDeviceMac)
        }

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

            rgFirstSetPetTypeGroup.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    rbFirstSetPetTypeDog.id -> {
                        mPetBean.petType = ConstantInt.DogPetType
                    }

                    rbFirstSetPetTypeCat.id -> {
                        mPetBean.petType = ConstantInt.CatPetType
                    }
                }
            }

            setViewClickHideInputMethod(llFirstSetPetTypeLayout)

            setOnClickListenerViews(btnFirstSetPetTypeNext, btnFirstSetPetTypeBackLogin)
        }
    }

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

    override fun listenBackPressed() {

    }

    private fun petTypeNext() {
        mViewBinding.apply {
            var name = ilFirstSetPetTypePetName.etInputContent.text.toString()
            if (TextUtils.isEmpty(name)) {
                showToast(R.string.txt_not_null)
                return
            }
            name = name.trimStart()
            name = name.trimEnd()
            mPetBean.petName = name
            val intent = Intent(mContext, FirstMeasurementUnitsActivity::class.java)
            intent.putExtra(ConstantString.Pet, mPetBean)
            intent.putExtra(ConstantString.isFirstBind, isFirstBind)
            startActivity(intent)
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnFirstSetPetTypeNext -> petTypeNext()
                btnFirstSetPetTypeBackLogin -> startActivityFinish(
                    Intent(
                        mContext, LoginV2Activity::class.java
                    )
                )
            }
        }
    }
}