import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ongbut_ntmanh/widgets/widgets_util.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
import '../../../../const/colors.dart';


class PlayVideoPage extends StatefulWidget {
  String link;
  bool clicked = true;

  PlayVideoPage({required this.link,Key? key}) : super(key: key);
  @override
  State<PlayVideoPage> createState() => _PlayVideoPageState();
}

class _PlayVideoPageState extends State<PlayVideoPage> {
  late YoutubePlayerController _controller;
  bool clicked = true;
  // 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 Scaffold(
      appBar: AppBar(title: Text(YoutubePlayer.convertUrlToId(widget.link).toString())),
      body: Column(
        children: [
          Container(
            decoration: const BoxDecoration(
              color: AppColors.orange,
            ),
            height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.3,
            width: double.maxFinite,
            child: 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) => Container(child: player)),
          ),
          Column(children: [
            DefaultTabController(
              length: 6,
              child: TabBar(
                indicatorColor: AppColors.primary,
                onTap: (index) {
                },
                tabs: [
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(0),
                            //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: const Text('Bai Giang',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15),

                            //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: const Text('Vo Viet',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15),

                            //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: Text('Bai Tap',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),

                          //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: Text('Dap An',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),

                          //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: Text('ViDeo',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                  Tab(
                    child: Container(
                        alignment: Alignment.center,
                        height: 35,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),

                          //color: clicked==true?AppColors.orange:AppColors.primary
                        ),
                        child: Text('Thao Luan',style: TextStyle(color: AppColors.primary_light,fontSize: 10),
                            softWrap: false,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis)
                    ),
                  ),
                ]),
            ),
            Container(
              decoration: const BoxDecoration(
                color: AppColors.white,
              ),
              height: checkLandscape(context)?getHeight(context)*0.6:getHeight(context)*0.5,
              width: double.maxFinite,

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