package gg.uhc.uhc.command;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.util.Arrays;
import java.util.Map;

public class SubcommandCommand implements CommandExecutor {

    protected final Map<String, CommandExecutor> subroutes = Maps.newHashMap();

    public void registerSubcommand(String name, CommandExecutor subcommand) {
        Preconditions.checkState(!subroutes.containsKey(name), "Subroute is already registered");

        subroutes.put(name.toLowerCase(), subcommand);
    }

    protected void sendUsage(CommandSender sender) {
        sender.sendMessage(ChatColor.RED + "This command requires an argument. Available: [" + Joiner.on(",").join(subroutes.keySet())+ "]");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            sendUsage(sender);
            return true;
        }

        CommandExecutor subcommand = subroutes.get(args[0]);

        if (subcommand == null) {
            sendUsage(sender);
            return true;
        }

        // cut subroute arg off and run subcommand
        return subcommand.onCommand(sender, command, label, Arrays.copyOfRange(args, 1, args.length));
    }
}

@@ -142,41 +142,100 @@
 ect.
-Maps;%0Aimport org.bukkit.ChatColor
+ImmutableList;%0Aimport com.google.common.collect.Lists;%0Aimport com.google.common.collect.Maps
 ;%0Aim
@@ -250,31 +250,25 @@
 .bukkit.
-command.Command
+ChatColor
 ;%0Aimport
@@ -287,31 +287,17 @@
 command.
-CommandExecutor
+*
 ;%0Aimport
@@ -312,29 +312,23 @@
 kit.
-command.CommandSender
+util.StringUtil
 ;%0A%0Ai
@@ -351,16 +351,39 @@
 Arrays;%0A
+import java.util.List;%0A
 import j
@@ -439,23 +439,19 @@
 lements 
-Command
+Tab
 Executor
@@ -503,24 +503,112 @@
 ecutor%3E 
-subrou
+commandExecutors = Maps.newHashMap();%0A    protected final Map%3CString, TabCompleter%3E tabComple
 te
+r
 s = Maps
@@ -675,34 +675,351 @@
 me, 
-CommandExecutor subcommand
+TabExecutor tabExecutor) %7B%0A        this.registerSubcommand(name, tabExecutor, tabExecutor);%0A    %7D%0A%0A    public void registerSubcommand(String name, CommandExecutor executor) %7B%0A        this.registerSubcommand(name, executor, null);%0A    %7D%0A%0A    public void registerSubcommand(String name, CommandExecutor commandExecutor, TabCompleter tabCompleter
 ) %7B%0A
@@ -1052,24 +1052,31 @@
 kState(!
-subroute
+commandExecutor
 s.contai
@@ -1132,24 +1132,31 @@
         
-subroute
+commandExecutor
 s.put(na
@@ -1177,20 +1177,137 @@
 (), 
-sub
 command
-);
+Executor);%0A%0A        if (tabCompleter != null) %7B%0A            tabCompleters.put(name.toLowerCase(), tabCompleter);%0A        %7D
 %0A   
@@ -1478,24 +1478,31 @@
 %22).join(
-subroute
+commandExecutor
 s.keySet
@@ -1768,24 +1768,31 @@
 mmand = 
-subroute
+commandExecutor
 s.get(ar
@@ -2060,11 +2060,655 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public List%3CString%3E onTabComplete(CommandSender sender, Command command, String alias, String%5B%5D args) %7B%0A        if (args.length == 1) %7B%0A            List%3CString%3E potentials = Lists.newArrayList();%0A            StringUtil.copyPartialMatches(args%5B0%5D, commandExecutors.keySet(), potentials);%0A            return potentials;%0A        %7D else %7B%0A            TabCompleter subcommand = tabCompleters.get(args%5B0%5D);%0A%0A            if (subcommand == null) %7B%0A                return ImmutableList.of();%0A            %7D%0A%0A            return subcommand.onTabComplete(sender, command, alias, Arrays.copyOfRange(args, 1, args.length));%0A        %7D%0A    %7D
 %0A%7D%0A

add tab complete support to SubcommandCommand with passthrough to subcommands past arg 1