import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';
import 'package:mobile_application_kids/games.dart';

void main() {
  runApp(PosturePro());
}

class PosturePro extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LevelScreen(),
    );
  }
}

class LevelScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Select Level'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.all(16.0),
            child: Center(
              child: Text(
                "Let's get started!",
                style: TextStyle(
                    color: Color.fromARGB(255, 48, 173, 26),
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ),
          Expanded(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  LevelButton(
                    imagePath: 'game_assets/level1_button.png',
                    level: 1,
                  ),
                  SizedBox(height: 10), // Adjusted height between buttons
                  LevelButton(
                    imagePath: 'game_assets/level2_button.png',
                    level: 2,
                  ),
                  SizedBox(height: 10), // Adjusted height between buttons
                  LevelButton(
                    imagePath: 'game_assets/level3_button.png',
                    level: 3,
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 20),
          // Add some spacing between the buttons and the quit button
          ElevatedButton(
            onPressed: () {
              // Navigate back to the GamesSelectionScreen when the quit button is pressed
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => GamesPage()),
              );
            },
            child: Text('Quit Game'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 231, 65, 43),
              onPrimary: Colors.white,
            ),
          ),
          SizedBox(height: 40),
        ],
      ),
    );
  }
}

class LevelButton extends StatelessWidget {
  final String imagePath;
  final int level;

  const LevelButton({
    required this.imagePath,
    required this.level,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // Navigate to the selected level screen
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => getLevelScreen(level)),
        );
      },
      child: Image.asset(
        imagePath,
        width: 500,
        height: 160,
        // You may adjust the width and height as needed
      ),
    );
  }

  Widget getLevelScreen(int level) {
    switch (level) {
      case 1:
        return Level1Screen(level: level);
      case 2:
        return Level2Screen(level: level);
      case 3:
        return Level3Screen(
            level: level); // Changed to Level3Screen for level 3
      default:
        return Container(); // Return empty container for unknown level
    }
  }
}

class Level1Screen extends StatelessWidget {
  final int level;

  const Level1Screen({required this.level});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level $level'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.all(20.0),
            child: Text(
              'Exercises List',
              style: TextStyle(
                  color: Color.fromARGB(255, 48, 173, 26),
                  fontSize: 20,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: ListView(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(
                        bottom: 20.0), // Adjust the bottom padding as needed
                    child: Image.asset('game_assets/Exercise_list.png'),
                  ),
                ],
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              // Navigate to the exercise screen
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => ExerciseScreen(level: level)),
              );
            },
            child: Text('Start'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 43, 159, 231),
              onPrimary: Colors.white,
            ),
          ),
          SizedBox(height: 50),
        ],
      ),
    );
  }
}

class ExerciseScreen extends StatefulWidget {
  final int level;

  const ExerciseScreen({required this.level});

  @override
  _ExerciseScreenState createState() => _ExerciseScreenState();
}

class _ExerciseScreenState extends State<ExerciseScreen> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.asset('game_assets/level1_vid.mp4');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      autoPlay: true,
      looping: true,
      allowFullScreen: false,
      allowMuting: true,
      aspectRatio: 16 / 11,
    );
  }

  @override
  void dispose() {
    _videoPlayerController.pause();
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  void pauseVideo() {
    if (_videoPlayerController.value.isPlaying) {
      _videoPlayerController.pause();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level ${widget.level} - Exercises'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          AspectRatio(
            aspectRatio: 16 / 11,
            child: Chewie(
              controller: _chewieController,
            ),
          ),
          SizedBox(height: 20),
          Image.asset('game_assets/ex1_det.png'),
          SizedBox(height: 30),
          ElevatedButton(
            onPressed: () {
              pauseVideo();
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ResultScreen()),
              );
            },
            child: Text('Finish Exercise'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 48, 173, 26),
              onPrimary: Colors.white,
            ),
          ),
        ],
      ),
    );
  }
}

class ResultScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Exercise Result'),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(builder: (context) => LevelScreen()),
            );
          },
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(height: 0),
          Image.asset(
            'game_assets/result1.png',
            height: 650,
            width: 500, // Adjust the height of the image as needed
          ),
          SizedBox(height: 20),
        ],
      ),
    );
  }
}

