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

class ConfettiLayer {
  OverlayEntry? _overlayEntry;
  final TickerProvider tickerProvider;
  final int numberOfParticles;
  late ConfettiController controller;

  ConfettiLayer(this.tickerProvider, {this.numberOfParticles = 20}) : controller = ConfettiController(duration: const Duration(milliseconds: 200));

  play() {
    controller.play();
  }

  setup(BuildContext context) {
    if (_overlayEntry != null) return;
    _overlayEntry = OverlayEntry(
      builder: (context) {
        return IgnorePointer(
          child: Align(
            alignment: Alignment.center,
            child: ConfettiWidget(
              confettiController: controller,
              blastDirectionality: BlastDirectionality.explosive,
              numberOfParticles: numberOfParticles,
              gravity: 1,
              shouldLoop: false,
              maxBlastForce: 400,
              emissionFrequency: 1,
            ),
          ),
        );
      },
    );
    Overlay.of(context).insert(_overlayEntry!);
  }

  dispose() {
    _overlayEntry?.remove();
  }
}
