package Algorithms.Animated;

public class AnimationController {
	
	private Action next;
	private boolean continuous;
	private long lastTime;
	private long timeBetween;
	
	public AnimationController()
	{
		next = null;
		continuous = false;
		lastTime = 0;
		timeBetween = 0;
	}
	
	public Action getNextAction() throws InterruptedException
	{
		long old = lastTime;
		Action ret = null;
		synchronized( this ) {
			while( next == null )
				wait();
			lastTime = System.currentTimeMillis();
			ret = next;
		}
		if( !continuous )
			next = null;
		else
		{
			if( lastTime - old < timeBetween )
			{
				Thread.sleep( timeBetween - ( lastTime - old ) );
				System.out.println( "sleep: " + ( timeBetween - ( lastTime - old ) ) );
				lastTime = System.currentTimeMillis();
			}
		}
		return ret;
	}
	
	public void setContinuous( boolean c )
	{
		this.continuous = c;
	}
	
	public void setTimeBetween( long between )
	{
		timeBetween = between;
	}
	
	public void setNextAction( Action a )
	{
		next = a;
		synchronized( this ) {
			notify();
		}
	}
	
	public boolean isContinuous()
	{
		return continuous;
	}
}
