import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/controller/base_controller.dart';
import 'package:fotodocumentation/pages/ui_utils/component/general_error_widget.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';

import '../../testing/test_utils.dart';

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

  group('GeneralErrorWidget', () {
    testWidgets('displays error icon', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Test error'),
      );
      await tester.pumpAndSettle();

      // Verify error icon is displayed
      expect(find.byIcon(Icons.error_outline), findsOneWidget);
    });

    testWidgets('displays error message with error text', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Something went wrong'),
      );
      await tester.pumpAndSettle();

      // Verify error message is displayed (German localization: "Fehler: {name}")
      expect(find.text('Fehler: Something went wrong'), findsOneWidget);
    });

    testWidgets('displays status code message when statusCode is provided', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: '', statusCode: 404),
      );
      await tester.pumpAndSettle();

      // Verify status code message is displayed (German localization: "Statuscode {statusCode}")
      expect(find.text('Statuscode 404'), findsOneWidget);
    });

    testWidgets('displays status code message for 500 error', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: '', statusCode: 500),
      );
      await tester.pumpAndSettle();

      // Verify status code message is displayed
      expect(find.text('Statuscode 500'), findsOneWidget);
    });

    testWidgets('does not display retry button when reload is null', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Test error'),
      );
      await tester.pumpAndSettle();

      // Verify retry button is not displayed
      expect(find.byType(ElevatedButton), findsNothing);
      expect(find.text('Wiederholen'), findsNothing);
    });

    testWidgets('displays retry button when reload is provided', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        GeneralErrorWidget(error: 'Test error', reload: () {}),
      );
      await tester.pumpAndSettle();

      // Verify retry button is displayed (German localization: "Wiederholen")
      expect(find.byType(ElevatedButton), findsOneWidget);
      expect(find.text('Wiederholen'), findsOneWidget);
    });

    testWidgets('calls reload callback when retry button is tapped', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      bool reloadCalled = false;

      await pumpApp(
        tester,
        GeneralErrorWidget(
          error: 'Test error',
          reload: () {
            reloadCalled = true;
          },
        ),
      );
      await tester.pumpAndSettle();

      // Tap retry button
      await tester.tap(find.byType(ElevatedButton));
      await tester.pumpAndSettle();

      // Verify reload was called
      expect(reloadCalled, isTrue);
    });

    testWidgets('fromServerError factory creates widget with status code', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      final serverError = ServerError(403);

      await pumpApp(
        tester,
        GeneralErrorWidget.fromServerError(serverError),
      );
      await tester.pumpAndSettle();

      // Verify status code message is displayed
      expect(find.text('Statuscode 403'), findsOneWidget);
    });

    testWidgets('fromServerError factory with reload callback', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      bool reloadCalled = false;
      final serverError = ServerError(401);

      await pumpApp(
        tester,
        GeneralErrorWidget.fromServerError(
          serverError,
          reload: () {
            reloadCalled = true;
          },
        ),
      );
      await tester.pumpAndSettle();

      // Verify status code is displayed
      expect(find.text('Statuscode 401'), findsOneWidget);

      // Verify retry button is displayed and functional
      expect(find.text('Wiederholen'), findsOneWidget);
      await tester.tap(find.byType(ElevatedButton));
      await tester.pumpAndSettle();

      expect(reloadCalled, isTrue);
    });

    testWidgets('widget renders correctly', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Test error'),
      );
      await tester.pumpAndSettle();

      // Verify GeneralErrorWidget is rendered
      expect(find.byType(GeneralErrorWidget), findsOneWidget);

      // Verify it contains a Column for layout
      final columnFinder = find.descendant(
        of: find.byType(GeneralErrorWidget),
        matching: find.byType(Column),
      );
      expect(columnFinder, findsOneWidget);
    });

    testWidgets('error icon has correct color', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Test error'),
      );
      await tester.pumpAndSettle();

      // Find the Icon widget and verify its color
      final icon = tester.widget<Icon>(find.byIcon(Icons.error_outline));
      expect(icon.color, equals(Colors.red[300]));
    });

    testWidgets('error icon has correct size', (WidgetTester tester) async {
      setScreenSize(tester, 1024, 1024);

      await pumpApp(
        tester,
        const GeneralErrorWidget(error: 'Test error'),
      );
      await tester.pumpAndSettle();

      // Find the Icon widget and verify its size
      final icon = tester.widget<Icon>(find.byIcon(Icons.error_outline));
      expect(icon.size, equals(60));
    });
  });
}
