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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemItemsupplierRowBinding
import es.verdnatura.presentation.common.OnEditSubQuantityListener
import es.verdnatura.presentation.common.OnImageTrashClickListener
import es.verdnatura.presentation.common.OnImageUpdateClickListener
import es.verdnatura.presentation.common.OnItemClickListener
import es.verdnatura.presentation.view.feature.packaging.model.ItemSupplier

class ItemSupplierAdapter(
    private val items: List<ItemSupplier>,
    private val onImageUpdateClickListener: OnImageUpdateClickListener?,
    private val onImageTrashClickListener: OnImageTrashClickListener?,
    private val onEditSubQuantityListener: OnEditSubQuantityListener?,
    private val onItemClickListener: OnItemClickListener? = null,
    private var showItems: Boolean = true
) : RecyclerView.Adapter<ItemSupplierAdapter.ItemHolder>() {

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

    override fun getItemCount() = items.size

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

        holder.binding.root.setOnClickListener {
        }

    }

    inner class ItemHolder(
        val binding: ItemItemsupplierRowBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ItemSupplier) {
            binding.apply {
                this.item = item

                if (!showItems) {
                    itemAdd.visibility = View.GONE
                    itemRemove.visibility = View.GONE
                }
                itemAdd.setOnClickListener {
                    onImageUpdateClickListener!!.onImageUpdateClickListener(
                        item
                    )
                }
                itemRemove.setOnClickListener {
                    onImageTrashClickListener!!.onImageTrashClickListener(item)
                }
                itemTitle.setOnClickListener {
                    onItemClickListener?.onItemClickListener(item)
                }

            }
        }

    }

}
