package commands;

import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import utils.Constants;
import utils.DiscordID;
import utils.Emoji;

import java.awt.*;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

public class GuildCommandListener extends ListenerAdapter {
	private final Map<String, GuildAction> commands;

	public GuildCommandListener() {
		commands = new HashMap<>();
		addCommands();
	}

	@Override
	public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
		String command = event.getMessage().getContentRaw().split(" ", 2)[0].toLowerCase();
		if (commands.containsKey(command))
			commands.get(command).run(event);
	}

	private void addCommands() {
		addPollCommand();
		addPingCommand();
		addPoopCommand();
	}

	private GuildAction addPollCommand() {
		GuildAction action = new GuildAction() {
			public String getCommand() {
				return "poll";
			}

			public void run(GuildMessageReceivedEvent event) {
				String invalidMessage = "Invalid Poll Format. Please use the format `"
						+ Constants.PREFIX
						+ "poll yourquestion | option1 | option2 | option3 ...`";
				String[] command = event.getMessage().getContentRaw().split(" ", 2);
				if(command.length < 2) {
					event.getChannel().sendMessage(invalidMessage).queue();
					return;
				}
				String[] params = command[1].split("\\|");
				if(params.length < 3) {
					event.getChannel().sendMessage(invalidMessage).queue();
					return;
				} else if (params.length > 10) {
					event.getChannel().sendMessage("You can only have up to 9 different answers in a poll.").queue();
					return;
				}

				event.getMessage().delete().queue();
				EmbedBuilder msg = new EmbedBuilder();
				msg.setAuthor(event.getAuthor().getName() + " created a poll!", null, event.getAuthor().getEffectiveAvatarUrl());
	    		msg.setTitle(params[0], null);
	    		msg.setColor(Color.MAGENTA);

	    		StringBuilder answers = new StringBuilder();
	    		for(int i = 1; i < params.length; i++) {
					answers.append(Emoji.NUMBER[i]);
					answers.append(' ');
					answers.append(params[i].trim());
					answers.append('\n');
				}

	    		msg.setDescription(answers.toString());
	    		Message poll = event.getChannel().sendMessage(msg.build()).complete();
	    		for(int i = 1; i < params.length; i++) {
	    			poll.addReaction(Emoji.NUMBER[i]).complete();
	    		}
			}
		};
		commands.put(Constants.PREFIX + action.getCommand(), action);
		return action;
	}

	private GuildAction addPingCommand() {
		GuildAction action = new GuildAction() {
			public String getCommand() {
				return "pingg";
			}

			public void run(GuildMessageReceivedEvent event) {
				event.getChannel().sendMessage("Ping: ...").queue(m -> {
					long ping = event.getMessage().getCreationTime().until(m.getCreationTime(), ChronoUnit.MILLIS);
					m.editMessage("Ping: " + ping  + "ms | Websocket: " + event.getJDA().getPing() + "ms").queue();
				});
			}
		};
		commands.put(Constants.PREFIX + action.getCommand(), action);
		return action;
	}

	private GuildAction addPoopCommand() {
		GuildAction action = new GuildAction() {
			public String getCommand() {
				return "poop";
			}

			public void run(GuildMessageReceivedEvent event) {
				if (event.getAuthor().getId().equals(DiscordID.KAGA))
					event.getChannel().sendMessage("no").queue();
			}
		};
		commands.put(Constants.PREFIX + action.getCommand(), action);
		return action;
	}
}
@@ -400,16 +400,41 @@
 til.Map;
+%0Aimport java.util.Random;
 %0A%0Apublic
@@ -934,24 +934,44 @@
 gCommand();%0A
+%09%09addRollCommand();%0A
 %09%09addPoopCom
@@ -3229,32 +3229,768 @@
 urn action;%0A%09%7D%0A%0A
+%09private GuildAction addRollCommand() %7B%0A%09%09GuildAction action = new GuildAction() %7B%0A%09%09%09public String getCommand() %7B%0A%09%09%09%09return %22roll%22;%0A%09%09%09%7D%0A%0A%09%09%09public void run(GuildMessageReceivedEvent event) %7B%0A%09%09%09%09String%5B%5D command = event.getMessage().getContentRaw().split(%22 %22, 2);%0A%09%09%09%09int max = 100;%0A%09%09%09%09if(command.length %3E 1) %7B%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09max = Integer.parseInt(command%5B1%5D) + 1;%0A%09%09%09%09%09%7D catch(NumberFormatException ex) %7B%0A%09%09%09%09%09%09event.getChannel().sendMessage(%22Invalid format. Use %60!roll %5Bmax%5D%60. Example: %60!roll 1000%60%22).queue();%0A%09%09%09%09%09%09return;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09event.getChannel().sendMessage(%22You rolled a %22 + new Random().nextInt(max) + %22!%22).queue();%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09commands.put(Constants.PREFIX + action.getCommand(), action);%0A%09%09return action;%0A%09%7D%0A%0A
 %09private GuildAc

Add !roll command