package com.sambath.kunkhmer.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sambath.kunkhmer.R
import com.squareup.picasso.Picasso

class TopNewsHeaderAdapter(private val cardList: List<TopNewsCardItem>) :
    RecyclerView.Adapter<TopNewsHeaderAdapter.ViewHolder>() {
    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = cardList[position]

        Picasso.get().load(currentItem.imageResource).into(holder.imageView)
        holder.dateTextView.text = currentItem.date
        holder.titleTextView.text = currentItem.title
        holder.subTextView.text = currentItem.subTitle
    }

    override fun getItemCount() = cardList.size

    // Holds the views for adding it to image and text
    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val dateTextView: TextView = itemView.findViewById(R.id.dateTextView)
        val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
        val subTextView: TextView = itemView.findViewById(R.id.subTitleTextView)
    }
}