import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/consts.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class VideoPlayerWidget extends StatefulWidget {
  final String link;

  VideoPlayerWidget(this.link, {Key key}) : super(key: key);

  @override
  _VideoPlayerWidgetState createState() {
    return _VideoPlayerWidgetState();
  }
}

class _VideoPlayerWidgetState extends State<VideoPlayerWidget> {
  bool doNotShowAgain = false;
  YoutubePlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller = YoutubePlayerController(
      initialVideoId: widget.link,
      flags: YoutubePlayerFlags(
        autoPlay: true,
        mute: true,
      ),
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return Dialog(
      insetPadding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 24.0),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: Container(
        width: double.infinity,
        decoration: BoxDecoration(
          color: ColorConsts.primaryBlue,
          borderRadius: BorderRadius.circular(16),
        ),
        padding: EdgeInsets.symmetric(vertical: MediaQuery.of(context).orientation == Orientation.portrait ? 32 : 16, horizontal: 16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            MediaQuery.of(context).orientation == Orientation.portrait ? videoPlayer() : Expanded(child: videoPlayer()),
            SizedBox(height: 8),
            Row(
              children: [
                Expanded(
                  child: Text(
                    "لا تظهر مرة أخرى",
                    textAlign: TextAlign.right,
                    style: TextStyle(color: Colors.white),
                  ),
                ),
                SizedBox(width: 16),
                SizedBox(
                  width: 12,
                  height: 12,
                  child: Checkbox(
                    value: doNotShowAgain,
                    side: BorderSide(color: Colors.white),
                    activeColor: ColorConsts.secondaryPink,
                    onChanged: (value) {
                      setState(() {
                        doNotShowAgain = value;
                      });
                    },
                  ),
                ),
              ],
            ),
            SizedBox(height: 16),
            SizedBox(
              width: double.infinity,
              height: 40,
              child: TextButton(
                onPressed: () async {
                  if (doNotShowAgain) {
                    SharedPreferences prefs = await SharedPreferences.getInstance();
                    await prefs.setBool(GlobalConsts.doNotShowWelcomeVideo, doNotShowAgain);
                  }
                  Navigator.pop(context);
                },
                style: TextButton.styleFrom(
                  primary: Colors.white,
                  padding: EdgeInsets.all(2),
                  backgroundColor: ColorConsts.secondaryPink,
                  textStyle: TextStyle(fontSize: 14, fontFamily: "DroidKufi"),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6.0),
                  ),
                ),
                child: Text("أغلق"),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget videoPlayer() {
    return YoutubePlayer(
      controller: _controller,
      showVideoProgressIndicator: true,
    );
  }
}
