package com.danitheskunk.skunkworks.nodes;

import java.util.ArrayList;
import java.util.List;

public class Tweener {
	private int currentFrame;
	private final List<BaseTween> tweens;

	public Tweener() {
		tweens = new ArrayList<>();
		currentFrame = 0;
	}

	public void add(BaseTween tween) {
		tweens.add(tween);
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void tick() {
		var toRemove = new ArrayList<BaseTween>();
		for(var tween : tweens) {
			if(currentFrame >= tween.startFrame &&
				currentFrame <= tween.endFrame) {
				var len = tween.endFrame - tween.startFrame;
				var off = currentFrame - tween.startFrame;
				var progress = (double) off / (double) len;
				tween.setProgress(progress);
				tween.apply();
			}
			if(currentFrame == tween.endFrame) {
				toRemove.add(tween);
			}
		}
		for(var tween : toRemove) {
			if(tween.thenFunc != null) {
				tween.thenFunc.run();
			}
			tweens.remove(tween);
		}
		++currentFrame;
	}
}
