package Algorithms.Animated.BK;

import Algorithms.Animated.AlgorithmStage;
import Model.LayeredGraphNode;

/**
 * The stage where the for extremal layouts are computed.
 * @author kolja
 *
 */
public class ExtremalLayoutCalc implements AlgorithmStage {

	public enum LayoutType{
		TOP_BOTTOM_LEFT,
		TOP_BOTTOM_RIGHT,
		BOTTOM_TOP_LEFT,
		BOTTOM_TOP_RIGHT,
		COMBINED
	}
	
	private enum LayoutState
	{
		BLOCK_CALCULATION,
		COMPACTION
	}
	
	private BlockCalc bc;
	private Compaction cp;
	private LayoutState status;
	
	
	public ExtremalLayoutCalc( LayoutType typ, LayeredGraphNode graph )
	{
		status = LayoutState.BLOCK_CALCULATION;
		bc = new BlockCalc( graph, typ );
		cp = new Compaction( graph, typ );
	}
	
	@Override
	public StageStatus forwardStep() {
		if( status == LayoutState.BLOCK_CALCULATION )
		{
			if( bc.forwardStep() == StageStatus.FINISHED )
			{
				status = LayoutState.COMPACTION;
			}
			return StageStatus.UNFINISHED;
		}
		if( status == LayoutState.COMPACTION )
			return cp.forwardStep();
		return StageStatus.UNFINISHED;
	}

	@Override
	public StageStatus backwardStep() {
		if( status == LayoutState.BLOCK_CALCULATION )
			return bc.backwardStep();
		if( status == LayoutState.COMPACTION )
		{
			if( cp.backwardStep() == StageStatus.FINISHED )
				status = LayoutState.BLOCK_CALCULATION;
		}
		return StageStatus.UNFINISHED;
	}

}
