package com.abbidot.tracker.ui.activity.subscribe

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.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.SubscriptionPlanAdapter
import com.abbidot.tracker.adapter.SubscriptionPlanDetailAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.SubscriptionsOrderBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivitySubscriptionPlanBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.SubscriptionManageViewModel

/**
 * 选择订阅计划
 */
class SubscriptionPlanActivity :
    BaseActivity<ActivitySubscriptionPlanBinding>(ActivitySubscriptionPlanBinding::inflate) {

    private val mSubscriptionViewModel: SubscriptionManageViewModel by viewModels()

    private lateinit var mPlanAdapter: SubscriptionPlanAdapter
    private lateinit var mPlanDetailAdapter: SubscriptionPlanDetailAdapter

    private var mSelectPlanIndex = 0

    private var mOrderBean: SubscriptionsOrderBean? = null

    //设备充值类型，0=首次设备充值激活，1=已添加设备再次添加设备充值，2=给当前的设备续费或者升级套餐
    private var mRechargeType = ConstantInt.Type1

    //设备类型
    private var mType = ConstantInt.Type1

    override fun getTopBar() = mViewBinding.subscribePlanTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_activate_subscribe)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        intent.extras?.apply {
            mOrderBean = Util.getParcelableAdaptive(
                intent, ConstantString.LkSetMeal, SubscriptionsOrderBean::class.java
            )
            mType = getInt(ConstantString.Type, ConstantInt.Type1)
            val isFirstBind = getBoolean(ConstantString.isFirstBind, false)
            mRechargeType = getInt(ConstantString.RechargeType, ConstantInt.Type1)
            if (isFirstBind) mRechargeType = ConstantInt.Type0
        }

        addListData()
        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewGridLayout(
                mContext,
                rvSubscribePlanList,
                mPlanAdapter,
                2,
                AppUtils.dpToPx(140),
                offset = AppUtils.dpToPx(66)
            )

            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvSubscribePlanDetailList, mPlanDetailAdapter, bottom = AppUtils.dpToPx(8)
            )

            setOnClickListenerViews(btnSelectSubscribePlanContinue)
        }

        mSubscriptionViewModel.getPackageType()
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.PayBack) {
            finish()
        }
        mSubscriptionViewModel.mPackageTypeLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.let { list ->
                        list[0].selected = true
                        mPlanAdapter.setData(list, true)
                        mPlanDetailAdapter.setData(list[0].list, true)
                    }
                }
            })
        }
    }

    private fun addListData() {
        mPlanAdapter = SubscriptionPlanAdapter(mContext, null).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    val list = getData()
                    if (pos > list.size - 1) return
                    val dataBean = list[pos]
                    if (dataBean.selected) return
                    for (i in 0 until list.size) {
                        val data = list[i]
                        if (data.selected) {
                            data.selected = false
                            notifyItemChanged(i)
                            break
                        }
                    }
                    dataBean.selected = true
                    mSelectPlanIndex = pos
                    notifyItemChanged(pos)

                    mPlanDetailAdapter.setData(dataBean.list, true)
                }
            })
        }

        mPlanDetailAdapter = SubscriptionPlanDetailAdapter(mContext, null)
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnSelectSubscribePlanContinue -> Intent(
                    mContext, ChoosePlanActivity::class.java
                ).let {
                    if (mPlanAdapter.getData().isNotEmpty()) {
                        it.putExtra(
                            ConstantString.Title, mPlanAdapter.getData()[mSelectPlanIndex].name
                        )
                        it.putExtra(ConstantString.LkSetMeal, mOrderBean)
                        it.putExtra(ConstantString.Type, mType)
                        it.putExtra(ConstantString.RechargeType, mRechargeType)
                        startActivity(it)
                    }
                }
            }
        }
    }
}