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

class ToolBarAdapterTooltip (

        private val items: List<ImageView>,
        private val onOptionsSelectedListener: OnOptionsSelectedListener
    ) : RecyclerView.Adapter<ToolBarAdapterTooltip.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


            fun bind(item: ImageView) {
                binding.apply {
                    imagebuttonIcon.setImageDrawable(item.drawable)

                    imagebuttonIcon.setOnClickListener {
                        onOptionsSelectedListener.onOptionsItemSelected(item.drawable)
                    }

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        imagebuttonIcon.tooltipText=item.tooltipText
                    }

                }
            }
        }
    }




