package com.remax.visualnovel.ui.chat.message.detail.flirting

import android.content.Context
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.DialogFlirtingRetrieveBinding
import com.remax.visualnovel.entity.request.HeartbeatRelation
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.getTemperatureTxt
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.ui.wallet.manager.WalletManager
import com.remax.visualnovel.widget.dialoglib.LBindingDialog

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

class FlirtingRetrieveDialog(context: Context) :
    LBindingDialog<DialogFlirtingRetrieveBinding>(context, DialogFlirtingRetrieveBinding::inflate) {

    fun build(relation: HeartbeatRelation, buyCallback: () -> Unit): FlirtingRetrieveDialog {
        with()
        setBottom()
        setBgColorToken(R.string.color_transparent)
        setCancelBtn(R.id.cancel)
        with(binding) {
            quantity.text = relation.subtractHeartbeatVal?.getTemperatureTxt()
            priceView.setPrice(formatPrice(relation.price))
            val total = maxOf(1.0, (relation.subtractHeartbeatVal ?: 0.0) * (relation.price ?: 0L))
            relationPrice.setPrice(formatPrice(total.toLong()))

            setOnClick(purchaseBtn) {
                if (WalletManager.balance < total) {
                    WalletManager.showChargeDialog()
                } else {
                    buyCallback.invoke()
                }
            }
        }
        return this
    }

}
