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

import android.content.Context
import android.content.res.Resources
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat.getColor
import androidx.recyclerview.widget.RecyclerView
import es.verdnatura.R
import es.verdnatura.databinding.ItemAjustesRowBinding
import es.verdnatura.presentation.common.OnAjustesItemClickListener
import es.verdnatura.presentation.view.feature.ajustes.model.AjustesItemVO
import java.security.AccessController.getContext

class AjustesAdapter (
    private val items: List<AjustesItemVO>,
    private val onAjustesItemClickListener: OnAjustesItemClickListener
): RecyclerView.Adapter<AjustesAdapter.AjustesItemHolder> () {

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

        return AjustesItemHolder(
            ItemAjustesRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        )

    }

    override fun getItemCount() =items.size

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


    inner class AjustesItemHolder(
        val binding: ItemAjustesRowBinding
    ) : RecyclerView.ViewHolder(binding.root){
        fun bind(item: AjustesItemVO) {
            binding.apply {

              this.item = item

                if (item.title==context!!.getString(R.string.closeSession)){
                binding.itemTitle.setTextColor(
                    ContextCompat.getColor(
                        context!!,
                        R.color.verdnatura_pumpkin_orange
                    )
                )
                }else{
                    binding.itemTitle.setTextColor(
                        ContextCompat.getColor(
                            context!!,
                            R.color.verdnatura_white))
                }

                if (!item.action){
                    binding.itemImage.visibility= View.INVISIBLE
                }
            }
        }
    }
}