import 'dart:math';
import 'package:flutter/material.dart';

void main() {
  runApp(const EnglishLearningApp());
}

class EnglishLearningApp extends StatelessWidget {
  const EnglishLearningApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'تعلم الإنجليزية',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('الصفحة الرئيسية'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const LevelSelectionScreen(),
                  ),
                );
              },
              child: const Text('اختيار المستوى'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const TestSelectionScreen(),
                  ),
                );
              },
              child: const Text('اختبارات'),
            ),
          ],
        ),
      ),
    );
  }
}

class LevelSelectionScreen extends StatelessWidget {
  const LevelSelectionScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('اختيار المستوى'),
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: ListView(
        children: <Widget>[
          ListTile(
            title: const Text('المستوى الأول (A-E)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LetterScreen(letters: 'ABCDE'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('المستوى الثاني (F-I)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LetterScreen(letters: 'FGHI'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('المستوى الثالث (J-P)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LetterScreen(letters: 'JKLMNOP'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('المستوى الرابع (Q-Z)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      const LetterScreen(letters: 'QRSTUVWXYZ'),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}

class LetterScreen extends StatelessWidget {
  final String letters;

  const LetterScreen({Key? key, required this.letters}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('تعلم الحروف'),
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          crossAxisSpacing: 10,
          mainAxisSpacing: 10,
        ),
        padding: const EdgeInsets.all(16.0),
        itemCount: letters.length,
        itemBuilder: (context, index) {
          final letter = letters[index];
          return LetterButton(letter: letter);
        },
      ),
    );
  }
}

class LetterButton extends StatelessWidget {
  final String letter;

  const LetterButton({Key? key, required this.letter}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => LetterDetailScreen(letter: letter),
          ),
        );
      },
      child: Text(letter, style: const TextStyle(fontSize: 24.0)),
    );
  }
}

class LetterDetailScreen extends StatelessWidget {
  final String letter;

  const LetterDetailScreen({Key? key, required this.letter}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final images = {
      'A': 'assets/apple.png',
      'B': 'assets/ball.png',
      'C': 'assets/cat.png',
    };

    return Scaffold(
      appBar: AppBar(
        title: Text('حرف $letter'),
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              letter,
              style: const TextStyle(fontSize: 100.0),
            ),
            const SizedBox(height: 20),
            Text(
              _getLetterDescription(letter),
              style: const TextStyle(fontSize: 24.0),
            ),
            const SizedBox(height: 20),
            if (images.containsKey(letter)) Image.asset(images[letter]!),
          ],
        ),
      ),
    );
  }

  String _getLetterDescription(String letter) {
    final descriptions = {
      'A': 'Apple',
      'B': 'Ball',
      'C': 'Cat',
    };
    return descriptions[letter] ?? 'No description available';
  }
}

class TestSelectionScreen extends StatelessWidget {
  const TestSelectionScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('اختبارات'),
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: ListView(
        children: <Widget>[
          ListTile(
            title: const Text('اختبار المستوى الأول (A-E)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LevelTestScreen(letters: 'ABCDE'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('اختبار المستوى الثاني (F-I)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LevelTestScreen(letters: 'FGHI'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('اختبار المستوى الثالث (J-P)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      const LevelTestScreen(letters: 'JKLMNOP'),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('اختبار المستوى الرابع (Q-Z)'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      const LevelTestScreen(letters: 'QRSTUVWXYZ'),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}

class LevelTestScreen extends StatefulWidget {
  final String letters;

  const LevelTestScreen({Key? key, required this.letters}) : super(key: key);

  @override