package com.amz.genie.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.amz.genie.R
import com.amz.genie.models.AttachmentItem

class AttachmentAdapter(
    private val onRemove: (AttachmentItem) -> Unit
) : RecyclerView.Adapter<AttachmentAdapter.VH>() {

    private val items = mutableListOf<AttachmentItem>()

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

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

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

    override fun getItemCount(): Int = items.size

    inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvName: TextView = itemView.findViewById(R.id.tv_name_attachment_chip)
        private val ibRemove: ImageButton = itemView.findViewById(R.id.ib_remove_attachment_chip)

        fun bind(item: AttachmentItem) {
            tvName.text = item.name
            ibRemove.setOnClickListener { onRemove(item) }
        }
    }
}