package core.commands;

import java.awt.Color;
import java.util.List;

import core.entities.Server;
import core.exceptions.InvalidUseException;
import core.util.Utils;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed.Field;

public class CmdHelp extends Command {

	public CmdHelp(Server server) {
		super(server);
	}

	@Override
	public Message execCommand(Member caller, String[] args) {
		String prefix = "!";
		EmbedBuilder embedBuilder = new EmbedBuilder();
		
		embedBuilder.setColor(Color.green);
		
		if (args.length == 0) {
			List<ICommand> cmdList = server.getCommandManager().getCommandList();
			
			cmdList.sort((ICommand c1, ICommand c2) -> c1.getName().compareTo(c2.getName()));
			
			String regCmds = "";
			String adminCmds = "";
			String customCmds = "";
			
			for(ICommand cmd : cmdList){
				String helpLine = String.format("%s%s - %s%n", prefix, cmd.getName(), cmd.getDescription());
				
				if(cmd.getClass() == CustomCommand.class){
					customCmds += helpLine;
				}else if(cmd.isAdminRequired()){
					adminCmds += helpLine;
				}else{
					regCmds += helpLine;
				}
			}
			
			regCmds += customCmds;
			
			embedBuilder.addField(new Field("Commands:", regCmds,false));
			
			if(server.isAdmin(caller)){
				embedBuilder.addField(new Field("Admin Commands:", adminCmds, false));
			}
			
			caller.getUser().openPrivateChannel().complete().sendMessage(embedBuilder.build()).queue();
			
			return Utils.createMessage("`Help sent`");
			
		} else {
			String cmdName = args[0];
			
			if(!server.getCommandManager().doesCommandExist(cmdName)){
				throw new InvalidUseException("Command does not exist");
			}
			
			ICommand cmd = server.getCommandManager().getCommand(cmdName);
			
			
			String help = String.format("%s%n%n%s", cmd.getDescription(), cmd.getHelp());
			embedBuilder.addField(new Field(cmd.getName(), help, false));
			
			return new MessageBuilder().setEmbed(embedBuilder.build()).build();
		}
	}

	@Override
	public boolean isAdminRequired() {
		return false;
	}

	@Override
	public boolean isGlobalCommand() {
		return true;
	}

	@Override
	public String getName() {
		return "Help";
	}

	@Override
	public String getDescription() {
		return "Gives information about this bot's commands and functions";
	}

	@Override
	public String getHelp() {
		return  getBaseCommand() + " - Lists all available commands\n" +
				getBaseCommand() + " <command name> - Lists information about a specific command";
	}
}

@@ -880,23 +880,29 @@
 %09String 
-regCmds
+currentLetter
  = %22%22;%0A%09
@@ -910,22 +910,17 @@
 %09String 
-adminC
+c
 mds = %22%22
@@ -928,30 +928,46 @@
 %0A%09%09%09
-String customCmds = %22%22
+boolean admin = server.isAdmin(caller)
 ;%0A%09%09
@@ -1039,18 +1039,22 @@
 rmat(%22%25s
-%25s
+**%25s**
  - %25s%25n%22
@@ -1109,184 +1109,142 @@
 %09%09%09%09
-%0A%09%09%09%09if(cmd.getClass() == CustomCommand.class)%7B%0A%09%09%09%09%09customCmds += helpLine;%0A%09%09%09%09%7Delse if(cmd.isAdminRequired())%7B%0A%09%09%09%09%09adminCmds += helpLine;%0A%09%09%09%09%7Delse%7B%0A%09%09%09%09%09regCmds += helpL
+String letter = String.valueOf(cmd.getName().charAt(0)).toUpperCase();%0A%09%09%09%09%0A%09%09%09%09%0A%09%09%09%09if(cmd.isAdminRequired() && !admin)%7B%0A%09%09%09%09%09cont
 in
+u
 e;%0A%09
@@ -1254,46 +1254,53 @@
 %7D%0A%09%09
-%09%7D%0A
 %09%09
-%09
 %0A%09%09%09
-regCmds += customCmds;
+%09if(!currentLetter.equals(letter))%7B
 %0A%09%09%09
-%0A%09
 %09%09em
@@ -1333,29 +1333,29 @@
 eld(
-%22Commands:%22, regC
+currentLetter, c
 mds,
+ 
 fals
@@ -1366,123 +1366,129 @@
 %0A%09%09%09
-%0A%09%09%09if(server.isAdmin(caller))%7B%0A%09%09%09%09embedBuilder.addField(new Field(%22Admin Commands:%22, adminCmds, false));%0A%09%09%09%7D%0A%09%09%09
+%09%09cmds = %22%22;%0A%09%09%09%09%09currentLetter = letter;%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09cmds += helpLine;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09embedBuilder.setTitle(%22Commands%22);
 %0A%09%09%09

Update no args help response