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.MySubscriptionAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityMySubscriptionBinding
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.SubscriptionManageViewModel

class MySubscriptionActivity :
    BaseActivity<ActivityMySubscriptionBinding>(ActivityMySubscriptionBinding::inflate) {

    private val mSubscriptionViewModel: SubscriptionManageViewModel by viewModels()

    private lateinit var mSubscriptionAdapter: MySubscriptionAdapter
    private var mPosition = 0
    private var mRefreshPackage = true

    override fun getTopBar() = mViewBinding.ilMySubscriptionTopBar.titleTopBar

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

        mViewBinding.apply {
            mSubscriptionAdapter = MySubscriptionAdapter(mContext, null)
//                .apply {
//                setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
//                    override fun onItemClick(itemView: View?, pos: Int) {
//                        Intent(mContext, SubscriptionDetailActivity::class.java).let {
//                            it.putExtra(ConstantString.SetMeal, getData()[pos])
//                            startActivity(it)
//                        }
//                    }
//                })
//            }
            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvMySubscriptionList, mSubscriptionAdapter, top = AppUtils.dpToPx(10)
            )
        }
    }

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

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshPackage) {
            mRefreshPackage = true
        }
        mSubscriptionViewModel.apply {
            mSubscriptionsOrderLiveData.observe(this@MySubscriptionActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.let { list ->
                            mSubscriptionAdapter.setData(list, true)
                        }
                    }
                })
            }

            mUpdateStatusListLiveData.observe(this@MySubscriptionActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        showToast(R.string.txt_successful)
                        mSubscriptionAdapter.getData()[mPosition].let { bean ->
                            bean.subscriptionStatus =
                                if (bean.subscriptionStatus == ConstantInt.Close) ConstantInt.Open
                                else ConstantInt.Close
                            mSubscriptionAdapter.notifyItemChanged(mPosition)
                            mViewBinding.rvMySubscriptionList.smoothScrollToPosition(mPosition)
                        }
                    }
                })
            }
        }
    }

    fun updateSubscriptionStatus(subscriptionId: String, status: Int, position: Int) {
        mPosition = position
        val messageId = if (0 == status) R.string.txt_auto_recharge_title
        else R.string.txt_auto_recharge_content
        ViewUtil.instance.operateDialogTips(mContext, messageId) {
            mSubscriptionViewModel.updateSubscriptionStatus(subscriptionId, status)
        }
    }
}