package com.amz.genie.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.amz.genie.R
import com.amz.genie.helpers.Utils.isFemale
import com.amz.genie.models.Pegawai
import com.google.android.material.imageview.ShapeableImageView

class RecipientPickerAdapter(
    private val onPick: (Pegawai) -> Unit
) : RecyclerView.Adapter<RecipientPickerAdapter.VH>() {

    private val items = mutableListOf<Pegawai>()

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

    class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivAvatar: ShapeableImageView = itemView.findViewById(R.id.iv_avatar_recipient)
        val tvName: TextView = itemView.findViewById(R.id.tv_name_recipient)
        val btnRemove: View = itemView.findViewById(R.id.btn_remove_recipient)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_recipient, parent, false) // item XML yang kamu kasih
        return VH(v)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = items[position]
        holder.tvName.text = item.nama

        // di dialog, tombol remove tidak dipakai
        holder.btnRemove.visibility = View.GONE

        val female = isFemale(item.id_kelamin)
        holder.ivAvatar.setImageResource(if (female) R.drawable.ic_woman else R.drawable.ic_man)

        holder.itemView.setOnClickListener { onPick(item) }
    }

    override fun getItemCount(): Int = items.size
}