class Level2Screen extends StatelessWidget {
  final int level;

  const Level2Screen({required this.level});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level $level'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.all(20.0),
            child: Text(
              'Behavior List',
              style: TextStyle(
                  color: Color.fromARGB(255, 48, 173, 26),
                  fontSize: 20,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: ListView(
                children: [
                  Image.asset(
                      'game_assets/Behavior_list.png'), // Use Behavior_list.png for Level 2
                ],
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              // Navigate to the exercise screen
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Level2VideoScreen(level: level)),
              );
            },
            child: Text('Start'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 43, 159, 231),
              onPrimary: Colors.white,
            ),
          ),
          SizedBox(height: 50),
        ],
      ),
    );
  }
}

class Level2VideoScreen extends StatefulWidget {
  final int level;

  const Level2VideoScreen({required this.level});

  @override
  _Level2VideoScreenState createState() => _Level2VideoScreenState();
}

class _Level2VideoScreenState extends State<Level2VideoScreen> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.asset('game_assets/level2_vid.mp4');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      autoPlay: true,
      looping: true,
      allowFullScreen: false,
      allowMuting: true,
      aspectRatio: 16 / 11,
    );
  }

  @override
  void dispose() {
    _videoPlayerController.pause();
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  void pauseVideo() {
    if (_videoPlayerController.value.isPlaying) {
      _videoPlayerController.pause();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level ${widget.level} Video'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          AspectRatio(
            aspectRatio: 16 / 11,
            child: Chewie(
              controller: _chewieController,
            ),
          ),
          SizedBox(height: 20),
          Image.asset('game_assets/ex2_det.png'),
          SizedBox(height: 40),
          ElevatedButton(
            onPressed: () {
              pauseVideo();
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ResultScreen()),
              );
            },
            child: Text('End Watching'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 48, 173, 26),
              onPrimary: Colors.white,
            ),
          ),
        ],
      ),
    );
  }
}

class Level3Screen extends StatelessWidget {
  final int level;

  const Level3Screen({required this.level});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level $level'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: EdgeInsets.all(16.0),
            child: Text(
              'Activity List', // Change to Activity List for Level 3
              style: TextStyle(
                  color: Color.fromARGB(255, 48, 173, 26),
                  fontSize: 20,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: ListView(
                children: [
                  Image.asset(
                      'game_assets/Activity_list.png'), // Use Activity_list.png for Level 3
                ],
              ),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              // Navigate to the exercise screen
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Level3VideoScreen(level: level)),
              );
            },
            child: Text('Start'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 43, 159, 231),
              onPrimary: Colors.white,
            ),
          ),
          SizedBox(height: 50),
        ],
      ),
    );
  }
}

class Level3VideoScreen extends StatefulWidget {
  final int level;

  const Level3VideoScreen({required this.level});

  @override
  _Level3VideoScreenState createState() => _Level3VideoScreenState();
}

class _Level3VideoScreenState extends State<Level3VideoScreen> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.asset('game_assets/level3_vid.mp4');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      autoPlay: true,
      looping: true,
      allowFullScreen: false,
      allowMuting: true,
      aspectRatio: 16 / 11,
    );
  }

  @override
  void dispose() {
    _videoPlayerController.pause();
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  void pauseVideo() {
    if (_videoPlayerController.value.isPlaying) {
      _videoPlayerController.pause();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Level ${widget.level} Video'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          AspectRatio(
            aspectRatio: 16 / 11,
            child: Chewie(
              controller: _chewieController,
            ),
          ),
          SizedBox(height: 20),
          Image.asset('game_assets/ex3_det.png'),
          SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              pauseVideo();
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => ResultScreen()),
              );
            },
            child: Text('End Watching'),
            style: ElevatedButton.styleFrom(
              primary: Color.fromARGB(255, 48, 173, 26),
              onPrimary: Colors.white,
            ),
          ),
        ],
      ),
    );
  }
}

class Level3ResultScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Exercise Result'),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(builder: (context) => LevelScreen()),
            );
          },
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(height: 0),
          Image.asset(
            'game_assets/result3.png', // Change to result3.png for Level 3
            height: 650,
            width: 500, // Adjust the height of the image as needed
          ),
          SizedBox(height: 20),
        ],
      ),
    );
  }
}
