package com.example.pap_teste;

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.pap_teste.models.LocalReservation;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.chip.Chip;
import java.util.List;

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

    private final List<LocalReservation> reservations;
    private final OnActionListener listener;

    public interface OnActionListener {
        void onCancel(LocalReservation reservation);
        void onEdit(LocalReservation reservation);
    }

    public LocalReservaAdapter(List<LocalReservation> reservations, OnActionListener listener) {
        this.reservations = reservations;
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LocalReservation res = reservations.get(position);
        holder.restaurantName.setText(res.getRestaurantName());
        holder.dateInfo.setText(res.getDate() + " às " + res.getTime());
        holder.tableInfo.setText("Mesa " + res.getTableNumber() + " • " + res.getGuests() + " pessoas");
        holder.statusChip.setText(res.getStatus());

        holder.btnCancel.setOnClickListener(v -> listener.onCancel(res));
        holder.itemView.setOnClickListener(v -> listener.onEdit(res));
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView restaurantName, dateInfo, tableInfo;
        Chip statusChip;
        MaterialButton btnCancel;

        ViewHolder(View itemView) {
            super(itemView);
            restaurantName = itemView.findViewById(R.id.txtRestauranteNome);
            dateInfo = itemView.findViewById(R.id.txtDataHora);
            tableInfo = itemView.findViewById(R.id.txtMesaPessoas);
            statusChip = itemView.findViewById(R.id.chipStatus);
            btnCancel = itemView.findViewById(R.id.btnCancelar);
        }
    }
}
