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

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.SubscriptionHistoryAdapter
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.constant.MultipleEntity
import com.abbidot.tracker.databinding.ActivitySubscriptionHistoryBinding
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.SubscriptionManageViewModel

class SubscriptionHistoryActivity :
    BaseActivity<ActivitySubscriptionHistoryBinding>(ActivitySubscriptionHistoryBinding::inflate) {

    private val mSubscriptionViewModel: SubscriptionManageViewModel by viewModels()

    private lateinit var mHistoryAdapter: SubscriptionHistoryAdapter
    private var mSubscriptionsOrderList = mutableListOf<SubscriptionsOrderBean>()
    private var mDeviceId = ""
    private var mRefreshPackage = true

    override fun getTopBar() = mViewBinding.ilSubscriptionHistoryTopBar.titleTopBar

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

        mHistoryAdapter = SubscriptionHistoryAdapter(mSubscriptionsOrderList)
        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvSubscriptionHistoryList, mHistoryAdapter, bottom = AppUtils.dpToPx(12)
            )
        }

        intent?.extras?.apply {
            mDeviceId = getString(ConstantString.DeviceId, "")
        }
    }

    override fun onResume() {
        super.onResume()
        if (mRefreshPackage) {
            mRefreshPackage = false
            mSubscriptionViewModel.getSubscriptionsHistory(mDeviceId)
        }
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshPackage) {
            mRefreshPackage = true
        }
        mSubscriptionViewModel.mSubscriptionsHistoryLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.let { list ->
                        if (mSubscriptionsOrderList.size > 0) {
                            mHistoryAdapter.notifyItemRangeRemoved(0, mSubscriptionsOrderList.size)
                            mSubscriptionsOrderList.clear()
                        }
                        for (l in list) {
                            mSubscriptionsOrderList.add(l)
                            if (l.annualCareType == ConstantInt.Type1) {
                                val subscriptionsOrder = SubscriptionsOrderBean(MultipleEntity.IMG)
                                subscriptionsOrder.orderNum = l.orderNum
                                subscriptionsOrder.createTime = l.createTime
                                subscriptionsOrder.enabled = l.enabled
                                subscriptionsOrder.surplusDays = l.surplusDays
                                subscriptionsOrder.subscriptionStatus = l.subscriptionStatus
                                subscriptionsOrder.mealPeriod = l.mealPeriod
                                subscriptionsOrder.annualCareType = l.annualCareType
                                subscriptionsOrder.annualCarePrice = l.annualCarePrice
                                mSubscriptionsOrderList.add(subscriptionsOrder)
                            }
                        }
                        mHistoryAdapter.notifyItemRangeChanged(0, mSubscriptionsOrderList.size)
                    }
                }
            })
        }
    }
}