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

import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isGone
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.HelpTextImageTypeAdapter
import com.abbidot.tracker.adapter.SubscriptionHistoryAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.MenuTxtBean
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
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo

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

    private val mSubscriptionViewModel: SubscriptionManageViewModel by viewModels()

    private lateinit var mHistoryAdapter: SubscriptionHistoryAdapter
    private var mSubscriptionsOrderList = mutableListOf<SubscriptionsOrderBean>()
    private var subscriptionId = ""
    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)
        intent?.extras?.apply {
            subscriptionId = getString(ConstantString.Id, "")
        }
        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvSubscriptionHistoryList, mHistoryAdapter, bottom = AppUtils.dpToPx(12)
            )
            setOnClickListenerViews(btnSubscriptionRefundIssue, tvCloseRefundIssueBtn)
        }

        initIssue()
    }

    private fun initIssue() {
        val menuList = mutableListOf<MenuTxtBean>()
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_refund_policy),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            isSwitch = true,
            state = 16
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_automatic_refund),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            isSwitch = true,
            state = 12
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_automatic_refund_dec),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            state = 12
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_standard_refund),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            isSwitch = true,
            state = 12
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_standard_refund_dec),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            state = 12
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_no_refund),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            isSwitch = true,
            state = 12
        )
        ViewUtil.instance.addMenuBean(
            menuList,
            getString(R.string.txt_no_refund_dec),
            menuType = MultipleEntity.TEXT_TEXT_IMG,
            colorRedId = R.color.data_black_color,
            state = 12
        )
        val helpBatteryAdapter = HelpTextImageTypeAdapter(menuList)
        ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
            mContext, mViewBinding.rvSubscriptionRefundIssueList, helpBatteryAdapter
        )
    }

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

    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 = l.copy(menuType = MultipleEntity.IMG)
                                mSubscriptionsOrderList.add(subscriptionsOrder)
                            }
                        }
                        mHistoryAdapter.notifyItemRangeChanged(0, mSubscriptionsOrderList.size)
                    }
                }
            })
        }
    }

    /**
     * 显示问题
     */
    private fun showLedIssue() {
        mViewBinding.apply {
            if (llSubscriptionRefundIssueLayout.isGone) {
                YoYo.with(Techniques.BounceInUp).duration(700).onStart {
                    llSubscriptionRefundIssueLayout.visibility = View.VISIBLE
                    vRefundIssueShadeView.visibility = View.VISIBLE
                }.playOn(llSubscriptionRefundIssueLayout)
            } else {
                vRefundIssueShadeView.visibility = View.GONE
                YoYo.with(Techniques.SlideOutDown).duration(600).onEnd {
                    llSubscriptionRefundIssueLayout.visibility = View.GONE
                }.playOn(llSubscriptionRefundIssueLayout)
            }
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnSubscriptionRefundIssue, tvCloseRefundIssueBtn -> showLedIssue()
            }
        }
    }
}