package com.example.smartlab.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.smartlab.R
import com.example.smartlab.databinding.ItemOfCatalogBinding
import com.example.smartlab.models.Catalog

class CatalogAdapter() : ListAdapter<Catalog, CatalogAdapter.Holder>(Comparator())  {

    class Holder(view: View) : RecyclerView.ViewHolder(view){

        private val binding = ItemOfCatalogBinding.bind(view)

        fun bind(catalog: Catalog) = with(binding){

            catalogHeader.text = catalog.name
            days.text = catalog.timeResul
            price.text = catalog.price.toString() + " ₽"
        }

    }

    class Comparator: DiffUtil.ItemCallback<Catalog>(){
        override fun areItemsTheSame(oldItem: Catalog, newItem: Catalog): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Catalog, newItem: Catalog): Boolean {
            return oldItem == newItem
        }
    }

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

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bind(getItem(position))
    }


}