import 'package:flutter/material.dart';

import 'parabolic_animation_widget.dart';
import 'popup_animation_widget.dart';

class AnimationPointManager {
  List<AnimatedWidget> list = [];
  static AnimationController controller1;
  static AnimationController controller2;

  Future<void> addParabolicAniamtion({
    @required TickerProvider vsync,
    @required GlobalKey stackKey,
    @required GlobalKey startKey,
    @required GlobalKey endKey,
    @required Duration duration,
    @required AnimationStatusListener statusListener,
    Color color = Colors.red,
    double size = 20,
    Offset startAdjustOffset = Offset.zero,
    Offset endAdjustOffset = Offset.zero,
  }) async {
    controller1 = createController(vsync, duration);
    Animation animation = createAnimation(controller1);

    AnimatedWidget animatedWidget = ParabolicAnimationWidget(
      animation: animation,
      stackKey: stackKey,
      startKey: startKey,
      endKey: endKey,
      size: size,
      color: color,
      startAdjustOffset: startAdjustOffset,
      endAdjustOffset: endAdjustOffset,
    );
    list.add(animatedWidget);
    statusListener(AnimationStatus.dismissed);

    try {
      await controller1.forward().orCancel;
      list.remove(animatedWidget);
      controller1.dispose();
      print('Controller1 disposed');
    } on TickerCanceled {
      print("Ticker Canceled");
    } catch (error) {
      print('Error: $error');
    }

    statusListener(AnimationStatus.completed);
  }

  Future<void> addPopupAniamtion({
    @required TickerProvider vsync,
    @required GlobalKey stackKey,
    @required GlobalKey startKey,
    @required Widget child,
    Duration duration,
    Offset popupOffset = Offset.zero,
    AnimationStatusListener statusListener,
  }) async {
    controller2 = createController(vsync, duration);

    AnimatedWidget animatedWidget = PopupAnimationWidget(
      animation: controller2.view,
      stackKey: stackKey,
      startKey: startKey,
      child: child,
      popupOffset: popupOffset,
    );
    list.add(animatedWidget);
    statusListener(AnimationStatus.dismissed);

    try {
      await controller2.forward().orCancel;
      await controller2.reverse().orCancel;
      list.remove(animatedWidget);
      controller2.dispose();
      print('Controller2 disposed');
    } on TickerCanceled {
      print("Ticker Canceled");
    } catch (error) {
      print('Error: $error');
    }

    statusListener(AnimationStatus.completed);
  }

  static AnimationController createController(
      TickerProvider vsync, Duration duration) {
    AnimationController ani = AnimationController(
        lowerBound: 0,
        upperBound: 1,
        duration: duration ?? Duration(milliseconds: 800),
        vsync: vsync);
    return ani;
  }

  static CurvedAnimation createAnimation(controller) {
    return CurvedAnimation(parent: controller, curve: Curves.linear);
  }

  void dispose() {
    try {
      controller1?.dispose();
      controller2?.dispose();
    } catch (error) {

    }
  }
}