package com.example.orderly.ui.catalog

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.orderly.R
import com.example.orderly.data.entity.Product
import com.example.orderly.databinding.ItemFeaturedBinding

class FeaturedAdapter(private val items: List<Product>) :
    RecyclerView.Adapter<FeaturedAdapter.FeaturedViewHolder>() {

    class FeaturedViewHolder(private val binding: ItemFeaturedBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(product: Product) {
            binding.tvFeaturedTitle.text = product.name
            binding.tvFeaturedDesc.text = product.description

            Glide.with(binding.ivFeatured.context)
                .load(product.imageUrl)
                .placeholder(R.drawable.ic_header)
                .error(R.drawable.ic_header)
                .into(binding.ivFeatured)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeaturedViewHolder {
        val binding = ItemFeaturedBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return FeaturedViewHolder(binding)
    }

    override fun onBindViewHolder(holder: FeaturedViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount(): Int = items.size
}
