// ignore_for_file: use_build_context_synchronously

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

class LanguageDialog extends StatelessWidget {
  // ignore: use_super_parameters
  const LanguageDialog({Key? key}) : super(key: key);

  Future<void> _setLocale(BuildContext context, String langCode) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('locale', langCode); // Save selected language
    KMobile.setLocale(context, Locale(langCode)); // Update locale in app
    Navigator.of(context).pop(); // Close the dialog
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(AppLocalizations.of(context).selectLanguage),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ListTile(
            leading: const Icon(Icons.language),
            title: const Text('English'),
            onTap: () => _setLocale(context, 'en'),
          ),
          ListTile(
            leading: const Icon(Icons.language),
            title: const Text('हिन्दी'),
            onTap: () => _setLocale(context, 'hi'),
          ),
        ],
      ),
    );
  }
}
