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

import 'api/youtube_classes.dart';

class YouTubeVideoPlayerScreen extends StatefulWidget {
  final YouTubeItems ytVideo;
  YouTubeVideoPlayerScreen({required this.ytVideo});
  @override
  _YouTubeVideoPlayerScreenState createState() => _YouTubeVideoPlayerScreenState();
}

class _YouTubeVideoPlayerScreenState extends State<YouTubeVideoPlayerScreen> {

  late YoutubePlayerController _controller;
  bool _fullScreen = false;
  @override
  void initState() {
    super.initState();
    _controller = new YoutubePlayerController(initialVideoId: widget.ytVideo.id!.videoId!);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //appBar: AppBar(),
      body: SafeArea(
        child: YoutubePlayerBuilder(
          player: YoutubePlayer(
            controller: _controller,
            topActions: [
              IconButton(icon: Icon(Icons.close),onPressed: (){
                if(this._fullScreen){
                  _controller.toggleFullScreenMode();
                }
                else{
                  Navigator.of(context).pop();
                }
              }),
            ],
          ),
          onEnterFullScreen: (){
            this._fullScreen = true;
          },
          onExitFullScreen: ()=>this._fullScreen = false,
          builder: (context,player){
            return Column(
              children: [
                player,
                Expanded(
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      children: [
                        Text(widget.ytVideo.snippet!.title!),
                        SizedBox(height: 10.0,),
                        Text(widget.ytVideo.snippet!.description!),                  
                      ],
                    ),
                  )
                ),

              ],
            );
          },
        ),
      )
    );
  }
}