package com.remax.visualnovel.ui.wallet.recharge

/**
 * Created by HJW on 2025/9/12
 */
import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.remax.visualnovel.R
import com.remax.visualnovel.app.base.BaseBindingFragment
import com.remax.visualnovel.databinding.FragmentRechargeBinding
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.launchAndCollect
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.manager.pay.GooglePayManager
import com.remax.visualnovel.ui.wallet.WalletActivity
import com.remax.visualnovel.ui.wallet.WalletViewModel
import com.remax.visualnovel.ui.wallet.dialog.setRechargeProduct
import com.remax.visualnovel.ui.wallet.transaction.TransactionDetailActivity
import com.remax.visualnovel.utils.Routers
import com.pengxr.modular.eventbus.generated.events.EventDefineOfWalletEvents
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Routers.RECHARGE)
class RechargeFragment : BaseBindingFragment<FragmentRechargeBinding>() {

    private val walletViewModel by activityViewModels<WalletViewModel>()

    @SuppressLint("SetTextI18n")
    override fun onCreated(bundle: Bundle?) {
        with(binding) {
            transactionEnter.text = "${getString(R.string.transaction_detail)}>"
            setOnClick(transactionEnter) {
                when (this) {
                    transactionEnter -> {
                        TransactionDetailActivity.start()
                    }
                }
            }
        }
        getProducts()
    }

    @SuppressLint("SetTextI18n")
    private fun getProducts() {
        launchAndCollect({
            walletViewModel.getProducts()
        }) {
            onSuccess = {
                it?.productList?.forEachIndexed { index, chargeProduct ->
                    GooglePayManager.productDetails.forEach { productDetails ->
                        if (chargeProduct.productId == productDetails?.productId) {
                            chargeProduct.local = productDetails.oneTimePurchaseOfferDetails?.formattedPrice
                            chargeProduct.localCurrencyCode =
                                productDetails.oneTimePurchaseOfferDetails?.priceCurrencyCode ?: "USD"
                        }
                    }
                }
                (activity as? WalletActivity)?.refreshRechargeTxt()
                binding.rvRecharge.setRechargeProduct(it?.productList) { item ->
                    (activity as? WalletActivity)?.refreshRechargeTxt()
                }
            }
        }
    }

    override fun subscribeUi() {
        EventDefineOfWalletEvents.buffBalanceUpdateSucceeded().observe(this) {
            binding.balance.setPrice(formatPrice(it?.balance))
        }
    }

    companion object {
        fun newInstance(): RechargeFragment {
            return ARouter.getInstance().build(Routers.RECHARGE)
                .navigation() as RechargeFragment
        }
    }

}