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

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.FamilyMembersAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.FamilyBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityFamilyMembersBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.FamilyViewModel

/**
 * 家人成员
 */
class FamilyMembersActivity :
    BaseActivity<ActivityFamilyMembersBinding>(ActivityFamilyMembersBinding::inflate) {

    private val mFamilyViewModel: FamilyViewModel by viewModels()

    private lateinit var mFamilyMembersAdapter: FamilyMembersAdapter

    private var mPetBean: PetBean? = null

    override fun getTopBar() = mViewBinding.familyMembersTopBar.titleTopBar

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

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

        mFamilyMembersAdapter = FamilyMembersAdapter(mContext, null)

        mViewBinding.apply {

            mPetBean?.let {
                ViewUtil.instance.setPetTypeHead(
                    ilFamilyMembersPetHead.appHeadImage, it.imgurl, it.petType
                )
                mFamilyViewModel.getFamilies(this@FamilyMembersActivity, it.deviceId)
            }

            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvFamilyMembers, mFamilyMembersAdapter, bottom = AppUtils.dpToPx(12)
            )
            setOnClickListenerViews(btnFamilyMembersAddInvite)
        }
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshFamily) {
            mPetBean?.apply {
                mFamilyViewModel.getFamilies(this@FamilyMembersActivity, deviceId)
            }
        }

        mFamilyViewModel.apply {
            mFamilyListLiveData.observe(this@FamilyMembersActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        mFamilyMembersAdapter.setData(null)
                        it.getOrNull()?.let { familyPet ->
                            familyPet.families?.let { list ->
                                mFamilyMembersAdapter.setData(list, true)
                            }
                        }
                    }
                })
            }

            mFamilyRemoveLiveData.observe(this@FamilyMembersActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        XEventBus.post(EventName.DeleteFamily)
                        showToast(R.string.txt_delete_success)
                        mPetBean?.apply {
                            mFamilyViewModel.getFamilies(this@FamilyMembersActivity, deviceId)
                        }
                    }
                })
            }

            mInviteLiveData.observe(this@FamilyMembersActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        showToast(R.string.tracker_manage_set_add_family_succ)
                    }
                })
            }
        }
    }

    fun deleteFamily(familyBean: FamilyBean) {
        val str = String.format(
            mContext.getString(R.string.txt_family_delete), familyBean.acceptEmail
        )
        ViewUtil.instance.showDialog(mContext, str, object : BaseDialog.OnDialogOkListener {
            override fun onOkClick(dialog: BaseDialog<*>) {
                dialog.dismiss()
                mFamilyViewModel.removeFamily(this@FamilyMembersActivity, familyBean.familyId)
            }
        }, okTextResId = R.string.start_txt_yes)
    }

    fun resendFamily(familyBean: FamilyBean) {
        mFamilyViewModel.invite(this, familyBean)
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnFamilyMembersAddInvite -> {
                    mPetBean?.let { pet ->
                        Intent(mContext, InviteFamilyActivity::class.java).let {
                            it.putExtra(ConstantString.Pet, pet)
                            startActivity(it)
                        }
                    }
                }
            }
        }
    }
}