import 'dart:ui';

import 'package:guru_utils/log/log.dart';

/// Created by Haoyi on 2022/3/18

class VisualFeastExtras {
  final List<VisualFeastCompleter> bindCompleter;

  VisualFeastExtras(this.bindCompleter);

  void complete(int id) {
    for (var completer in bindCompleter) {
      completer.complete(id);
    }
  }
}

class VisualFeastCompleter {
  int flag = 0;

  bool get isRunning => flag != 0;

  double idleTick = 0;

  final VoidCallback? onComplete;

  VisualFeastCompleter({this.onComplete});

  void join(int id) {
    flag ^= id;
    Log.d("join:$id $flag");
    idleTick = 0;
  }

  void complete(int id) {
    flag ^= id;
    Log.d("complete:$id $flag");
    if (flag == 0) {
      idleTick = 0;
      onComplete?.call();
    }
  }

  void tick(double dt) {
    if (flag != 0) {
      idleTick += dt;
      if (idleTick > 8) {
        idleTick = 0;
        reset();
        onComplete?.call();
      }
    }
  }

  void reset() {
    flag = 0;
    Log.e("reset: $flag", tag: "VF");
  }
}
