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

import android.graphics.drawable.Drawable
import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.databinding.ItemToolbarRowBinding
import es.verdnatura.presentation.common.OnOptionsSelectedListener

class ToolBarAdapter (
    private val items: List<Drawable>,
    private val onOptionsSelectedListener: OnOptionsSelectedListener
): RecyclerView.Adapter<ToolBarAdapter.ItemHolder> () {




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

    override fun getItemCount() =items.size

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.bind(items[position])


    }

    inner class ItemHolder(
        val binding: ItemToolbarRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        private val res = binding.root.context.resources
        @RequiresApi(Build.VERSION_CODES.O)
        fun bind(item: Drawable) {
            binding.apply {
                imagebuttonIcon.setImageDrawable(item)
                imagebuttonIcon.setOnClickListener {
                   onOptionsSelectedListener.onOptionsItemSelected(item)
               }

            }
        }
    }
}



