package com.robothand.highqualitybot.command;

import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

import java.util.Hashtable;

/**
 * Created by ethan on 6/19/17.
 */
public class HelpCommand extends Command {
    private final Hashtable<String, Command> commands;

    @Override
    public String[] getNames() {
        return new String[] {"help", "commands"};
    }

    @Override
    public String getDescription() {
        return null;
    }

    @Override
    public String getShortDesc() {
        return "Lists all available commands. Provide a specific command for more info.";
    }

    public HelpCommand() {
        commands = new Hashtable<>();
    }

    public Command addCommand(Command command) {
        commands.put(command.getNames()[0], command);
        return command;
    }

    @Override
    public void onCommand(MessageReceivedEvent event, String[] args) {
        MessageChannel channel = event.getChannel();

        if (args.length < 2) {
            StringBuilder message = new StringBuilder();
            message.append("Available commands:\n");

            for (String key : commands.keySet()) {
                Command command = commands.get(key);
                String[] names = command.getNames();
                String shortDesc = command.getShortDesc();

                message.append("**").append(names[0]).append("** - ");
                message.append((shortDesc != null) ? shortDesc : "Short description not available.").append("\n");
            }

            channel.sendMessage(message.toString()).queue();
        }
    }
}

@@ -425,11 +425,16 @@
 nds%22
+, %22h%22
 %7D;%0A
-
     
@@ -502,20 +502,168 @@
  return 
-null
+%22Lists all available commands. Provide the name of another command to get more information as well as%22 +%0A                %22usage.%5CnUsage: help %5Bcommand%5D%22
 ;%0A    %7D%0A
@@ -1580,24 +1580,24 @@
 d(%22** - %22);%0A
-
             
@@ -1710,24 +1710,1248 @@
          %7D%0A%0A
+            channel.sendMessage(message.toString()).queue();%0A        %7D else %7B%0A            StringBuilder message = new StringBuilder();%0A            Command command = commands.get(args%5B1%5D);%0A%0A            if (command == null) %7B%0A                message.append(%22Command %5C%22**%22).append(args%5B1%5D).append(%22**%5C%22 is unknown.%22);%0A            %7D else %7B%0A                String%5B%5D names = command.getNames();%0A                String description = command.getDescription();%0A%0A                message.append(%22**%22);%0A                int index = 0;%0A                for (String alias : command.getNames()) %7B%0A                    if (index == 0) %7B%0A                        message.append(alias);%0A                        if (names.length != 1) %7B%0A                            message.append(%22 (%22);%0A                        %7D%0A                    %7D else if (index == names.length - 1) %7B%0A                        message.append(alias).append(%22) %22);%0A                    %7D else %7B%0A                        message.append(alias).append(%22, %22);%0A                    %7D%0A%0A                    index++;%0A                %7D%0A                message.append(%22**: %22);%0A                message.append((description != null) ? description : %22Description not available.%22);%0A            %7D%0A%0A
             

add detailed help for commands