package com.remax.visualnovel.ui.chat.setting.model

import android.app.Activity
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.DialogChatModelBinding
import com.remax.visualnovel.databinding.ItemChatModelBinding
import com.remax.visualnovel.entity.response.ChatModel
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.showMoreTxtDialog
import com.remax.visualnovel.ui.wallet.WalletActivity
import com.remax.visualnovel.ui.wallet.manager.WalletManager
import com.remax.visualnovel.widget.dialoglib.LBindingDialog
import com.drake.brv.utils.divider
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

/**
 * Created by HJW on 2025/8/18
 */
class ChatModelDialog(private val activity: Activity) :
    LBindingDialog<DialogChatModelBinding>(activity, DialogChatModelBinding::inflate) {

    private var selectCode: String? = null

    fun build(
        models: List<ChatModel>,
        selectCode: String?,
        recharge: (() -> Unit)? = null,
        click: (ChatModel) -> Unit
    ) {
        this.selectCode = selectCode
        with()
        setBottom()
        setCancelBtn(R.id.cancel)
        with(binding) {
            dialogTitle.setText(if (recharge == null) R.string.dialog_model else R.string.recharge_dialog_title)
            modelHint.isVisible = recharge == null
            listOf(btnRecharge, dialogText, priceView).forEach {
                it.isVisible = !modelHint.isVisible
            }

            setOnClick(btnRecharge) {
                WalletActivity.start()
                dismiss()
            }
            priceView.setPrice(formatPrice(WalletManager.balance))

            rvModel.linear()
                .divider {
                    setDivider(16, true)
                }.setup {
                    addType<ChatModel>(R.layout.item_chat_model)
                    onClick(R.id.group) {
                        val item = getModel<ChatModel>()
                        click.invoke(item)
                    }
                    onBind {
                        val item = getModel<ChatModel>()
                        with(getBinding<ItemChatModelBinding>()) {
                            modelSelect.isVisible = item.code == selectCode
                            modelTitle.text = item.name
                            modelDesc.text = item.description
                            priceView1.setPrice("${formatPrice(item.textPrice)}/${context.getString(R.string.text_message)}")
                            priceView2.setPrice("${formatPrice(item.voicePrice)}/${context.getString(R.string.send_or_play_voice)}")
                            priceView3.setPrice("${formatPrice(item.voiceChatPrice)}/${context.getString(R.string.voice_call_message)}")

                            setOnClick(priceTips) {
                                activity.showMoreTxtDialog(
                                    texts = listOf(
                                        item.questionMark ?: ""
                                    )
                                )
                            }
                        }
                    }
                }.models = models
        }
    }


//    inner class ChatModelAdapter(private val activity: Activity, data: List<ChatModel>) :
//        BannerAdapter<ChatModel, ChatModelAdapter.ViewHolder>(data) {
//
//        inner class ViewHolder(view: View, itemBinding: ItemChatModelBinding) : RecyclerView.ViewHolder(view) {
//            val binding = itemBinding
//        }
//
//        override fun onCreateHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//            val binding = ItemChatModelBinding.inflate(LayoutInflater.from(parent.context), parent, false)
//            return ViewHolder(binding.root, binding)
//        }
//
//        override fun onBindView(holder: ViewHolder?, data: ChatModel?, position: Int, size: Int) {
//            data?.let { item ->
//                holder?.binding?.run {
//                    modelSelect.isVisible = item.code == selectCode
//                    modelTitle.text = item.name
//                    modelDesc.text = item.description
//                    priceView1.setPrice("${item.textPrice}/${context.getString(R.string.text_message)}")
//                    priceView2.setPrice("${item.voiceChatPrice}/${context.getString(R.string.voice_call_message)}")
//
//                    setOnClick(priceTips) {
//                        activity.showMoreTxtDialog(
//                            texts = listOf(
//                                item.questionMark ?: ""
//                            )
//                        )
//                    }
//                }
//            }
//        }
//    }
}
