package com.remax.visualnovel.ui.wallet

/**
 * Created by HJW on 2025/8/26
 */

import android.annotation.SuppressLint
import androidx.activity.viewModels
import androidx.core.view.isVisible
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.BaseBindingActivity
import com.remax.visualnovel.databinding.ActivityWalletBinding
import com.remax.visualnovel.extension.addScrollerAlpha
import com.remax.visualnovel.extension.launchAndLoadingCollect
import com.remax.visualnovel.extension.setMagicIndicator
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.ui.wallet.income.IncomeFragment
import com.remax.visualnovel.ui.wallet.manager.WalletManager
import com.remax.visualnovel.ui.wallet.recharge.RechargeFragment
import com.remax.visualnovel.utils.Routers
import com.remax.visualnovel.utils.spannablex.activateClick
import com.remax.visualnovel.utils.spannablex.span.SimpleClickableConfig
import com.remax.visualnovel.utils.spannablex.spannable
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.indicator.SecondaryNavigatorAdapter
import com.remax.visualnovel.widget.uitoken.expend.dsl.expandDp
import com.remax.visualnovel.widget.uitoken.handleUIToken
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue

@AndroidEntryPoint
@Route(path = Routers.WALLET)
class WalletActivity : BaseBindingActivity<ActivityWalletBinding>() {

    private val walletViewModel by viewModels<WalletViewModel>()

    override fun initView() {
        setToolbar(R.string.wallet)
        with(binding) {
            appBarLayout.addScrollerAlpha(tvTitle, isFromToolbar = true)

            setMagicIndicator(
                listOf(RechargeFragment.newInstance(), IncomeFragment.newInstance()),
                SecondaryNavigatorAdapter(
                    listOf(
                        R.string.recharge,
                        R.string.income,
                    ).map { res -> getString(res) }, viewPager2 = viewPager
                ),
                indicator, leftPadding = 24.dp
            ) {
                bottomLayout.isVisible = it == 0
            }

            agreement.activateClick(false).text = spannable {
                getString(R.string.recharge_agreement_hint).text()

                val color = handleUIToken(R.string.color_primary_variant_normal)?.color ?: 0
                " ${getString(R.string.recharge_agreement)}".span {
                    clickable(color, config = SimpleClickableConfig(false)) { _, _ ->
                        Routers.navigationToRA()
                    }
                }
            }

            radioRecharge.expandDp(30, 30)
            setOnClick(radioRecharge, btnRecharge) {
                when (this) {
                    radioRecharge -> {
                        radioRecharge.viewChecked(!radioRecharge.isChecked)
                        btnRecharge.isEnabled = radioRecharge.isChecked
                    }

                    btnRecharge -> {
                        walletViewModel.products?.find { it.selected }?.let {
                            WalletManager.onCreateOrder(it)
                        }
                    }
                }
            }
        }
    }

    override fun initData() {
        launchAndLoadingCollect({
            walletViewModel.getMyWallet()
        })
    }

    @SuppressLint("SetTextI18n")
    fun refreshRechargeTxt() {
        walletViewModel.products?.find { it.selected }?.let { item ->
            binding.btnRecharge.text = "${getString(R.string.recharge)} ${item.local}"
        }
    }

    companion object {
        fun start() {
            ARouter.getInstance()
                .build(Routers.WALLET)
                .navigation()
        }
    }

}