import 'package:flutter/material.dart';
import 'package:em2rp/utils/colors.dart';

/// États possibles lors de l'ajout d'un équipement
enum AddEquipmentState {
  loading,
  success,
  error,
}

/// Dialog pour afficher le résultat de l'ajout d'un équipement/container
class AddEquipmentToEventDialog extends StatelessWidget {
  final AddEquipmentState state;
  final String? itemName;
  final String? errorMessage;

  const AddEquipmentToEventDialog({
    super.key,
    required this.state,
    this.itemName,
    this.errorMessage,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildIcon(),
            const SizedBox(height: 16),
            _buildMessage(),
            if (state != AddEquipmentState.loading) ...[
              const SizedBox(height: 24),
              ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.rouge,
                ),
                child: const Text('OK'),
              ),
            ],
          ],
        ),
      ),
    );
  }

  Widget _buildIcon() {
    switch (state) {
      case AddEquipmentState.loading:
        return const SizedBox(
          width: 64,
          height: 64,
          child: CircularProgressIndicator(
            color: AppColors.rouge,
            strokeWidth: 4,
          ),
        );
      case AddEquipmentState.success:
        return const Icon(
          Icons.check_circle,
          size: 64,
          color: Colors.green,
        );
      case AddEquipmentState.error:
        return const Icon(
          Icons.error,
          size: 64,
          color: Colors.red,
        );
    }
  }

  Widget _buildMessage() {
    switch (state) {
      case AddEquipmentState.loading:
        return const Text(
          'Recherche en cours...',
          style: TextStyle(fontSize: 16),
        );
      case AddEquipmentState.success:
        return Column(
          children: [
            const Text(
              'Ajouté avec succès !',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            if (itemName != null) ...[
              const SizedBox(height: 8),
              Text(
                itemName!,
                style: const TextStyle(fontSize: 16),
                textAlign: TextAlign.center,
              ),
            ],
          ],
        );
      case AddEquipmentState.error:
        return Column(
          children: [
            const Text(
              'Non trouvé',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            if (errorMessage != null) ...[
              const SizedBox(height: 8),
              Text(
                errorMessage!,
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.grey[600]),
              ),
            ],
          ],
        );
    }
  }
}
