package com.example.smartlab.Adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.smartlab.Model.CatalogItem
import com.example.smartlab.Model.NewsItem
import com.example.smartlab.R
import com.example.smartlab.databinding.PartAnaliseBinding
import com.example.smartlab.databinding.PartNewsBinding

class AdapterCatalog(private val listener: Listener) : RecyclerView.Adapter<AdapterCatalog.CatalogHolder>() {

    private val catalogList = ArrayList<CatalogItem>()

    class CatalogHolder(item: View) : RecyclerView.ViewHolder(item) {
        private val binding = PartAnaliseBinding.bind(item) // Находим необходимый layout (шаблон)
        fun bind(catalog: CatalogItem, listener: Listener) = with(binding)
        {
            // Расскладываем данные по объектам на странице Itemobject
            analiseHeader.text = catalog.name
            priceAnalise.text = catalog.price.toString() + " ₽"
            countDayAnalise.text = catalog.timeResul
            btnAddDelAnalise.setOnClickListener()
            {
                listener.OnClick(catalog)
            }
            itemView.setOnClickListener()
            {
                listener.itemOnClick(catalog)
            }
        }
    }

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

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

    override fun onBindViewHolder(holder: CatalogHolder, position: Int) {
        holder.bind(catalogList[position], listener)
    }
    fun addCatalog(catalog: CatalogItem) {
        catalogList.add(catalog)
        notifyDataSetChanged()
    }

    interface Listener {
        fun OnClick(catalog: CatalogItem)
        fun itemOnClick(catalog: CatalogItem)
    }

}
