package com.adealink.weparty.wallet.recharge

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.module.wallet.Wallet
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.wallet.databinding.ActivityRechargeBinding
import com.adealink.weparty.wallet.pay.payManager
import com.adealink.weparty.wallet.recharge.fragment.BeanExchangedFragment
import com.adealink.weparty.wallet.recharge.fragment.CoinRechargeFragment
import com.adealink.weparty.wallet.recharge.fragment.DiamondRechargeFragment
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Wallet.Recharge.PATH],
    desc = "钱包充值"
)
class RechargeActivity : BaseActivity() {

    @BindExtra(Wallet.Common.EXTRA_CURRENCY)
    var currency: Int? = null

    private val binding by viewBinding(ActivityRechargeBinding::inflate)

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.setPadding(0, top, 0, 0)
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                height = top + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
            }
        }

        binding.btnBack.onClick {
            finish()
        }
        when (Currency.map(currency)) {
            Currency.COIN -> {
                supportFragmentManager.beginTransaction()
                    .replace(binding.flContent.id, CoinRechargeFragment(), "CoinRechargeFragment")
                    .commitAllowingStateLoss()
            }

            Currency.DIAMOND -> {
                supportFragmentManager.beginTransaction()
                    .replace(
                        binding.flContent.id,
                        DiamondRechargeFragment(),
                        "DiamondRechargeFragment"
                    )
                    .commitAllowingStateLoss()
            }

            Currency.BEAN -> {
                supportFragmentManager.beginTransaction()
                    .replace(binding.flContent.id, BeanExchangedFragment(), "BeanExchangedFragment")
                    .commitAllowingStateLoss()
            }

            null -> {
                supportFragmentManager.beginTransaction()
                    .replace(binding.flContent.id, CoinRechargeFragment(), "CoinRechargeFragment")
                    .commitAllowingStateLoss()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        payManager.queryAndHandleUnDealPurchases()
    }

}