package com.example.pap_teste;

import android.graphics.Color;
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 com.example.pap_teste.models.Mesa;
import com.google.android.material.card.MaterialCardView;
import java.util.List;

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

    private final List<Mesa> tables;
    private final OnTableClickListener listener;
    private int selectedPosition = -1;

    public interface OnTableClickListener {
        void onTableClick(Mesa mesa);
    }

    public TableSelectionAdapter(List<Mesa> tables, OnTableClickListener listener) {
        this.tables = tables;
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Mesa mesa = tables.get(position);
        holder.txtNumber.setText("Mesa " + mesa.getNumero());
        
        boolean isOccupied = "OCUPADA".equalsIgnoreCase(mesa.getEstado()) || "RESERVADA".equalsIgnoreCase(mesa.getEstado());
        holder.txtStatus.setText(mesa.getEstado());
        
        if (isOccupied) {
            holder.card.setCardBackgroundColor(Color.parseColor("#F5F5F5"));
            holder.card.setStrokeColor(Color.TRANSPARENT);
            holder.txtStatus.setTextColor(Color.GRAY);
            holder.itemView.setEnabled(false);
            holder.imgStatus.setAlpha(0.3f);
        } else {
            boolean isSelected = selectedPosition == position;
            holder.card.setCardBackgroundColor(isSelected ? holder.itemView.getContext().getColor(R.color.colorChipConfirmed) : Color.WHITE);
            holder.card.setStrokeColor(isSelected ? holder.itemView.getContext().getColor(R.color.colorSuccess) : holder.itemView.getContext().getColor(R.color.colorBorder));
            holder.txtStatus.setTextColor(isSelected ? holder.itemView.getContext().getColor(R.color.colorSuccess) : holder.itemView.getContext().getColor(R.color.colorTextSecondary));
            holder.itemView.setEnabled(true);
            holder.imgStatus.setAlpha(1.0f);
        }

        holder.itemView.setOnClickListener(v -> {
            int previousSelected = selectedPosition;
            selectedPosition = holder.getAdapterPosition();
            if (previousSelected != -1) notifyItemChanged(previousSelected);
            notifyItemChanged(selectedPosition);
            listener.onTableClick(mesa);
        });
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        MaterialCardView card;
        TextView txtNumber, txtStatus;
        ImageView imgStatus;

        ViewHolder(View itemView) {
            super(itemView);
            card = itemView.findViewById(R.id.cardTable);
            txtNumber = itemView.findViewById(R.id.txtTableNumber);
            txtStatus = itemView.findViewById(R.id.txtTableStatus);
            imgStatus = itemView.findViewById(R.id.imgTableStatus);
        }
    }
}
