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.beans.RECORD_GOLD_MINUS_CONVERT_2_CASH
import com.gamedog.vididin.beans.RecordGoldShow
import java.text.SimpleDateFormat
import com.viddin.videos.free.databinding.FragmentWithdrawRecordGoldItemBinding as ViewBinding

class RecordGoldRvAdapter : ListAdapter<RecordGoldShow, RecordGoldRvAdapter.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: RecordGoldShow) {
            binding.tvDate.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data.dateMs)
            binding.tvTitle.text = ResUtil.getString(data.title)
            binding.tvAmount.text = (if(data.amountNum > 0) "+" else "") + data.amountNum.toString()
            binding.ivType.setImageResource(data.iconRes)
            if (data.recordType == RECORD_GOLD_MINUS_CONVERT_2_CASH) {
                binding.tvDescription.text = String.format(ResUtil.getString(data.description), Math.abs(data.amountNum))
            } else {
                binding.tvDescription.text = ResUtil.getString(data.description)
            }
        }
    }

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

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