package es.verdnatura.presentation.view.feature.packaging.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.esafirm.imagepicker.model.Image
import es.verdnatura.databinding.ItemImageRowBinding
import es.verdnatura.presentation.common.OnImageTrashClickListener
import es.verdnatura.presentation.common.OnImageUpdateClickListener



class ListImageAdapter(
    private val items: List<Image>,
    private val onImageUpdateClickListener: OnImageUpdateClickListener,
    private val onImageTrashClickListener: OnImageTrashClickListener,

    ) : RecyclerView.Adapter<ListImageAdapter.ItemHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        return ItemHolder(
            ItemImageRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun getItemCount() = items.size


    inner class ItemHolder(
        val binding: ItemImageRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Image) {
            binding.apply {
                this.item = item
                itemImage.setImageURI(item.uri)
                itemName.text = item.name

                 itemImageUpdate.setOnClickListener {
                    onImageUpdateClickListener.OnImageUpdateClickListener(
                        item
                    )
                    itemImageTrash.setOnClickListener {
                        onImageTrashClickListener.OnImageUpdateClickListener(item)
                    }
                }
            }

        }

    }

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