package com.abbidot.tracker.deprecated.ui.activity.profile.recharge.cn

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.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.RechargeDeviceBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityRechargeAmountCnBinding
import com.abbidot.tracker.deprecated.adapter.RechargeAmountCNAdapter
import com.abbidot.tracker.deprecated.ui.activity.vm.RechargeAmountViewModel
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.util.QMUIDisplayHelper

/**
 * 国内套餐列表
 */
class RechargeAmountCNActivity :
    BaseActivity<ActivityRechargeAmountCnBinding>(ActivityRechargeAmountCnBinding::inflate) {

    private val mRechargeAmountViewModel: RechargeAmountViewModel by viewModels()

    private lateinit var mRechargeAmountCNAdapter: RechargeAmountCNAdapter
    private var mRechargeDeviceBean: RechargeDeviceBean? = null

    override fun getTopBar() = mViewBinding.rechargeAmountCnBar.titleTopBar

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

        //充值设备数据
        mRechargeDeviceBean = Util.getParcelableAdaptive(
            intent,
            ConstantString.RechargeDevice,
            RechargeDeviceBean::class.java
        )

        mRechargeAmountCNAdapter = RechargeAmountCNAdapter(this, null)
        mRechargeAmountCNAdapter.setOnItemClickListener(object :
            BaseRecyclerAdapter.OnItemClickListener {
            override fun onItemClick(itemView: View?, pos: Int) {
                mRechargeAmountCNAdapter.setSelectIndex(pos)
            }
        })
        ViewUtil.instance.setRecyclerViewGridLayout(
            mContext!!,
            mViewBinding.rechargeAmountCnRecyclerView,
            mRechargeAmountCNAdapter,
            2,
            bottom = QMUIDisplayHelper.dpToPx(28),
        )

        setOnClickListenerViews(mViewBinding.btnRechargeAmountCnSubmitOrder)

        mRechargeAmountViewModel.getSetMealList(this)
    }

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

        mRechargeAmountViewModel.mSetMealListLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    val data = it.getOrNull()!!
                    mRechargeAmountCNAdapter.setData(data, true)
                }
            })
        }
    }

    /**
     * 提交订单
     */
    private fun submitOrder() {
        val index = mRechargeAmountCNAdapter.getSelectIndex()
        if (index == -1) {
            showToast(R.string.txt_pay_amount)
            return
        }
        val intent = Intent(mContext, RechargeOrderDetailCNActivity::class.java)
        intent.putExtra(ConstantString.RechargeDevice, mRechargeDeviceBean)
        intent.putExtra(ConstantString.SetMeal, mRechargeAmountCNAdapter.getData()[index])
        startActivity(intent)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.btnRechargeAmountCnSubmitOrder -> submitOrder()
        }
    }
}