package com.example.projekta

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView

class HistoriAdapter(

    private val context: Context,
    private val historiList: ArrayList<HistoriModel>

) : RecyclerView.Adapter<HistoriAdapter.ViewHolder>() {

    class ViewHolder(itemView: View)
        : RecyclerView.ViewHolder(itemView) {

        val tvTanggal: TextView =
            itemView.findViewById(R.id.tvTanggal)

        val tvJam: TextView =
            itemView.findViewById(R.id.tvJam)

        val tvMode: TextView =
            itemView.findViewById(R.id.tvMode)

        val tvDurasi: TextView =
            itemView.findViewById(R.id.tvDurasi)

        val tvInterval: TextView =
            itemView.findViewById(R.id.tvInterval)

        val tvStatusTanah: TextView =
            itemView.findViewById(R.id.tvStatusTanah)

        val cardHistori: CardView =
            itemView.findViewById(R.id.cardHistori)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {

        val view = LayoutInflater.from(context)
            .inflate(
                R.layout.item_histori,
                parent,
                false
            )

        return ViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {

        val histori =
            historiList[position]

        // TANGGAL
        holder.tvTanggal.text =
            histori.createdAt

        // JAM
        holder.tvJam.text =
            histori.jamPemupukan

        // MODE
        holder.tvMode.text =
            "• Mode: ${histori.mode}"

        // DURASI
        holder.tvDurasi.text =
            "• Durasi: ${histori.durasiPompa}"

        // INTERVAL
        holder.tvInterval.text =
            "• Interval: ${histori.intervalHari}"

        // STATUS TANAH
        holder.tvStatusTanah.text =
            "🔥 Status Tanah: ${histori.statusTanah}"
    }

    override fun getItemCount(): Int {

        return historiList.size
    }
}