/*
 * DefaultCommandRoute.java
 *
 * Copyright (c) 2014. Graham Howden <graham_howden1 at yahoo.co.uk>.
 *
 * This file is part of PluginFramework.
 *
 * PluginFramework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PluginFramework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PluginFramework.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.pluginframework.routing;

import com.publicuhc.pluginframework.routing.exception.CommandInvocationException;
import com.publicuhc.pluginframework.routing.proxy.MethodProxy;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.io.IOException;
import java.io.StringWriter;

public class DefaultCommandRoute implements CommandRoute
{

    private final MethodProxy proxy;
    private final OptionParser parser;
    private final String commandName;
    private final String permission;
    private final OptionSpec helpSpec;

    public DefaultCommandRoute(String commandName, String permission, MethodProxy proxy, OptionParser parser, OptionSpec helpSpec)
    {
        this.helpSpec = helpSpec;
        this.commandName = commandName;
        this.proxy = proxy;
        this.parser = parser;
        this.permission = permission.equals(CommandMethod.NO_PERMISSIONS) ? null : permission;
    }

    @Override
    public OptionParser getOptionDetails()
    {
        return parser;
    }

    @Override
    public MethodProxy getProxy()
    {
        return proxy;
    }

    private void printHelpFor(CommandSender sender)
    {
        //catch the option exceptions and print an error message out with the option syntax
        StringWriter writer = new StringWriter();
        try {
            parser.printHelpOn(writer);
            sender.sendMessage(writer.toString());
        } catch(IOException ioex) {
            //shouldn't run, if it does the world probably exploded
            ioex.printStackTrace();
        }
    }

    @Override
    public void run(Command command, CommandSender sender, String[] args) throws CommandInvocationException
    {
        try {
            OptionSet optionSet = parser.parse(args);
            if(optionSet.has(helpSpec)) {
                printHelpFor(sender);
            } else {
                proxy.invoke(new CommandRequest(command, sender, optionSet));
            }
        } catch(OptionException e) {
            printHelpFor(sender);
        } catch(Throwable e) {
            throw new CommandInvocationException("Exception thrown when running the command " + command.getName(), e);
        }
    }

    @Override
    public String getCommandName()
    {
        return commandName;
    }

    @Override
    public String getPermission() {
        return permission;
    }
}

@@ -1126,16 +1126,45 @@
 onSpec;%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -1539,16 +1539,80 @@
 elpSpec;
+%0A    private final Class%3C? extends CommandSender%3E allowedSender;
 %0A%0A    pu
@@ -1674,16 +1674,62 @@
 mission,
+ Class%3C? extends CommandSender%3E allowedSender,
  MethodP
@@ -1858,24 +1858,68 @@
 ommandName;%0A
+        this.allowedSender = allowedSender;%0A
         this
@@ -2828,24 +2828,209 @@
 ption%0A    %7B%0A
+        if(!allowedSender.isAssignableFrom(sender.getClass())) %7B%0A            sender.sendMessage(ChatColor.RED + %22You cannot run that command from here!%22);%0A            return;%0A        %7D%0A
         try 

add allowed senders to the route