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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemInventaryRowBinding
import es.verdnatura.presentation.common.OnInvetoryNichoClickListener
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.inventario.model.ItemInventaryVO
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

class InventoryAdapter  (
    private val items: List<ItemInventaryVO>,
    private val onInvetoryNichoClickListener: OnInvetoryNichoClickListener,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener
): RecyclerView.Adapter<InventoryAdapter.ItemHolder> () {


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

    override fun getItemCount() =items.size

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

    inner class ItemHolder(
        val binding: ItemInventaryRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        fun bind(item: ItemInventaryVO) {
            binding.apply {
                this.item = item
                if(item.producer.isNullOrEmpty()){
                    itemProducer.visibility = View.GONE
                }else{
                    itemProducer.visibility = View.VISIBLE
                }
                itemNicho.setOnClickListener {
                    onInvetoryNichoClickListener.onInvetoryNichoClickListener(item)
                }
                itemFk.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(PasillerosItemVO(title = res.getString(
                        R.string.titleItemConsult)),item.itemFk)
                }
                itemUp.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(PasillerosItemVO(title = res.getString(
                        R.string.titleItemSearch)),item.itemFk)
                }
                itemDown.setOnClickListener {
                    onPasillerosItemClickListener.onPasillerosItemClickListener(PasillerosItemVO(title = res.getString(
                        R.string.titleItemSearch)),item.itemFk)
                }
            }
        }
    }
}