package com.example.smartlab.Api

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Recycler
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.smartlab.Models.Catalog
import com.example.smartlab.databinding.ItemcatalogBinding

class AdapterCatalog(private var listener:Listener):RecyclerView.Adapter<AdapterCatalog.CatalogHolder>() {
    private val ListCatalog = ArrayList<Catalog>()
    class CatalogHolder(item: View): RecyclerView.ViewHolder(item){
        private val binding = ItemcatalogBinding.bind(item)
        fun bind(catalog: Catalog, listener: Listener) = with(binding){
            CatalogName.text = catalog.name
            CatalogCost.text = catalog.price.toString() + " Р"
            CatalogDays.text = catalog.timeResul
            ItemCatalog.setOnClickListener{
                listener.OnClick(catalog)
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatalogHolder {
        val view = LayoutInflater.from(parent.context).inflate(com.example.smartlab.R.layout.itemcatalog, parent, false)
        return CatalogHolder(view)
    }

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

    override fun onBindViewHolder(holder: CatalogHolder, position: Int) {
        holder.bind(ListCatalog[position], listener)
    }
    fun addCatalog(catalog: Catalog){
        ListCatalog.add(catalog)
        notifyDataSetChanged()
    }
    interface Listener{
        fun OnClick(catalog: Catalog)
    }
}