package com.example.smartlab.Api

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.example.smartlab.Models.News
import com.example.smartlab.databinding.ItemnewsBinding

class AdapterNews(): RecyclerView.Adapter<AdapterNews.NewsHolder>() {
    private val ADList = ArrayList<News>()

    class NewsHolder(item: View) : RecyclerView.ViewHolder(item) {
        private val binding = ItemnewsBinding.bind(item)
        fun bind(news: News) = with(binding)
        {
            newsName.text = news.name
            newsDescription.text = news.description
            newsCost.text = news.price.toString() + " ₽"
            newsPhoto.load(news.image)
        }
}

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

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

    override fun onBindViewHolder(holder: NewsHolder, position: Int) {
        holder.bind(ADList[position])
    }
    fun addNews(news: News){
        ADList.add(news)
        notifyDataSetChanged()
    }
}