package com.nurulwafa.nufakuproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class BeritaAdapter extends RecyclerView.Adapter<BeritaAdapter.BeritaViewHolder> {

    private List<Berita> listBerita;
    private Context context;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Berita berita);
    }

    public BeritaAdapter(Context context, List<Berita> listBerita, OnItemClickListener listener) {
        this.context = context;
        this.listBerita = listBerita;
        this.listener = listener;
    }

    @NonNull
    @Override
    public BeritaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_berita, parent, false);
        return new BeritaViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BeritaViewHolder holder, int position) {
        Berita berita = listBerita.get(position);
        holder.tvJudulBerita.setText(berita.getJudul());

        // Jika kamu ingin klik tombol detail untuk pindah halaman:
        holder.btnDetailBerita.setOnClickListener(v -> listener.onItemClick(berita));
    }

    @Override
    public int getItemCount() {
        return listBerita.size();
    }

    public class BeritaViewHolder extends RecyclerView.ViewHolder {
        TextView tvJudulBerita;
        ImageView btnDetailBerita;

        public BeritaViewHolder(@NonNull View itemView) {
            super(itemView);
            tvJudulBerita = itemView.findViewById(R.id.tvJudulBerita);
            btnDetailBerita = itemView.findViewById(R.id.btnDetailBerita);
        }
    }
}



