package com.remax.visualnovel.ui.wallet.transaction

/**
 * Created by HJW on 2025/9/12
 */

import com.chad.library.adapter.base.module.LoadMoreModule
import com.remax.visualnovel.R
import com.remax.visualnovel.app.BaseBindingQuickAdapter
import com.remax.visualnovel.app.widget.setMyEmptyView
import com.remax.visualnovel.databinding.ItemIncomeBinding
import com.remax.visualnovel.entity.request.PageQuery
import com.remax.visualnovel.entity.response.AccountBuffBill
import com.remax.visualnovel.entity.response.TransactionGift
import com.remax.visualnovel.extension.convertFromJson
import com.remax.visualnovel.extension.formatPrice
import com.remax.visualnovel.extension.glide.load
import com.remax.visualnovel.manager.gift.GiftManager
import com.remax.visualnovel.utils.TimeUtils
import com.remax.visualnovel.widget.uitoken.changeTextColor

/**
 * 这是一个适配器，LoadMoreModule按需取舍
 */
class TransactionAdapter(private val type: String? = null) :
    BaseBindingQuickAdapter<AccountBuffBill, ItemIncomeBinding>(ItemIncomeBinding::inflate),
    LoadMoreModule {

    override fun convert(holder: BaseBindingHolder, item: AccountBuffBill) {
        with(holder.getViewBinding<ItemIncomeBinding>()) {
            timeIncome.text = TimeUtils.format_y_m_d_h_m_s(item.time)
            titleIncome.text = item.item
            ivIncome.setImageResource(R.mipmap.ic_transaction)
            GiftManager.gifts?.find { it.id == item.extend.convertFromJson<TransactionGift>()?.giftId }?.let { gift ->
                ivIncome.load(gift.icon)
            }

            when {
                type == AccountBuffBill.INCOME -> {
                    priceIncome.setPrice("+${formatPrice(item.amount)}")
                }

                item.isIn -> {
                    priceIncome.getContentView()?.changeTextColor {
                        textUIColorToken = context.getString(R.string.color_positive_normal)
                    }
                    priceIncome.setPrice("+${formatPrice(item.amount)}")
                }

                else -> {
                    priceIncome.getContentView()?.changeTextColor {
                        textUIColorToken = context.getString(R.string.color_important_variant_normal)
                    }
                    priceIncome.setPrice("-${formatPrice(item.amount)}")
                }
            }
        }
    }

    fun setTransactionData(isRefresh: Boolean, data: List<AccountBuffBill>, topMargin: Int) {
        if (isRefresh) {
            setList(data)
            setMyEmptyView(R.string.no_transaction_yet, topMargin = topMargin)
        } else {
            addData(data)
            loadMoreModule.loadMoreComplete()
        }
        if (data.size < PageQuery.DEFAULT_PAGE_SIZE) {
            loadMoreModule.loadMoreEnd()
        }
    }

}