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

import android.content.Context
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.BR
import es.verdnatura.R
import es.verdnatura.databinding.ItemMainMenuBinding
import es.verdnatura.databinding.ItemPasillerosMainMenuBinding
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import timber.log.Timber.d

class PasillerosAdapter (
    private val items: List<PasillerosItemVO>,
    private val onPasillerosItemClickListener: OnPasillerosItemClickListener,
    private val isMenuInit:Boolean

): RecyclerView.Adapter<PasillerosAdapter.PasillerosItemHolder> () {

    var context : Context? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PasillerosItemHolder {
        this.context = parent.context


            val binding: ViewDataBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                viewType,
                parent,
                false)



        return if (isMenuInit){
            PasillerosItemHolder(binding as ItemMainMenuBinding)
        }else{
            PasillerosItemHolder(binding as ItemPasillerosMainMenuBinding)
        }

    }

    override fun getItemViewType(position: Int): Int {
        if (isMenuInit) return R.layout.item_main_menu
        else{
            return R.layout.item_pasilleros_main_menu
        }

      //  return super.getItemViewType(position)

    }

    override fun getItemCount() =items.size

    override fun onBindViewHolder(holder: PasillerosItemHolder, position: Int) {
        holder.bind(items[position])
        holder.binding.root.setOnClickListener {
            onPasillerosItemClickListener.onPasillerosItemClickListener(items[position],"")
        }

    }


    inner class PasillerosItemHolder(
        val binding: ViewDataBinding

    ) : RecyclerView.ViewHolder(binding.root){



        fun bind(item: PasillerosItemVO) {

            binding.setVariable(BR.item,item)
            binding.setVariable(BR.title,  context?.getString(item.titleToShow))


            if (isMenuInit)
            {

                try {
                    (binding as ItemMainMenuBinding).itemImage.setImageResource(item.iconResource)
                }catch(exception:Exception){
                   // d("Exception::"+exception.message)
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    (binding as ItemMainMenuBinding).itemImage.tooltipText=item.description
                }

            }
            else{

                try {
                    (binding as ItemPasillerosMainMenuBinding).itemImage.setImageResource(item.iconResource)
                }catch(exception:Exception){
                    //d("Exception::"+exception.message)
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    (binding as ItemPasillerosMainMenuBinding).itemImage.tooltipText=item.description
                }

            }

         /*   binding.apply {
                this.item = item
                this.title = context?.getString(item.titleToShow)

                try {
                    itemImage.setImageResource(item.iconResource)
                }catch(exception:Exception){
                   d("Exception::"+exception.message)
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    itemImage.tooltipText=item.description
                }
            }*/
        }
    }
}