package com.casic.br.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.casic.br.R
import com.casic.br.model.RecommendModel

class RecommendAdapter(
    private val context: Context,
    private val dataRows: MutableList<RecommendModel>
) : RecyclerView.Adapter<RecommendAdapter.ItemViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder(
        layoutInflater.inflate(R.layout.item_recommend_rv_l, parent, false)
    )

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        Glide.with(context).load(dataRows[position].imageLink).into(holder.recommendImageView)
        holder.itemView.setOnClickListener {
            clickListener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int = dataRows.size

    private var clickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(clickListener: OnItemClickListener?) {
        this.clickListener = clickListener
    }

    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val recommendImageView: ImageView = itemView.findViewById(R.id.recommendImageView)
    }
}