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

import android.text.InputType
import android.text.TextUtils
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.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.FamilyAvatarAdapter
import com.abbidot.tracker.adapter.FamilyRelationAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.bean.FamilyBean
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.ActivityInviteFamilyBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.FamilyViewModel

/**
 * 邀请家人
 */
class InviteFamilyActivity :
    BaseActivity<ActivityInviteFamilyBinding>(ActivityInviteFamilyBinding::inflate) {

    private val mFamilyViewModel: FamilyViewModel by viewModels()

    private lateinit var mRelationAdapter: FamilyRelationAdapter
    private lateinit var mAvatarAdapter: FamilyAvatarAdapter
    private lateinit var mFamilyBean: FamilyBean
    private var mPetBean: PetBean? = null

    override fun getTopBar() = mViewBinding.inviteFamilyTopBar.titleTopBar

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

        mFamilyBean = FamilyBean().apply {
            userId = MMKVUtil.getString(MMKVKey.UserId)
            userName = MMKVUtil.getString(MMKVKey.UserName)
            sendEmail = MMKVUtil.getString(MMKVKey.Email)
        }

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

        mPetBean?.apply {
            mFamilyBean.let {
                it.deviceId = deviceId
                it.petId = petId
                it.petName = petName
            }
        }

        mViewBinding.apply {
            ilInviteFamilyEmail.let {
                it.tvInputNameTitle.text = getString(R.string.txt_email_address)
                it.etInputContent.setHint(R.string.txt_email_address)
                it.etInputContent.inputType = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
                ViewUtil.instance.setInputViewErrorHideClick(
                    mContext, it.rlEtInputLayout, tvEmailErrorTipInviteFamily, it.etInputContent
                )
            }

            setData()

            ViewUtil.instance.setRecyclerViewFlexboxLayout(
                mContext, rvInviteFamilyRelation, mRelationAdapter, right = AppUtils.dpToPx(12)
            )
            ViewUtil.instance.setRecyclerViewFlexboxLayout(
                mContext, rvInviteFamilyAvatar, mAvatarAdapter, right = AppUtils.dpToPx(16)
            )

            setViewClickHideInputMethod(vInviteFamilyAnimate)
            setViewClickHideInputMethod(llInviteFamilyLayout)
            setViewClickHideInputMethod(rvInviteFamilyAvatar)
            setOnClickListenerViews(btnInviteFamilyInvite)
        }

        setListenKeyboardChange(210,methodType = ConstantInt.Type1)
    }

    override fun liveDataObserve() {
        mFamilyViewModel.mInviteLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    XEventBus.post(EventName.RefreshFamily)
                    showToast(R.string.tracker_manage_set_add_family_succ, true)
                }

                override fun onRequestError(exceptionCode: String?) {
                    mViewBinding.btnInviteFamilyInvite.isEnabled = true
                }
            })
        }
    }

    private fun setData() {
        val relationList = mutableListOf<DataBean>()
        DataBean().apply {
            name = getString(R.string.tracker_manage_set_family)
            selected = true
            relationList.add(this)
        }
        DataBean().apply {
            name = getString(R.string.txt_friend)
            relationList.add(this)
        }
        DataBean().apply {
            name = getString(R.string.txt_love)
            relationList.add(this)
        }
        DataBean().apply {
            name = getString(R.string.txt_colleague)
            relationList.add(this)
        }
        mRelationAdapter = FamilyRelationAdapter(mContext, relationList).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    ViewUtil.instance.updateDataBeanOnlySelectedItem(
                        mRelationAdapter, relationList, pos
                    )
                    mFamilyBean.relation = pos + 1
                }
            })
        }

        val avatarList = mutableListOf<DataBean>()
        DataBean().apply {
            imageId = R.drawable.icon_family_boy_avatar
            selected = true
            avatarList.add(this)
        }
        DataBean().apply {
            imageId = R.drawable.icon_family_girl_avatar
            avatarList.add(this)
        }
        mAvatarAdapter = FamilyAvatarAdapter(mContext, avatarList).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    hideInputMethod(mViewBinding.root)
                    ViewUtil.instance.updateDataBeanOnlySelectedItem(
                        mAvatarAdapter, avatarList, pos
                    )
                    mFamilyBean.avatar = pos + 1
                }
            })
        }
    }

    private fun inviteOk() {
        mViewBinding.apply {
            val email = ilInviteFamilyEmail.etInputContent.text.toString()
            if (TextUtils.isEmpty(email) || !Utils.isValidEmail(email)) {
                ViewUtil.instance.setInputViewErrorShow(
                    mContext, ilInviteFamilyEmail.rlEtInputLayout, tvEmailErrorTipInviteFamily
                )
                return
            }
            mFamilyBean.acceptEmail = email
            btnInviteFamilyInvite.isEnabled = false
            mFamilyViewModel.invite(this@InviteFamilyActivity, mFamilyBean)
        }
    }

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

    override fun onDestroy() {
        super.onDestroy()
    }
}