package com.example.quiz.data.adapter;

import android.util.Log;
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.PointDeVente;

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

public class PointDeVenteAdapter extends RecyclerView.Adapter<PointDeVenteAdapter.ViewHolder> {

    private List<PointDeVente> points = new ArrayList<>();
    private OnItemClickListener listener;



    public PointDeVenteAdapter(){

    }
    // Interface pour gérer le clic sur un item
    public interface OnItemClickListener {
        void onItemClick(PointDeVente point);
    }

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

    public void setPoints(List<PointDeVente> points) {
        this.points = points;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PointDeVente point = points.get(position);
        holder.txtNom.setText(point.getNom());
        String adresse = point.getVille()+", "+point.getAdresse();
        holder.txtAdresse.setText(adresse);
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) listener.onItemClick(point);
        });
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtNom;

        TextView txtAdresse;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtNom = itemView.findViewById(R.id.txtNom);
            txtAdresse = itemView.findViewById(R.id.txtAdresse);
        }
    }
}

