package com.adealink.weparty.wallet.recharge.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.commonSelectedDrawable
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.wallet.databinding.LayoutRechargeItemBinding
import com.adealink.weparty.wallet.recharge.data.RechargeItemData
import com.adealink.weparty.wallet.recharge.data.RechargeProduct.Companion.INVALID_PRODUCT
import com.adealink.weparty.R as APP_R

class RechargeItemViewBinder(
    val currency: Currency,
    val onItemSelected: (item: RechargeItemData) -> Unit
) :
    ItemViewBinder<RechargeItemData, BindingViewHolder<LayoutRechargeItemBinding>>() {
    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): BindingViewHolder<LayoutRechargeItemBinding> {
        return BindingViewHolder(
            LayoutRechargeItemBinding.inflate(
                inflater,
                parent,
                false
            )
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutRechargeItemBinding>,
        item: RechargeItemData
    ) {
        holder.binding.root.onClick {
            onItemSelected.invoke(item)
        }
        if (item.data == INVALID_PRODUCT) {
            holder.binding.vBg.background = null
            holder.binding.ivCurrency.setImageResource(0)
            holder.binding.tvAmount.text = "--"
            holder.binding.tvSpend.text = "--"
            return
        }
        when (currency) {
            Currency.BEAN -> {
                holder.binding.ivCurrency.setImageResource(APP_R.drawable.common_wallet_bean_ic)
            }

            Currency.COIN -> {
                holder.binding.ivCurrency.setImageResource(APP_R.drawable.common_wallet_coin_ic)
            }

            Currency.DIAMOND -> {
                holder.binding.ivCurrency.setImageResource(APP_R.drawable.common_wallet_diamond_ic)
            }
        }

        if (item.selected) {
            holder.binding.vBg.background = selectedDrawable
        } else {
            holder.binding.vBg.background = null
        }
        holder.binding.tvAmount.text = item.data.amount.toString()
        holder.binding.tvSpend.text =
            "${item.data.priceCurrency} ${
                formatNumberStr(
                    item.data.price,
                    omitted = false,
                    group = true
                )
            }"

    }

    companion object {
        private val selectedDrawable = commonSelectedDrawable()
    }


}