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

class VideoPlayer extends ModalRoute<void> {
  final String initialUrl;

  VideoPlayer({required this.initialUrl});

  @override
  Duration get transitionDuration => const Duration(milliseconds: 500);

  @override
  bool get opaque => false;

  @override
  bool get barrierDismissible => true;

  @override
  Color get barrierColor => Colors.black.withAlpha(210);

  @override
  String get barrierLabel => 'Video';

  @override
  bool get maintainState => true;

  late final VideoPlayerController _controller =
      VideoPlayerController.networkUrl(Uri.parse(initialUrl));
  late final ChewieController chewieController = ChewieController(
      videoPlayerController: _controller,
      autoPlay: true,
      looping: true,
      aspectRatio: 16 / 9);

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

  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    return Material(
      type: MaterialType.transparency,
      child: SafeArea(
        child: _buildOverlayContent(context),
      ),
    );
  }

  Widget _buildOverlayContent(BuildContext context) {
    return Stack(
      children: [
        Center(
          child: Chewie(
            controller: chewieController,
          ),
        ),
        Positioned(
            top: 16,
            left: 16,
            child: BackButton(
              color: Theme.of(context).colorScheme.onSurface,
            ))
      ],
    );
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    // You can add your own animations for the overlay content
    return FadeTransition(
      opacity: animation,
      child: ScaleTransition(
        scale: animation,
        child: child,
      ),
    );
  }
}
