package de.prob2.ui.consoles.groovy;

import javax.script.ScriptEngine;

import com.google.inject.Inject;

import de.prob.scripting.ScriptEngineProvider;

import de.prob2.ui.consoles.ConsoleExecResult;
import de.prob2.ui.consoles.ConsoleExecResultType;
import de.prob2.ui.consoles.ConsoleInstruction;
import de.prob2.ui.consoles.Executable;
import de.prob2.ui.consoles.groovy.codecompletion.CodeCompletionTriggerAction;
import de.prob2.ui.consoles.groovy.codecompletion.GroovyCodeCompletion;
import de.prob2.ui.consoles.groovy.objects.GroovyObjectStage;
import de.prob2.ui.internal.StageManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class GroovyInterpreter implements Executable {
	private static final Logger logger = LoggerFactory.getLogger(GroovyInterpreter.class);
	private final ScriptEngine engine;
	private final GroovyCodeCompletion codeCompletion;
	private final GroovyObjectStage groovyObjectStage;

	@Inject
	private GroovyInterpreter(final StageManager stageManager, final ScriptEngineProvider sep, final GroovyObjectStage groovyObjectStage) {
		engine = sep.get();
		this.groovyObjectStage = groovyObjectStage;
		this.codeCompletion = new GroovyCodeCompletion(stageManager, engine);
	}
	
	@Override
	public ConsoleExecResult exec(final ConsoleInstruction instruction) {
		logger.trace("Exec");
		if ("inspect".equals(instruction.getInstruction())) {
			groovyObjectStage.showObjects(engine);
			return new ConsoleExecResult("", "", ConsoleExecResultType.PASSED);
		} else if ("clear".equals(instruction.getInstruction())) {
			return new ConsoleExecResult("","", ConsoleExecResultType.CLEAR);
		} else {
			StringBuilder console = new StringBuilder();
			engine.put("__console", console);
			logger.trace("Eval {} on {}", instruction.getInstruction(), engine);
			String resultString;
			ConsoleExecResultType resultType = ConsoleExecResultType.PASSED;
			try {
				Object eval = engine.eval(instruction.getInstruction());
				resultString = eval.toString();
				logger.trace("Evaled {} to {}", instruction.getInstruction(), resultString);
			} catch (Throwable e) {
				logger.debug("Groovy Evaluation failed", e);
				resultString = e.toString();
				resultType = ConsoleExecResultType.ERROR;
			}
			return new ConsoleExecResult(console.toString(), resultString, resultType);
		}
	}
	
	public void setCodeCompletion(GroovyConsole parent) {
		codeCompletion.setParent(parent);
	}
	
	public void triggerCodeCompletion(String currentLine, CodeCompletionTriggerAction action) {
		if (!codeCompletion.isVisible()) {
			codeCompletion.activate(currentLine, action);
		}
	}
	
	public void triggerCloseCodeCompletion() {
		codeCompletion.deactivate();
	}
	
	public void closeObjectStage() {
		groovyObjectStage.close();
	}
}

@@ -1315,32 +1315,8 @@
 ) %7B%0A
-%09%09logger.trace(%22Exec%22);%0A
 %09%09if
@@ -1710,80 +1710,8 @@
 e);%0A
-%09%09%09logger.trace(%22Eval %7B%7D on %7B%7D%22, instruction.getInstruction(), engine);%0A
 %09%09%09S
@@ -1908,89 +1908,8 @@
 ();%0A
-%09%09%09%09logger.trace(%22Evaled %7B%7D to %7B%7D%22, instruction.getInstruction(), resultString);%0A
 %09%09%09%7D

Remove some debug logging calls from GroovyInterpreter