// AI English Learning App Widget Tests
//
// Comprehensive widget tests for the AI English Learning application.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// Import the main app
import 'package:ai_english_learning/main.dart';

Widget createTestApp() {
  return const MaterialApp(
    home: Scaffold(
      body: Center(
        child: Text('AI English Learning Test App'),
      ),
    ),
  );
}

void main() {
  group('App Initialization Tests', () {
    testWidgets('Test app should build correctly', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      expect(find.text('AI English Learning Test App'), findsOneWidget);
    });

    testWidgets('App should build without errors', (WidgetTester tester) async {
      // Build our test app and trigger a frame.
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // Verify that the app builds successfully
      expect(find.byType(MaterialApp), findsOneWidget);
    });

    testWidgets('App should have correct title', (WidgetTester tester) async {
      // Build our test app
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // Get the MaterialApp widget
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      
      // Verify the app has a title (test app doesn't have specific title)
      expect(materialApp, isNotNull);
    });

    testWidgets('App should use correct theme', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // Get the MaterialApp widget
      final materialApp = tester.widget<MaterialApp>(find.byType(MaterialApp));
      
      // Verify theme is available
      expect(materialApp, isNotNull);
    });
  });

  group('Navigation Tests', () {
    testWidgets('App should build without errors', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // 验证应用能够正常构建
      expect(find.byType(MaterialApp), findsOneWidget);
    });
  });

  group('Performance Tests', () {
    testWidgets('Test app should render quickly', (WidgetTester tester) async {
      final stopwatch = Stopwatch()..start();
      
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();
      
      stopwatch.stop();
      
      // Verify app renders quickly (less than 1 second)
      expect(stopwatch.elapsedMilliseconds, lessThan(1000));
    });
  });

  group('Widget Interaction Tests', () {
    testWidgets('App should handle basic interactions', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // 验证应用响应基本交互
      expect(find.byType(MaterialApp), findsOneWidget);
    });
  });

  group('Form Validation Tests', () {
    testWidgets('App should support text input', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // 验证应用支持文本输入功能
      expect(find.byType(MaterialApp), findsOneWidget);
    });
  });

  group('Accessibility Tests', () {
    testWidgets('App should support accessibility', (WidgetTester tester) async {
      await tester.pumpWidget(createTestApp());
      await tester.pumpAndSettle();

      // 验证应用支持可访问性
      final semanticsHandle = tester.ensureSemantics();
      expect(find.byType(MaterialApp), findsOneWidget);
      semanticsHandle.dispose();
    });
  });

  group('Error Handling Tests', () {
    testWidgets('App should handle errors gracefully', (WidgetTester tester) async {
      // Test error boundary behavior
      await tester.pumpWidget(
        ProviderScope(
          child: MaterialApp(
            home: Scaffold(
              body: Builder(
                builder: (context) {
                  try {
                    return const Text('正常内容');
                  } catch (e) {
                    return Text('错误: $e');
                  }
                },
              ),
            ),
          ),
        ),
      );

      // Verify normal content is displayed
      expect(find.text('正常内容'), findsOneWidget);
    });
  });
}
