package background.commands;

import background.CommandManager;
import background.systemVariables.SimpleVariable;

public class Write extends Command {
	
	public String name;	
	public String value = "";
	public Boolean array = false;
	public Boolean table = false;

	public Write(CommandManager cm) {super(cm); }
	public Write(Boolean viz, CommandManager cm) {super(viz, cm);}

	@Override
	public String getType() {
		return "Write";
	}
	
	public void setIndex(String s) {
		index = expressionInterpreter.getMath(s);
	}
	
	@Override
	public void applyCommand() {	
		SimpleVariable var;
		if (value.isEmpty()) {
			if (table) { 
				var = variableManager.getVariableByName(name+"["+Integer.toString(index.getTreeValue().intValue())+"]["+Integer.toString(index2.getTreeValue().intValue()) + "]");				
				if (!var.initialized) {
					this.variableManager.errorHandler.errorHappened("Initialized");
				} else commandManager.ioManager.addOutput(var.getValue("string"));					
				variableManager.makeActive(var);
			} else if (array) { 
				var = variableManager.getVariableByName(name+"["+Integer.toString(index.getTreeValue().intValue())+"]");
				if (!var.initialized) {
					this.variableManager.errorHandler.errorHappened("Initialized");
				} else commandManager.ioManager.addOutput(var.getValue("string"));
				variableManager.makeActive(var);
			} else {
				var = variableManager.getVariableByName(name);
				if (!var.initialized) {
					this.variableManager.errorHandler.errorHappened("Initialized");
				} else commandManager.ioManager.addOutput(var.getValue("string"));
				variableManager.makeActive(var);
			}
		} else commandManager.ioManager.addOutput(value);
	}
	
	
	
}
