import 'package:flutter/material.dart';
import '../../../l10n/app_localizations.dart';
import '../../providers/locale_provider.dart';

/// Sélecteur de langue de l'application (parmi [supportedLocales]).
class LanguagePicker extends StatelessWidget {
  final Locale selected;
  final Color primaryColor;
  final void Function(Locale) onSelect;

  const LanguagePicker({
    super.key,
    required this.selected,
    required this.primaryColor,
    required this.onSelect,
  });

  static const _flags = {'en': '🇬🇧', 'fr': '🇫🇷'};

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    final names = {'en': l.langEnglish, 'fr': l.langFrench};

    return Column(
      children: supportedLocales.map((locale) {
        final isSelected = locale.languageCode == selected.languageCode;
        return GestureDetector(
          onTap: () => onSelect(locale),
          child: AnimatedContainer(
            duration: const Duration(milliseconds: 200),
            margin: const EdgeInsets.only(bottom: 10),
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
            decoration: BoxDecoration(
              color: isSelected ? primaryColor : Colors.white,
              borderRadius: BorderRadius.circular(12),
              border: Border.all(
                color: isSelected ? primaryColor : Colors.grey.shade300,
                width: isSelected ? 3 : 1.5,
              ),
            ),
            child: Row(
              children: [
                Text(_flags[locale.languageCode] ?? '', style: const TextStyle(fontSize: 22)),
                const SizedBox(width: 16),
                Text(
                  names[locale.languageCode] ?? locale.languageCode,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: isSelected ? Colors.white : Colors.black87,
                  ),
                ),
                const Spacer(),
                if (isSelected) const Icon(Icons.check_circle, color: Colors.white, size: 22),
              ],
            ),
          ),
        );
      }).toList(),
    );
  }
}
