package com.example.quiz.data.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.quiz.R;
import com.example.quiz.data.model.Reunion;

import java.util.ArrayList;
import java.util.List;

public class ReunionAdapter extends RecyclerView.Adapter<ReunionAdapter.ReunionViewHolder> {

    private List<Reunion> reunions = List.of();

    private onItemClickListener listener;

    public interface onItemClickListener {
        void onItemClick(Reunion reunion);
    }

    public void setOnItemClickListener(onItemClickListener listener){
        this.listener = listener;
    }

    public void setReunions(List<Reunion> reunions){
        this.reunions = reunions;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ReunionViewHolder holder, int position){
        Reunion reunion = reunions.get(position);

        holder.reunionName.setText(String.valueOf(reunion.getNom()));
        holder.reunionDate.setText(String.valueOf(reunion.getDate()));

        holder.itemView.setOnClickListener(v -> {
            if(listener != null){
                listener.onItemClick(reunion);
            }
        });
    }

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

    static class ReunionViewHolder extends RecyclerView.ViewHolder {
        TextView reunionName, reunionDate;

        public ReunionViewHolder(View itemView){
            super(itemView);
            reunionName = itemView.findViewById(R.id.reunionName);
            reunionDate = itemView.findViewById(R.id.reunionDate);
        }
    }
}
