import 'package:confetti/confetti.dart';
import 'package:ezanimation/ezanimation.dart';
import 'package:flutter/material.dart';
import 'dart:math';

import 'package:flutter/scheduler.dart';

class VictoryConfetti extends StatefulWidget {
  @override
  _VictoryConfettiState createState() => _VictoryConfettiState();
}

class _VictoryConfettiState extends State<VictoryConfetti> {
  late ConfettiController _controllerBottomCenter;

  @override
  void initState() {
    _controllerBottomCenter = ConfettiController(duration: const Duration(seconds: 2));
    SchedulerBinding.instance!.addPostFrameCallback((_) {
      Future.delayed(Duration(milliseconds: 500)).then((value) => _controllerBottomCenter.play());
    });
    super.initState();
  }

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

  Path drawStar(Size size) {
    // Method to convert degree to radians
    double degToRad(double deg) => deg * (pi / 180.0);

    const numberOfPoints = 5;
    final halfWidth = size.width / 2;
    final externalRadius = halfWidth;
    final internalRadius = halfWidth / 2.5;
    final degreesPerStep = degToRad(360 / numberOfPoints);
    final halfDegreesPerStep = degreesPerStep / 2;
    final path = Path();
    final fullAngle = degToRad(360);
    path.moveTo(size.width, halfWidth);

    for (double step = 0; step < fullAngle; step += degreesPerStep) {
      path.lineTo(halfWidth + externalRadius * cos(step), halfWidth + externalRadius * sin(step));
      path.lineTo(halfWidth + internalRadius * cos(step + halfDegreesPerStep), halfWidth + internalRadius * sin(step + halfDegreesPerStep));
    }
    path.close();
    return path;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Align(
        alignment: Alignment.bottomCenter,
        child: ConfettiWidget(
          confettiController: _controllerBottomCenter,
          blastDirectionality: BlastDirectionality.explosive, // don't specify a direction, blast randomly
          numberOfParticles: 20,
          colors: const [Colors.green, Colors.blue, Colors.pink, Colors.orange, Colors.purple], // manually specify the colors to be used
          createParticlePath: drawStar, // define a custom shape/path.
        ),
      ),
    );
  }
}

class Victory extends StatefulWidget {
  final bool? victory;
  const Victory({this.victory});
  @override
  _VictoryState createState() => _VictoryState();
}

class _VictoryState extends State<Victory> {
  final EzAnimation animation = EzAnimation(1.0, 200.0, Duration(seconds: 3), reverseCurve: Curves.easeIn);

  @override
  void initState() {
    animation.start();
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        setState(() {});
      }
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: AnimatedBuilder(
            animation: animation,
            builder: (context, snapshot) {
              return Center(
                child: Container(
                  width: animation.value,
                  height: animation.value,
                  child: Row(children: [
                    VictoryConfetti(),
                    widget.victory != null && widget.victory == true ? Image.asset("asset/image/WT_cup_victory400.png") : Offstage(),
                  ]),
                ),
              );
            }));
  }
}
