package background.commands;


import background.CommandManager;
import background.expressions.*;
import background.systemVariables.ArrayVariable;
import background.systemVariables.SimpleVariable;
import background.systemVariables.TableVariable;

public class Operation extends Command {	
	public MathTree mathValue;
	public LogicalTree logicValue;
	public StringProcess stringValue;
	public String name;
	public Boolean array = false;
	public Boolean table = false;
	public String constantBool;

	public Operation(Boolean viz, CommandManager cm) { super(viz, cm); }
	public Operation(CommandManager cm) { super(cm); }	
	
	@Override
	public String getType() {
		return "Operation";
	}

	
	public void decomposeValue(String s, String name) {	
		mathValue = expressionInterpreter.getMath(s);		 
		logicValue = expressionInterpreter.getLogic(s); 
		stringValue = expressionInterpreter.getString(s);
	}
	
	@Override
	public void applyCommand() {		
		String type = "";
		if (table) {TableVariable table = variableManager.getTableVariableByName(name); if (table!=null) type = table.getType(); }
		else if (array) {ArrayVariable array = variableManager.getArrayVariableByName(name); if (array!=null) type = array.getType(); }
		else {SimpleVariable var = variableManager.getVariableByName(name); if (var!=null) type = var.getType(); }
		if (!type.isEmpty()) {
			String s = "";
			
			switch (type) {
				case "boolean": s = logicValue.getTreeValue().toString(); break; 
				case "int": s = mathValue.getTreeValue().toString(); break;
				case "string": s = stringValue.getStringValue(); break;
			}
			
			if (table) variableManager.updateTableVariable(name, index2.getTreeValue().intValue(), index.getTreeValue().intValue(), s);
			else if (array) variableManager.updateArrayVariable(name, index.getTreeValue().intValue(), s);
			else variableManager.updateVariable(name, s);
		}
	}

	
}
