import 'package:flutter/cupertino.dart';
import 'package:guru_utils/log/log.dart';
import 'package:guru_utils/visual_feast/visual_feast_animation.dart';

/// Created by Haoyi on 2022/3/17

class VisualFeastSink {
  bool lastIsRunning = false;
  bool isRunning = false;

  VoidCallback? onIdle;

  final List<BaseVisualFeastAnimation> animations = [];

  VisualFeastSink({this.onIdle});

  void join(BaseVisualFeastAnimation animation) {
    animations.add(animation);
  }

  void complete() {
    final List<BaseVisualFeastAnimation> _temp = [];
    for (var animation in animations) {
      if (!animation.isCompleted) {
        _temp.add(animation);
      }
    }
    animations.clear();
    animations.addAll(_temp);

    lastIsRunning = isRunning;
    isRunning = animations.isNotEmpty;
    if (lastIsRunning != isRunning) {
      Log.e("hasRunningAnimation $isRunning");
      if (!isRunning) {
        onIdle?.call();
      }
    }
  }
}
