package com.amz.genie.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.amz.genie.R

class SubjectAdapter(
    private val items: ArrayList<String> = arrayListOf()
) : RecyclerView.Adapter<SubjectAdapter.VH>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_inbox_detail_penerima, parent, false)
        return VH(v)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.bind(items[position], position)
    }

    override fun getItemCount(): Int = items.size

    class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tv: TextView = itemView.findViewById(R.id.tv_penerima_item)

        fun bind(item: String, position: Int) {
            tv.text = item

            val isGreen = position % 2 == 0

            tv.setBackgroundResource(
                if (isGreen) R.drawable.bg_chip_penerima else R.drawable.bg_chip_penerima_gray
            )

            tv.setTextColor(
                ContextCompat.getColor(
                    itemView.context,
                    if (isGreen) R.color.colorBlack else R.color.colorWhitePure
                )
            )
        }
    }

    fun submitList(newItems: List<String>) {
        items.clear()
        items.addAll(newItems)
        notifyDataSetChanged()
    }
}