import 'dart:ui';

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:tangheem/extensions/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.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 = true;
  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 BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 4, sigmaY: 4),
      child: Dialog(
        insetPadding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 24.0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        elevation: 0,
        backgroundColor: Colors.transparent,
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Container(
              width: MediaQuery.of(context).size.width * 273 / 375,
              margin: EdgeInsets.only(top: 11),
              decoration: BoxDecoration(
                color: ColorConsts.darkText.withOpacity(.60),
                borderRadius: BorderRadius.circular(16),
              ),
              padding: EdgeInsets.only(top: 24, bottom: 12),
              //    padding: EdgeInsets.symmetric(vertical: MediaQuery.of(context).orientation == Orientation.portrait ? 24 : 24, horizontal: 0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  MediaQuery.of(context).orientation == Orientation.portrait ? videoPlayer() : Expanded(child: videoPlayer()),
                  12.height,
                  SizedBox(
                    width: double.infinity,
                    height: 30,
                    child: TextButton(
                      onPressed: () async {
                        SharedPreferences prefs = await SharedPreferences.getInstance();
                        await prefs.setBool(GlobalConsts.doNotShowWelcomeVideo, doNotShowAgain);
                        Navigator.pop(context);
                      },
                      style: TextButton.styleFrom(
                        //      primary: Colors.white,
                        padding: EdgeInsets.all(0),
                        backgroundColor: Colors.transparent,
                        textStyle: TextStyle(fontSize: 14, fontFamily: "DroidKufi"),
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25.0), side: BorderSide(color: Colors.white, width: 2)),
                      ),
                      child: "لا تظهر مرة أخرى".toText(14),
                    ),
                  ).paddingOnly(left: 12, right: 12),
                ],
              ),
            ),
            Container(
              height: 22,
              width: 22,
              decoration: BoxDecoration(shape: BoxShape.circle, color: ColorConsts.brownLightColor),
              child: Icon(
                Icons.clear,
                size: 16,
                color: Colors.white,
              ),
            ).onPress(() => Navigator.pop(context)),
          ],
        ),
      ),
    );
  }

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