import 'dart:ui' show PointerDeviceKind;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/main.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:fotodocumentation/utils/main_utils.dart';

import 'testing/test_utils.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  DiContainer.instance.initState();
  DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());

  group('FotoDocumentationApp', () {
    testWidgets('renders MaterialApp with router', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Verify MaterialApp.router is used
      expect(find.byType(MaterialApp), findsOneWidget);
    });

    testWidgets('has correct app title', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify its title
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.title, equals('Hartmann Foto App'));
    });

    testWidgets('uses custom scroll behavior', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify its scrollBehavior type
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.scrollBehavior, isA<MyCustomScrollBehavior>());
    });

    testWidgets('supports German locale', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify German is in supported locales
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.supportedLocales, contains(const Locale('de')));
    });

    testWidgets('has localization delegates configured', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify localization delegates are set
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.localizationsDelegates, isNotNull);
      expect(materialApp.localizationsDelegates!.length, equals(4));
    });

    testWidgets('applies provided theme', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      final customTheme = ThemeData(
        primarySwatch: Colors.blue,
        brightness: Brightness.light,
      );

      await tester.pumpWidget(
        FotoDocumentationApp(theme: customTheme),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify the theme is applied
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.theme, equals(customTheme));
    });

    testWidgets('applies dark theme when provided', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      final darkTheme = ThemeData.dark();

      await tester.pumpWidget(
        FotoDocumentationApp(theme: darkTheme),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify the dark theme is applied
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.theme, equals(darkTheme));
    });

    testWidgets('uses router config', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await tester.pumpWidget(
        FotoDocumentationApp(theme: ThemeData.light()),
      );
      await tester.pumpAndSettle();

      // Find the MaterialApp and verify routerConfig is set
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      expect(materialApp.routerConfig, isNotNull);
    });
  });

  group('MyCustomScrollBehavior', () {
    test('includes touch pointer device', () {
      final scrollBehavior = MyCustomScrollBehavior();
      expect(scrollBehavior.dragDevices, contains(PointerDeviceKind.touch));
    });

    test('includes mouse pointer device', () {
      final scrollBehavior = MyCustomScrollBehavior();
      expect(scrollBehavior.dragDevices, contains(PointerDeviceKind.mouse));
    });

    test('extends MaterialScrollBehavior', () {
      final scrollBehavior = MyCustomScrollBehavior();
      expect(scrollBehavior, isA<MaterialScrollBehavior>());
    });
  });
}
