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

import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemItemsupplierRowBinding
import es.verdnatura.presentation.common.*
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 OnGeneralItemRowClickListener: OnGeneralItemRowClickListener,
    //  private var showDelete: Boolean = false
) : 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

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

                itemSubquantity.setOnEditorActionListener { v, actionId, event ->

                    if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                        onEditSubQuantityListener.OnEditSubQuantityListener(
                            item,
                            binding.itemSubquantity.text.toString()
                        )
                        v.clearFocus()
                    }
                    return@setOnEditorActionListener false
                }
            }
            }

        }

    }
