package com.example.pantallacompra

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide


class MyAdapter (private val eventList : ArrayList<Event>, private val context: Context ) : RecyclerView.Adapter<MyAdapter.MyViewHolder>(){

    private lateinit var mListener : onItemClickListener


    interface onItemClickListener{
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: onItemClickListener){
        mListener=listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView= LayoutInflater.from(parent.context).inflate(R.layout.event_item,parent,false)
        return MyViewHolder(itemView, mListener)
    }

    override fun getItemCount(): Int {
        return eventList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val list= eventList[position]
        val imageUrl=eventList[position].urlImage
        holder.nombre.text="Evento:" +list.nombre
        holder.tipo.text="Tipo: "+list.tipo
        holder.duracion.text="Duración: "+list.duracion.toString()+" min"
        Glide.with(context).load(imageUrl).into(holder.imagen)
    }

    class MyViewHolder(itemView : View,  listener: onItemClickListener): RecyclerView.ViewHolder(itemView){
        val nombre : TextView = itemView.findViewById(R.id.eventName)
        val tipo: TextView = itemView.findViewById(R.id.eventType)
        val duracion: TextView =itemView.findViewById(R.id.eventDuration)
        val imagen: ImageView =itemView.findViewById(R.id.imageEvent)

        init {
            itemView.setOnClickListener{
                listener.onItemClick(adapterPosition)
            }
        }

    }
}