/*
 	Copyright (c) 2014 code.fm

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
 */
package ductive.console.shell;


import java.io.IOException;
import java.io.PrintStream;

import javax.inject.Provider;

import org.apache.commons.lang3.StringUtils;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;

import ductive.commons.Names;
import ductive.console.commands.parser.CmdParser;
import ductive.console.commands.parser.model.CommandLine;
import ductive.console.commands.register.CommandContext;
import ductive.console.commands.register.CommandInvoker;
import ductive.console.config.StandardPromptProvider;
import ductive.parse.errors.NoMatchException;
import jline.console.UserInterruptException;

public class EmbeddedAppShell implements Shell {

	private static final Ansi DEFAULT_STANDARD_PROMPT = new Ansi().reset().bold().fg(Color.MAGENTA).a("app> ").reset();

	private static final String HISTORY_KEY = Names.from(EmbeddedAppShell.class,"history");

	private CommandInvoker commandInvoker;
	private HistoryProvider historyProvider;
	private Provider<CmdParser> cmdParserProvider;
	private StandardPromptProvider standardPromptProvider = new StandardPromptProvider() {
		@Override public Ansi get() {
			return DEFAULT_STANDARD_PROMPT;
		}
	};

	@Override
	public void execute(InteractiveTerminal terminal, TerminalUser user) throws IOException {
		CmdParser cmdParser = cmdParserProvider.get();
		try( ShellHistory history = historyProvider.history(HISTORY_KEY) ) {
			ShellSettings settings = new StaticShellSettings(standardPromptProvider,cmdParser,history);
			terminal.updateSettings(settings);

			while (true) {
				Integer result = execute(cmdParser,terminal,history,user);
				if(result != null)
					break;
			}
		}
	}

	private Integer execute(CmdParser cmdParser, InteractiveTerminal terminal, ShellHistory history, TerminalUser user) throws IOException {
		try {
			try {
				String command = terminal.readLine();

				if (command == null)
					return 0;

				if (StringUtils.isBlank(command))
					return null;

				CommandLine line = cmdParser.parse(command);
				//terminal.println(line.toString());
				commandInvoker.execute(new CommandContext(terminal,user),line);
			} catch(NoMatchException e) {
				terminal.errorln(e.getMessage());
			}
		} catch(UserInterruptException e) {
		} catch (Throwable e) {
			// Unroll invoker exceptions
			if (e.getClass().getCanonicalName().equals("org.codehaus.groovy.runtime.InvokerInvocationException")) {
				e = e.getCause();
			}

			PrintStream ps = new PrintStream(terminal.error());
			e.printStackTrace(ps);
			ps.flush();
		}
		return null;
	}

	public void setCmdParserProvider(Provider<CmdParser> cmdParserProvider) {
		this.cmdParserProvider = cmdParserProvider;
	}

	public void setCommandInvoker(CommandInvoker commandInvoker) {
		this.commandInvoker = commandInvoker;
	}

	public void setHistoryProvider(HistoryProvider historyProvider) {
		this.historyProvider = historyProvider;
	}

	public void setStandardPromptProvider(StandardPromptProvider standardPromptProvider) {
		this.standardPromptProvider = standardPromptProvider;
	}

}

@@ -2888,24 +2888,40 @@
 %7B%0A%09%09
-try %7B
+final CommandLine line;
 %0A%09%09
-%09
 try %7B%0A
-%09
 %09%09%09S
@@ -2957,25 +2957,24 @@
 Line();%0A%0A%09%09%09
-%09
 if (command 
@@ -2978,25 +2978,24 @@
 nd == null)%0A
-%09
 %09%09%09%09return 0
@@ -3000,17 +3000,16 @@
  0;%0A%0A%09%09%09
-%09
 if (Stri
@@ -3038,17 +3038,16 @@
 d))%0A%09%09%09%09
-%09
 return n
@@ -3059,21 +3059,8 @@
 %0A%09%09%09
-%09
-CommandLine 
 line
@@ -3094,190 +3094,170 @@
 ;%0A%09%09
-%09%09//terminal.println(line.toString());%0A%09%09%09%09commandInvoker.execute(new CommandContext(terminal,user),line);%0A%09%09%09%7D catch(NoMatchException e) %7B%0A%09%09%09%09terminal.errorln(e.getMessage());%0A%09%09%09%7D
+%7D catch(NoMatchException e) %7B%0A%09%09%09terminal.errorln(e.getMessage());%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09commandInvoker.execute(new CommandContext(terminal,user),line);
 %0A%09%09%7D

make sure io errors on read exit the loop