package speiger.src.coreengine.rendering.guiOld.helper.animations;

import speiger.src.collections.objects.maps.impl.hash.Object2ObjectLinkedOpenHashMap;
import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.collections.objects.utils.maps.Object2ObjectMaps;
import speiger.src.coreengine.math.value.IValue;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.helper.animations.transitions.ITransition;

public class Animation
{
	Object2ObjectMap<AnimationTarget, ITransition> transitions = new Object2ObjectLinkedOpenHashMap<>();
	AnimationListener listener;
	float duration = 0F;
	
	public Animation add(AnimationTarget target, ITransition transition)
	{
		transitions.put(target, transition);
		duration += transition.getDuration();
		return this;
	}
	
	public Animation addListener(AnimationListener listener)
	{
		this.listener = listener;
		return this;
	}
	
	public AnimationInstance createInstance(GuiComponent comp, AnimationInstance old, float delay, boolean reverse)
	{
		Object2ObjectMap<AnimationTarget, IValue> values = new Object2ObjectLinkedOpenHashMap<>();
		for(Object2ObjectMap.Entry<AnimationTarget, ITransition> entry : Object2ObjectMaps.fastIterable(transitions))
		{
			AnimationTarget target = entry.getKey();
			float base = target.getBase(comp);
			values.put(target, entry.getValue().initValue(base, old != null ? old.getCurrentProgress(target) : (reverse ? entry.getValue().getHiddenValue() : base), reverse, delay, duration));
		}
		return new AnimationInstance(values, duration + delay, reverse, listener);
	}
	
	@FunctionalInterface
	public static interface AnimationListener
	{
		public void onAnimationFinished(Animation instance, GuiComponent owner, boolean reverse);
	}
}
