package com.example.mysmartlab.Adapter

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.mysmartlab.Model.NewsItem
import com.example.mysmartlab.R
import com.example.mysmartlab.databinding.NewsBinding

class AdapterNews() :
    RecyclerView.Adapter<AdapterNews.NewHolder>() {
    private val newsList = ArrayList<NewsItem>()


    class NewHolder(item: View) : RecyclerView.ViewHolder(item) {
        private val binding = NewsBinding.bind(item)
        fun bind(new: NewsItem) = with(binding)
        {
            newsName.text = new.name
            newsDescription.text = new.description
            newsPrice.text = new.price.toString()
            Glide
                .with(binding.root)
                .load(new.image)
                .into(newsImage)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.news, parent, false)
        return NewHolder(view)
    }
     override fun onBindViewHolder(holder: NewHolder, position: Int) {
        holder.bind(newsList[position])
    }

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

    fun addNew(new: NewsItem) {
        newsList.add(new)
        notifyDataSetChanged()
    }
}