package com.remax.visualnovel.ui.wallet.dialog

import android.annotation.SuppressLint
import android.app.Activity
import androidx.recyclerview.widget.RecyclerView
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.DialogRechargeBinding
import com.remax.visualnovel.databinding.ItemRechargeBinding
import com.remax.visualnovel.entity.request.ChargeProduct
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.ui.wallet.manager.WalletManager
import com.remax.visualnovel.widget.dialoglib.LBindingDialog
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.setup

/**
 * Created by HJW on 2025/9/15
 */
class RechargeDialog(val activity: Activity) :
    LBindingDialog<DialogRechargeBinding>(activity, DialogRechargeBinding::inflate) {

    @SuppressLint("SetTextI18n")
    fun build(productList: List<ChargeProduct>) {
        with()
        setBottom()
        setCancelBtn(R.id.cancelBtn)
        with(binding) {
            balance.setPrice(formatPrice(WalletManager.balance))
            rvRecharge.setRechargeProduct(productList) { item ->
                btnRecharge.text = "${activity.getString(R.string.recharge)} ${item.local}"
            }
            setOnClick(btnRecharge) {
                productList.find { it.selected }?.let { product ->
                    WalletManager.onCreateOrder(product)
                    dismiss()
                }
            }
        }
        show()
    }
}

@SuppressLint("NotifyDataSetChanged")
fun RecyclerView.setRechargeProduct(productList: List<ChargeProduct>?, selectCallback: (ChargeProduct) -> Unit) {
    grid(2)
        .divider {
            setDivider(16, true)
            orientation = DividerOrientation.VERTICAL
        }.setup {
            addType<ChargeProduct>(R.layout.item_recharge)
            onClick(R.id.group) {
                val item = getModel<ChargeProduct>()
                models?.forEach { product ->
                    (product as? ChargeProduct)?.apply {
                        selected = item == product
                        if (selected) {
                            selectCallback.invoke(item)
                        }
                    }
                }
                bindingAdapter?.notifyDataSetChanged()
            }
            onBind {
                val item = getModel<ChargeProduct>()
                with(getBinding<ItemRechargeBinding>()) {
                    group.changeBackground {
                        strokeUIColorToken = context.getString(
                            if (item.selected)
                                R.string.color_primary_normal
                            else
                                R.string.color_surface_base_normal
                        )
                    }
                    priceView.setPrice(item.chargeAmount.toString())
                    amountView.text = item.local
                }
            }
        }.models = productList
}
