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

import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.ViewPagerAdapter
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.databinding.ActivityPaymentMethodBinding
import com.abbidot.tracker.ui.fragment.subscribe.CreditCardPaymentFragment
import com.abbidot.tracker.ui.fragment.subscribe.PaypalPaymentFragment
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

class PaymentMethodActivity :
    BaseActivity<ActivityPaymentMethodBinding>(ActivityPaymentMethodBinding::inflate) {

    var mOrderBean: SubscriptionsOrderBean? = null

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

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

    override fun getTopBar() = mViewBinding.ilPaymentMethodTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_payment_method)
        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)
            mRechargeType = getInt(ConstantString.RechargeType, ConstantInt.Type1)
        }

        mFragments = mutableListOf(
            CreditCardPaymentFragment.newInstance(mContext),
            PaypalPaymentFragment.newInstance(mContext)
        )

        mViewBinding.apply {
            rgSwitchPaymentMethod.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    rbPaymentMethodCreditCard.id -> vp2SwitchPaymentMethod.currentItem = 0
                    rbPaymentMethodPaypal.id -> vp2SwitchPaymentMethod.currentItem = 1
                }
            }

            vp2SwitchPaymentMethod.let {
                it.adapter = ViewPagerAdapter(mFragments, this@PaymentMethodActivity)
                ViewUtil.instance.setViewPager2Config(
                    it, object : ViewPager2.OnPageChangeCallback() {
                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            when (position) {
                                0 -> rgSwitchPaymentMethod.check(rbPaymentMethodCreditCard.id)
                                1 -> rgSwitchPaymentMethod.check(rbPaymentMethodPaypal.id)
                            }
                        }
                    })
            }
        }
    }

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

}