import 'dart:async';

import 'package:aitrainer_app/library/custom_icon_icons.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

// ignore: must_be_immutable
class DialogPremium extends StatefulWidget {
  final VoidCallback onTap;
  final VoidCallback? onCancel;
  String? description, function, unlockedText;
  final int unlockRound;

  bool unlocked;

  DialogPremium(
      {Key? key,
      required this.unlockRound,
      this.description,
      required this.onTap,
      this.onCancel,
      required this.unlocked,
      this.unlockedText,
      required this.function})
      : super(key: key) {
    description = description ?? "";
    function = function ?? "";
  }

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

class _DialogPremiumState extends State<DialogPremium> with Trans {
  bool isStart = true;
  late Timer _timer;

  @override
  Widget build(BuildContext context) {
    setContext(context);
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(31),
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: contentBox(context),
    );
  }

  @override
  void initState() {
    isStart = true;
    _timer = Timer.periodic(
        Duration(milliseconds: 1000),
        (Timer timer) => setState(() {
              isStart = !isStart;
            }));
    super.initState();
  }

  contentBox(context) {
    return Stack(alignment: AlignmentDirectional.topStart, children: [
      Stack(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(left: 20, top: 24, right: 20, bottom: 30),
            margin: EdgeInsets.only(top: 30),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(24),
              boxShadow: [BoxShadow(color: Colors.black, offset: Offset(0, 10), blurRadius: 10)],
              image: DecorationImage(
                image: AssetImage('asset/image/WT_black_G_background.jpg'),
                fit: BoxFit.cover,
                alignment: Alignment.center,
              ),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SizedBox(
                  height: 5,
                ),
                Stack(
                  alignment: AlignmentDirectional.topEnd,
                  children: [
                    Text(
                      widget.unlocked ? t("Keep testing") : t("Go Premium") + "   ",
                      style: GoogleFonts.archivoBlack(
                        fontSize: widget.unlocked ? 20 : 24,
                        color: Colors.yellow[400],
                        shadows: <Shadow>[
                          Shadow(
                            offset: Offset(5.0, 5.0),
                            blurRadius: 12.0,
                            color: Colors.black54,
                          ),
                          Shadow(
                            offset: Offset(-3.0, 3.0),
                            blurRadius: 12.0,
                            color: Colors.black54,
                          ),
                        ],
                      ),
                    ),
                    !widget.unlocked
                        ? Positioned(
                            right: 3,
                            top: 0,
                            child: AnimatedSwitcher(
                                duration: Duration(milliseconds: 900),
                                //reverseDuration: Duration(milliseconds: 200),
                                transitionBuilder: (Widget child, Animation<double> animation) {
                                  return FadeTransition(child: child, opacity: animation);
                                },
                                child: isStart
                                    ? Icon(
                                        CustomIcon.star_2,
                                        color: Colors.yellow[300],
                                      )
                                    : Offstage() /*  Icon(
                                    CustomIcon.exclamation_circle,
                                    color: Colors.yellow[300],
                                  ) */
                                ))
                        : Offstage(),
                  ],
                ),
                SizedBox(
                  height: 35,
                ),
                Text(
                  widget.unlocked ? "" : t("Unleash your potential with WorkoutTest Premium!"),
                  style: GoogleFonts.inter(
                    fontSize: 14,
                    color: Colors.white,
                    shadows: <Shadow>[
                      Shadow(
                        offset: Offset(5.0, 5.0),
                        blurRadius: 12.0,
                        color: Colors.black54,
                      ),
                      Shadow(
                        offset: Offset(-3.0, 3.0),
                        blurRadius: 12.0,
                        color: Colors.black54,
                      ),
                    ],
                  ),
                  textAlign: TextAlign.center,
                ),
                SizedBox(
                  height: 15,
                ),
                getDescription(),
                SizedBox(
                  height: 62,
                ),
                Align(
                    alignment: Alignment.center,
                    child: GestureDetector(
                        onTap: () => widget.unlocked ? Navigator.of(context).pop() : Navigator.of(context).popAndPushNamed("salesPage"),
                        child: Stack(
                          alignment: Alignment.center,
                          children: [
                            Image.asset('asset/icon/gomb_orange_c.png', width: 100, height: 45),
                            Text(
                              t("OK"),
                              style: TextStyle(fontSize: 16, color: Colors.white),
                            ),
                          ],
                        ))),
              ],
            ),
          ),
        ],
      ),
      GestureDetector(
          onTap: () {
            if (widget.onCancel == null) {
              Navigator.of(context).pop();
            } else {
              widget.onCancel!();
            }
          },
          child: CircleAvatar(
            backgroundColor: Colors.transparent,
            radius: 28,
            child: Text(
              "X",
              style: GoogleFonts.archivoBlack(fontSize: 32, color: Colors.white54),
            ),
          )),
    ]);
  }

  RichText getDescription() {
    return RichText(
        textAlign: TextAlign.center,
        text: TextSpan(
            style: GoogleFonts.inter(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: Colors.white,
              shadows: <Shadow>[
                Shadow(
                  offset: Offset(5.0, 5.0),
                  blurRadius: 12.0,
                  color: Colors.black54,
                ),
                Shadow(
                  offset: Offset(-3.0, 3.0),
                  blurRadius: 12.0,
                  color: Colors.black54,
                ),
              ],
            ),
            children: getDescriptionText()));
  }

  List<TextSpan> getDescriptionText() {
    List<TextSpan> list = [];

    if (widget.unlockedText != null) {
      list.add(TextSpan(text: widget.unlockedText));
      return list;
    }
    list.add(TextSpan(text: t("The")));
    list.add(TextSpan(text: t(" ")));
    list.add(
      TextSpan(
        text: t(widget.function!),
        style: GoogleFonts.inter(
          fontSize: 14,
          fontWeight: FontWeight.bold,
          color: Colors.yellow[300],
          shadows: <Shadow>[
            Shadow(
              offset: Offset(5.0, 5.0),
              blurRadius: 12.0,
              color: Colors.black54,
            ),
            Shadow(
              offset: Offset(-3.0, 3.0),
              blurRadius: 12.0,
              color: Colors.black54,
            ),
          ],
        ),
      ),
    );
    list.add(TextSpan(text: t(" ")));
    list.add(TextSpan(text: t("feature is reachable after you finished")));
    list.add(TextSpan(text: t(" ")));
    list.add(
      TextSpan(
        text: widget.unlockRound == 1
            ? t("the first")
            : widget.unlockRound == 2
                ? t("the second")
                : t("the third"),
        style: GoogleFonts.inter(
          fontSize: 14,
          fontWeight: FontWeight.bold,
          color: Colors.yellow[300],
          shadows: <Shadow>[
            Shadow(
              offset: Offset(5.0, 5.0),
              blurRadius: 12.0,
              color: Colors.black54,
            ),
            Shadow(
              offset: Offset(-3.0, 3.0),
              blurRadius: 12.0,
              color: Colors.black54,
            ),
          ],
        ),
      ),
    );
    list.add(TextSpan(text: t(" ")));
    list.add(TextSpan(text: t("100% test circles")));

    return list;
  }

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