import 'package:flutter/material.dart';
import '../../../l10n/app_localizations.dart';
import 'package:kmobile/app.dart';

class LanguageDialog extends StatelessWidget {
  const LanguageDialog({super.key});

  String getLocaleName(AppLocalizations localizations, String code) {
    final localeCodeMap = {
      'en': localizations.english,
      'hi': localizations.hindi,
    };
    return localeCodeMap[code] ?? 'Unknown';
  }

  @override
  Widget build(BuildContext context) {
    return Builder(
      builder: (context) {
        final localizations = AppLocalizations.of(context);

        final supportedLocales = [const Locale('en'), const Locale('hi')];

        return AlertDialog(
          title: Text(localizations.language),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: supportedLocales.map((locale) {
              return ListTile(
                leading: const Icon(Icons.language),
                title: Text(getLocaleName(localizations, locale.languageCode)),
                onTap: () {
                  Navigator.pop(context);
                  KMobile.setLocale(context, locale);
                },
              );
            }).toList(),
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text(localizations.cancel),
            ),
          ],
        );
      },
    );
  }
}
