package com.example.pap_teste;

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

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

import com.example.pap_teste.models.Reserva;

import java.util.List;

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

    public interface OnReservaActionListener {
        void onCheckIn(Reserva reserva);

        void onCancel(Reserva reserva);
    }

    private final List<Reserva> reservas;
    private final OnReservaActionListener listener;

    public ReservaAdapter(List<Reserva> reservas, OnReservaActionListener listener) {
        this.reservas = reservas;
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Reserva reserva = reservas.get(position);
        holder.txtRestaurante.setText(reserva.getRestauranteName());
        holder.txtDataHora.setText(reserva.getData() + " às " + reserva.getHora() + " • " + reserva.getPessoas() + "p");
        
        if (holder.chipStatus != null) {
            String state = reserva.getEstado() != null ? reserva.getEstado() : "Pendente";
            holder.chipStatus.setText(state);
            int colorBg, colorText;
            android.content.Context ctx = holder.itemView.getContext();
            if ("Confirmada".equals(state)) {
                colorBg = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipConfirmed);
                colorText = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipConfirmedText);
            } else if ("Cancelada".equals(state) || "Recusada".equals(state)) {
                colorBg = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipCancelled);
                colorText = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipCancelledText);
            } else {
                colorBg = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipPending);
                colorText = androidx.core.content.ContextCompat.getColor(ctx, R.color.colorChipPendingText);
            }
            holder.chipStatus.setChipBackgroundColor(android.content.res.ColorStateList.valueOf(colorBg));
            holder.chipStatus.setTextColor(colorText);
        }

        // Enable check-in only if confirmed
        boolean isConfirmed = "Confirmada".equals(reserva.getEstado());
        boolean hasLocationPermission = androidx.core.app.ActivityCompat.checkSelfPermission(holder.itemView.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == android.content.pm.PackageManager.PERMISSION_GRANTED;

        if (isConfirmed) {
            if (hasLocationPermission) {
                holder.btnCheckIn.setEnabled(true);
                if (holder.txtLocationWarning != null) {
                    holder.txtLocationWarning.setVisibility(View.GONE);
                }
            } else {
                holder.btnCheckIn.setEnabled(false);
                if (holder.txtLocationWarning != null) {
                    holder.txtLocationWarning.setVisibility(View.VISIBLE);
                }
            }
        } else {
            holder.btnCheckIn.setEnabled(false);
            if (holder.txtLocationWarning != null) {
                holder.txtLocationWarning.setVisibility(View.GONE);
            }
        }

        // Show cancel only if not concluded or already cancelled/refused
        boolean canCancel = "Pendente".equals(reserva.getEstado()) || "Confirmada".equals(reserva.getEstado());
        holder.btnCancelar.setVisibility(canCancel ? View.VISIBLE : View.GONE);

        holder.btnCheckIn.setOnClickListener(v -> {
            if (listener != null)
                listener.onCheckIn(reserva);
        });

        holder.btnCancelar.setOnClickListener(v -> {
            if (listener != null)
                listener.onCancel(reserva);
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtRestaurante, txtDataHora, txtStatus, txtLocationWarning;
        Button btnCheckIn, btnCancelar;
        com.google.android.material.chip.Chip chipStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtRestaurante = itemView.findViewById(R.id.txtReservaRestaurante);
            txtDataHora = itemView.findViewById(R.id.txtReservaDataHora);
            txtStatus = itemView.findViewById(R.id.txtReservaStatus);
            btnCheckIn = itemView.findViewById(R.id.btnCheckIn);
            btnCancelar = itemView.findViewById(R.id.btnCancelar);
            txtLocationWarning = itemView.findViewById(R.id.txtLocationWarning);
            chipStatus = itemView.findViewById(R.id.chipReservaStatus);
        }
    }
}
