package com.avairebot.orion.commands;

import com.avairebot.orion.contracts.commands.AbstractCommand;
import net.dv8tion.jda.core.entities.Message;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommandHandler {

    private static final Map<List<String>, CommandContainer> COMMANDS = new HashMap<>();

    public static CommandContainer getCommand(AbstractCommand command) {
        for (Map.Entry<List<String>, CommandContainer> entry : COMMANDS.entrySet()) {
            if (entry.getValue().getCommand().isSame(command)) {
                return entry.getValue();
            }
        }

        return null;
    }

    public static CommandContainer getCommand(Message message) {
        return getCommand(message.getContent().split(" ")[0].toLowerCase());
    }

    public static CommandContainer getCommand(String commandTrigger) {
        for (Map.Entry<List<String>, CommandContainer> entry : COMMANDS.entrySet()) {
            for (String trigger : entry.getKey()) {
                if (commandTrigger.equalsIgnoreCase(entry.getValue().getDefaultPrefix() + trigger)) {
                    return entry.getValue();
                }
            }
        }

        return null;
    }

    public static boolean register(AbstractCommand command) {
        for (String trigger : command.getTriggers()) {
            for (Map.Entry<List<String>, CommandContainer> entry : COMMANDS.entrySet()) {
                if (entry.getKey().contains(trigger.toLowerCase())) {
                    return false;
                }
            }
        }

        Category category = Category.fromCommand(command);
        if (category == null) {
            return false;
        }

        COMMANDS.put(command.getTriggers(), new CommandContainer(command, category));
        return true;
    }

    public static Collection<CommandContainer> getCommands() {
        return COMMANDS.values();
    }
}

@@ -140,16 +140,58 @@
 Message;
+%0Aimport net.dv8tion.jda.core.utils.Checks;
 %0A%0Aimport
@@ -1713,24 +1713,25 @@
 mmand);%0A
+%0A
         
 if (cate
@@ -1726,67 +1726,244 @@
     
-if (category == null) %7B%0A            return false;%0A        %7D
+Checks.notNull(category, String.format(%22%25s :: %25s%22, command.getName(), %22Invalid command category, command category%22));%0A        Checks.notNull(command.getDescription(), String.format(%22%25s :: %25s%22, command.getName(), %22Command description%22));
 %0A%0A  

Add checks when registering commands to make sure they are valid