package com.gamedog.vididin.features.withdrawrecord

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.ama.core.architecture.util.ResUtil
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.beans.RECORD_CASH_PLUS_GOLD_CONVERT
import com.gamedog.vididin.beans.RecordCashShow
import com.viddin.videos.free.R
import java.text.SimpleDateFormat
import com.viddin.videos.free.databinding.FragmentWithdrawRecordCashItemBinding as ViewBinding

class RecordCashRvAdapter : ListAdapter<RecordCashShow, RecordCashRvAdapter.ViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(private val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(data: RecordCashShow) {
            binding.tvDate.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data.dateMs)
            binding.tvTitle.text = ResUtil.getString(data.title)
            binding.ivType.setImageResource(data.iconRes)

            binding.tvAmount.text = (if (data.amountNum < 0) "-" else "+") + ResUtil.getString(R.string.cash) + " " + String.format("%.2f", Math.abs(data.amountNum))
            binding.tvAmount.setTextColor(ResUtil.getColor(data.textColor))

            if (data.recordType == RECORD_CASH_PLUS_GOLD_CONVERT) {
                binding.tvDescription.text = String.format(ResUtil.getString(data.description), Math.abs(data.amountNum *10 * VidiConst.PER_01CASH_COST_GOLD_NUM).toInt())
            } else {
                binding.tvDescription.text = ResUtil.getString(data.description)
            }
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<RecordCashShow>() {
        override fun areItemsTheSame(oldItem: RecordCashShow, newItem: RecordCashShow): Boolean {
            return oldItem.uuid == newItem.uuid
        }

        override fun areContentsTheSame(oldItem: RecordCashShow, newItem: RecordCashShow): Boolean {
            return oldItem.uuid == newItem.uuid
        }
    }
}