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

class VideoBenchmarkScreen extends StatefulWidget {
  const VideoBenchmarkScreen({super.key});

  @override
  State<VideoBenchmarkScreen> createState() => _VideoBenchmarkScreenState();
}

class _VideoBenchmarkScreenState extends State<VideoBenchmarkScreen> {
  late VideoPlayerController _controller;
  String selectedMode = '720p';

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.asset(
      'assets/video/video_720p.mp4',
    )
      ..initialize().then((_) {
        setState(() {});
      })
      ..setLooping(true);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Video Player Demo ($selectedMode)'),
        actions: [
          ElevatedButton(
              onPressed: () {
                _controller = VideoPlayerController.asset(
                  'assets/video/video_720p.mp4',
                )
                  ..initialize().then((_) {
                    setState(() {
                      selectedMode = '720p';
                    });
                  })
                  ..setLooping(true);
              },
              child: const Text('load 720p')),
          const SizedBox(
            width: 8,
          ),
          ElevatedButton(
              onPressed: () {
                _controller = VideoPlayerController.asset(
                  'assets/video/video_1080p.mp4',
                )
                  ..initialize().then((_) {
                    setState(() {
                      selectedMode = '1080p';
                    });
                  })
                  ..setLooping(true);
              },
              child: const Text('load 1080p')),
          const SizedBox(
            width: 8,
          ),
          ElevatedButton(
              onPressed: () {
                _controller = VideoPlayerController.asset(
                  'assets/video/video_2160p.mp4',
                )
                  ..initialize().then((_) {
                    setState(() {
                      selectedMode = '2160p';
                    });
                  })
                  ..setLooping(true);
              },
              child: const Text('load 2160p')),
          const SizedBox(
            width: 8,
          ),
        ],
      ),
      body: Center(
        child: _controller.value.isInitialized
            ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
            : Container(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _controller.value.isPlaying
                ? _controller.pause()
                : _controller.play();
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}
