import 'package:flutter/material.dart';
import 'package:geosector_app/core/services/api_service.dart';

/// Widget qui affiche les informations sur l'environnement actuel
/// Utile pour le débogage
class EnvironmentInfoWidget extends StatelessWidget {
  final bool showInDialog;

  const EnvironmentInfoWidget({
    super.key,
    this.showInDialog = false,
  });

  @override
  Widget build(BuildContext context) {
    final apiService = ApiService.instance;
    final environment = apiService.getCurrentEnvironment();
    final apiUrl = apiService.getCurrentApiUrl();
    final appIdentifier = apiService.getCurrentAppIdentifier();

    final content = Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '🌍 Environnement GeoSector',
            style: Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold, color: _getEnvironmentColor(environment)),
          ),
          const SizedBox(height: 16),
          _buildInfoRow(context, 'Environnement', environment),
          const Divider(),
          _buildInfoRow(context, 'URL API', apiUrl),
          const Divider(),
          _buildInfoRow(context, 'App Identifier', appIdentifier),
          const SizedBox(height: 20),
          if (!showInDialog)
            Align(
              alignment: Alignment.center,
              child: TextButton(
                onPressed: () => Navigator.of(context).pop(),
                child: const Text('Fermer'),
              ),
            ),
        ],
      ),
    );

    if (showInDialog) {
      return content;
    }

    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: content,
    );
  }

  Widget _buildInfoRow(BuildContext context, String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 120,
            child: Text(
              label,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: Theme.of(context).textTheme.bodyMedium,
            ),
          ),
        ],
      ),
    );
  }

  Color _getEnvironmentColor(String environment) {
    switch (environment) {
      case 'DEV':
        return Colors.green;
      case 'REC':
        return Colors.orange;
      case 'PROD':
        return Colors.red;
      default:
        return Colors.grey;
    }
  }

  /// Méthode statique pour afficher les informations sur l'environnement
  /// dans une boîte de dialogue
  static void show(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => const EnvironmentInfoWidget(),
    );
  }
}
