import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class PlayVideoPage extends StatefulWidget {
  String link;
  PlayVideoPage({required this.link,Key? key}) : super(key: key);

  @override
  State<PlayVideoPage> createState() => _PlayVideoPageState();
}

class _PlayVideoPageState extends State<PlayVideoPage> {

  late YoutubePlayerController _controller;
  // late TextEditingController _idController;
  // late TextEditingController _seekToController;

  // final List<String> _ids = [
  //   'nPt8bK2gbaU',
  // ];
  @override
  void initState() {
    super.initState();
    _controller =
        YoutubePlayerController(

      // initialVideoId: "nPt8bK2gbaU",
      // initialVideoId: widget.link,
      initialVideoId: YoutubePlayer.convertUrlToId(widget.link).toString(),

      flags: const YoutubePlayerFlags(
        mute: false,
        autoPlay: true,
        disableDragSeek: false,
        loop: false,
        isLive: false,
        forceHD: false,
        enableCaption: true,

      ),
    );
  }



  @override
  void deactivate() {
    // Pauses video while navigating to next page.
    _controller.pause();
    super.deactivate();
  }

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

  @override
  Widget build(BuildContext context) {
    return

      YoutubePlayerBuilder(
      onExitFullScreen: () {
        SystemChrome.setPreferredOrientations(DeviceOrientation.values);
      },
      player: YoutubePlayer(
        controller: _controller,
        showVideoProgressIndicator: true,
        progressIndicatorColor: Colors.blueAccent,
        // onReady: () {
        //   _isPlayerReady = true;
        // },
        // onEnded: (data) {
        //   _controller
        //       .load(_ids[(_ids.indexOf(data.videoId) + 1) % _ids.length]);
        // },
      ),
      builder: (context, player) => Scaffold(
        appBar: AppBar(title: Text(YoutubePlayer.convertUrlToId(widget.link).toString()),),
        body: Container(child: player),
      ),
    );
  }
}
