import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/app.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/services/locale_store.dart';
import 'package:tane/ui/settings_screen.dart';

import '../support/test_support.dart';

/// Asturian (`ast`) is a first-class language, but Flutter ships no
/// Material/Cupertino localizations for it. The app carries the honest `ast`
/// locale for its own strings and borrows Spanish for the framework chrome
/// (see [materialLocaleFor]). These tests pin both halves of that contract.
void main() {
  testWidgets('picking Asturian switches app text to Asturian, keeps Material '
      'chrome resolvable in Spanish, and persists the choice', (tester) async {
    LocaleSettings.setLocaleSync(AppLocale.en);
    final store = LocaleStore(InMemorySecretStore());
    late Locale materialLocale;

    await tester.pumpWidget(
      TranslationProvider(
        child: Builder(
          builder: (context) {
            final appLocale = TranslationProvider.of(context).flutterLocale;
            return MaterialApp(
              // Mirrors app.dart: the framework localizations follow the mapped
              // locale, the app's own strings follow slang.
              locale: materialLocaleFor(appLocale),
              supportedLocales: AppLocaleUtils.supportedLocales,
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              home: Builder(
                builder: (context) {
                  // Resolving these must never throw for the Asturian locale.
                  materialLocale = Localizations.localeOf(context);
                  MaterialLocalizations.of(context);
                  return SettingsScreen(localeStore: store);
                },
              ),
            );
          },
        ),
      ),
    );

    // Starts in English.
    expect(find.text('Language'), findsOneWidget);
    expect(materialLocale.languageCode, 'en');

    await tester.tap(find.text('Asturianu'));
    await tester.pumpAndSettle();

    // App strings are now Asturian…
    expect(find.text('Llingua'), findsOneWidget);
    // …the framework chrome fell back to Spanish (not the unsupported `ast`)…
    expect(materialLocale.languageCode, 'es');
    // …and the choice was remembered for next launch.
    expect(await store.saved(), AppLocale.ast);
  });

  test('materialLocaleFor maps Asturian to Spanish and passes others through',
      () {
    expect(materialLocaleFor(const Locale('ast')), const Locale('es'));
    expect(materialLocaleFor(const Locale('es')), const Locale('es'));
    expect(materialLocaleFor(const Locale('en')), const Locale('en'));
    expect(materialLocaleFor(const Locale('pt')), const Locale('pt'));
  });
}
