package background.commands;

import java.util.ArrayList;

import background.CommandManager;

public class Cycle extends IfSegment {
	private String type;
	public Operation init;
	public Operation increment;	
	public Boolean incrementFirst = false;
	
	public Cycle(String t, CommandManager cm) { super(cm); type = t; }
	public Cycle(String t, Boolean viz,  CommandManager cm) { super(viz, cm); type = t; }
		
	@Override
	public String getType() {
		return type;
	}

	
	
	public void applyCommand() {
		
		switch (type) {
		case "For": {						
			increment.codeLine = this.codeLine;
			init.codeLine = this.codeLine;
			increment.vizualize = false;
			
			Cycle w = (Cycle) commandManager.getCommand("While", vizualize);
			w.condition = this.condition;
			w.codeLine = this.codeLine;
			
			w.innerCommands = new ArrayList<Command>(this.innerCommands);
			//if (incrementFirst) w.innerCommands.add(0, increment);
			//else
				w.innerCommands.add(increment);		
			
			commandManager.appendCommand(w);			
			commandManager.appendCommand(init);
			
			break;
			}
		case "While": {
			if (condition.getTreeValue()) {
				Cycle w = (Cycle) commandManager.getCommand("While", vizualize);
				w.condition = this.condition;
				w.innerCommands = this.innerCommands;
				w.codeLine = this.codeLine;
				
				commandManager.appendCommand(w);				
				addInnerCommands();				
				}	
				break;
			}
		case "Repeat": {
			addInnerCommands();
			while (!condition.getTreeValue()) {
				applyInnerCommands();
			}	
			break;
			}

		}
	}


}
