import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:plezy/focus/input_mode_tracker.dart';
import 'package:plezy/i18n/strings.g.dart';
import 'package:plezy/screens/profile/add_local_profile_screen.dart';
import 'package:plezy/utils/platform_detector.dart';

import '../../test_helpers/prefs.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    resetSharedPreferencesForTest();
    TvDetectionService.debugSetAppleTVOverride(true);
    // Simulated TV device: the test host's desktop OS must not leak into
    // InputModeTracker's desktop force-TV exemption.
    PlatformDetector.debugSetIsDesktopOSOverride(false);
    LocaleSettings.setLocaleSync(AppLocale.en);
  });

  tearDown(() {
    TvDetectionService.debugSetAppleTVOverride(null);
    TvDetectionService.setForceTVSync(false);
    PlatformDetector.debugSetIsDesktopOSOverride(null);
  });

  testWidgets('D-pad leaves profile name input and reaches actions', (tester) async {
    await tester.pumpWidget(
      TranslationProvider(
        child: const InputModeTracker(child: MaterialApp(home: AddLocalProfileScreen())),
      ),
    );
    await tester.pumpAndSettle();
    final fieldFinder = find.byType(TextField);

    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:Name');
    expect(find.byKey(const Key('tv_virtual_keyboard_panel')), findsNothing);
    expect(tester.widget<TextField>(fieldFinder).readOnly, isFalse);

    await tester.enterText(fieldFinder, 'A');
    await tester.pump();
    expect(tester.widget<TextField>(fieldFinder).controller!.text, 'A');

    await tester.sendKeyEvent(LogicalKeyboardKey.gameButtonB);
    await tester.pumpAndSettle();

    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:Name');
    expect(tester.widget<TextField>(fieldFinder).readOnly, isTrue);

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pump();
    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:SetPin');

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pump();
    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:Continue');

    await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
    await tester.pump();
    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:Cancel');
  });

  testWidgets('Android TV virtual keyboard done leaves profile name input', (tester) async {
    TvDetectionService.debugSetAppleTVOverride(null);
    await TvDetectionService.getInstance(forceTv: true);
    TvDetectionService.setForceTVSync(true);

    await tester.pumpWidget(
      TranslationProvider(
        child: const InputModeTracker(child: MaterialApp(home: AddLocalProfileScreen())),
      ),
    );
    await tester.pumpAndSettle();

    expect(FocusManager.instance.primaryFocus?.debugLabel, 'TvVirtualKeyboard');

    await tester.tap(find.byIcon(Symbols.check_rounded));
    await tester.pumpAndSettle();

    expect(FocusManager.instance.primaryFocus?.debugLabel, 'AddLocalProfile:SetPin');
  });

  testWidgets('remote back pops the new profile page', (tester) async {
    await tester.pumpWidget(
      TranslationProvider(
        child: InputModeTracker(
          child: MaterialApp(
            home: Builder(
              builder: (context) => Scaffold(
                body: Center(
                  child: ElevatedButton(
                    onPressed: () =>
                        Navigator.of(context).push(MaterialPageRoute(builder: (_) => const AddLocalProfileScreen())),
                    child: const Text('Open new profile'),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.text('Open new profile'));
    await tester.pumpAndSettle();
    expect(find.text(t.profiles.newProfile), findsOneWidget);
    final fieldFinder = find.byType(TextField);
    expect(find.byKey(const Key('tv_virtual_keyboard_panel')), findsNothing);
    expect(tester.widget<TextField>(fieldFinder).readOnly, isFalse);
    await tester.showKeyboard(fieldFinder);

    await tester.sendKeyEvent(LogicalKeyboardKey.gameButtonB);
    await tester.pumpAndSettle();

    expect(find.text(t.profiles.newProfile), findsOneWidget);
    expect(find.byKey(const Key('tv_virtual_keyboard_panel')), findsNothing);
    expect(tester.widget<TextField>(fieldFinder).readOnly, isTrue);

    await tester.sendKeyEvent(LogicalKeyboardKey.gameButtonB);
    await tester.pumpAndSettle();

    expect(find.text('Open new profile'), findsOneWidget);
    expect(find.text(t.profiles.newProfile), findsNothing);
  });
}
