package org.wanna.jabbot.extensions.script;

import org.wanna.jabbot.command.config.CommandConfig;
import org.wanna.jabbot.command.parser.args.NullArgParser;
import org.wanna.jabbot.command.parser.args.ArgsParser;
import org.wanna.jabbot.extensions.AbstractCommandAdapter;
import org.wanna.jabbot.command.CommandResult;
import org.wanna.jabbot.command.MessageWrapper;
import java.io.StringReader;


import com.sipstacks.script.*;


import java.util.Map;

/**
 * @author tsearle 
 * @since 2015-02-21
 */
public class ScriptCommand extends AbstractCommandAdapter {
	private int loopLimit = -1;
	private int bufferLimit = -1;

	public ScriptCommand(CommandConfig configuration) {
		super(configuration);
	}

	@Override
	public void configure(Map<String, Object> configuration) {
		if (configuration == null ) return;
		
		if (configuration.containsKey("loop_limit")) {
			loopLimit = Integer.parseInt(configuration.get("loop_limit").toString());
		}
		if (configuration.containsKey("buffer_limit")) {
			bufferLimit = Integer.parseInt(configuration.get("buffer_limit").toString());
		}
	}

        @Override
        public ArgsParser getArgsParser() {
                return new NullArgParser();
        }

	@Override
	public CommandResult process(MessageWrapper message) {
		String script = message.getArgs().get(0);

		Script s = new Script(new StringReader(script));
		if (loopLimit > 0) {
			s.setLoopLimit(loopLimit);
		}

		String response = null;

		try {
			response = s.run();

			if (bufferLimit > 0 && response.length() > bufferLimit) {
				response = response.substring(0, bufferLimit);
				response += "\n*Message Truncated*";
			}

		} catch (ScriptParseException spe) {
			CommandResult result = new CommandResult();
			result.setText(spe.getMessage());
			return result;
		}
		
		CommandResult result = new CommandResult();
		result.setText(response);
		return result;
	}

}

@@ -372,61 +372,474 @@
 ort 
-java.io.StringReader;%0A%0A%0Aimport com.sipstacks.script.*
+org.wanna.jabbot.command.behavior.CommandFactoryAware;%0Aimport org.wanna.jabbot.command.config.CommandConfig;%0Aimport org.wanna.jabbot.extensions.AbstractCommandAdapter;%0Aimport org.wanna.jabbot.command.MessageWrapper;%0Aimport org.wanna.jabbot.command.Command;%0Aimport org.wanna.jabbot.command.CommandFactory;%0Aimport java.io.StringReader;%0A%0A%0Aimport com.sipstacks.script.Script;%0Aimport com.sipstacks.script.ScriptParseException;%0Aimport com.sipstacks.script.ExternalFunction
 ;%0A%0A%0A
@@ -968,17 +968,89 @@
 Adapter 
-%7B
+ implements CommandFactoryAware %7B%0A%09private CommandFactory commandFactory;
 %0A%09privat
@@ -1904,16 +1904,694 @@
 );%0A%09%09%7D%0A%0A
+%09%09for(Command command : commandFactory.getAvailableCommands().values())%7B%0A%09%09%09s.addExternalFunction(command.getCommandName(), new ExternalFunction() %7B%0A%09%09%09%09%09%0A%09%09%09%09private Command cmd;%0A%09%09%09%09private String sender;%0A%09%09%09%09public ExternalFunction init(Command command, String sender) %7B%0A%09%09%09%09%09cmd = command;%0A%09%09%09%09%09this.sender = sender;%0A%09%09%09%09%09return this;%0A%09%09%09%09%7D%0A%0A%09%09%09%09public String run(String args) %7B%0A%09%09%09%09%09MessageWrapper msg = new MessageWrapper(null);%0A%09%09%09%09%09msg.setBody(args);%0A%09%09%09%09%09msg.setArgs(cmd.getArgsParser().parse(msg.getBody()));%0A%09%09%09%09%09msg.setSender(sender);%0A%09%09%09%09%09CommandResult result = cmd.process(msg);%0A%09%09%09%09%09return result.getText();%0A%09%09%09%09%7D%0A%09%09%09%7D.init(command, message.getSender()));%0A%09%09%7D%0A%0A%0A%0A
 %09%09String
@@ -3043,10 +3043,165 @@
 lt;%0A%09%7D%0A%0A
+        @Override%0A        public void setCommandFactory(CommandFactory commandFactory) %7B%0A                this.commandFactory = commandFactory;%0A        %7D%0A%0A%0A
 %7D%0A

add commands as functions