import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:tanami_app/core/routes/routes.dart';
import 'package:video_player/video_player.dart';

class VimeoScreen extends StatefulWidget {
  final String videoUrl;
  const VimeoScreen({
    super.key,
    required this.videoUrl,
  });

  @override
  State<VimeoScreen> createState() => _VimeoScreenState();
}

class _VimeoScreenState extends State<VimeoScreen> {
  late VideoPlayerController videoPlayerController;
  late ChewieController chewieController;
  var videoUrl1 =
      "https://player.vimeo.com/progressive_redirect/playback/848263896/rendition/1080p/file.mp4?loc=external&signature=440740807e0632840f08b973014d831dbb8d17516532a30d24bf10b44b9282cd";
  @override
  void initState() {
    _initializePlayer();
    super.initState();
  }

  _initializePlayer() async {
    try {
      videoPlayerController = VideoPlayerController.networkUrl(
        Uri.parse(videoUrl1),
      );

      await videoPlayerController.initialize();
      initChewie();
      setState(() {});
    } catch (e) {
      print("Error initializing video player: $e");
    }
  }

  initChewie() {
    chewieController = ChewieController(
        videoPlayerController: videoPlayerController,
        autoPlay: true,
        looping: false);
  }

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

  @override
  Widget build(BuildContext context) {
    return videoPlayerController.value.isInitialized
        ? Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              backgroundColor: Colors.transparent,
              leading: InkWell(
                onTap: () => goRouter.pop(),
                child: const Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                ),
              ),
            ),
            // CommonAppbar(titleTxt: ""),
            body: Center(
              child: AspectRatio(
                aspectRatio: videoPlayerController.value.aspectRatio,
                child: Chewie(
                  controller: chewieController,
                ),
              ),
            ),
          )
        : const Center(
            child:
                //ShimmerCommon(),
                CircularProgressIndicator(
              color: Color(0xFF0093FF),
            ),
          );
  }
}
