package me.despawningbone.discordbot.command.misc;

import java.util.Arrays;
import java.util.Random;

import me.despawningbone.discordbot.DiscordBot;
import me.despawningbone.discordbot.command.Command;
import me.despawningbone.discordbot.command.CommandResult;
import me.despawningbone.discordbot.command.CommandResult.CommandResultType;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.User;

public class Roll extends Command {
	public Roll() {
		this.desc = "Roll a dice!";
		this.usage = "[int][-int]";
		this.remarks = Arrays.asList("Possible parameter combinations are as follows:\n" +
		DiscordBot.prefix + "roll - this will return a value within normal dice range.\n" +
		DiscordBot.prefix + "roll [intbound]\n - this will return a value from 0 to the bound.\n" + 
		DiscordBot.prefix + "roll [intmin]-[intmax] - this will return a value within the range.\n");
		this.examples = Arrays.asList("", "100", "100-1000");
	}
	
	@Override
	public CommandResult execute(TextChannel channel, User author, Message msg, String[] args) {
		Random randomno = new Random();
		MessageCreateBuilder smsg = new MessageCreateBuilder();
		int num = 6;
		int range = 0;
		boolean no = false;
		if (args.length > 0) {
			String p1 = args[0];
			int n = 0;
			try {
				n = Integer.parseInt(p1);
			} catch (NumberFormatException e) {
				if (p1.equals("yourself")) { // easter egg
					smsg.addContent("*rolls*");
					no = true;
					n = 1;
				} else if (p1.contains("-")) {
					try {
						range = Integer.parseInt(p1.split("-")[0]);
						n = Integer.parseInt(p1.split("-")[1]);
					} catch (NumberFormatException e1) {
						smsg.addContent("Please enter a correct range.\n");
						no = true;
					}
				} else {
					smsg.addContent("Please enter a correct number.\n");
					no = true;
					n = 1;
				}
			}
			if (n > 100000000) {
				smsg.addContent("The number entered is too large.\n");
				no = true;
			}
			if (n < 1 && !no) {
				smsg.addContent("No number smaller than 1, please.\n");
				no = true;
			}
			if (!no) {
				num = n;
				if (num < range) {
					int buffer = num;
					num = range;
					range = buffer;
				}
				int dice = randomno.nextInt((num - range) + 1) + range;
				String ID = author.getId();
				smsg.addContent("<@!" + ID + ">" + " ,Your roll is: ");
				smsg.addContent(String.valueOf("`" + dice + "`" + "!"));
				MessageCreateData out = smsg.build();
				channel.sendMessage(out).queue();
				return new CommandResult(CommandResultType.SUCCESS);
			} else {
				return new CommandResult(CommandResultType.FAILURE, smsg.getContent());
			}
		} else {
			int dice = 0;
			if (range == 0) {
				dice = randomno.nextInt(num) + 1;
			} else {
				dice = randomno.nextInt((num - range) + 1) + range;
			}
			String ID = author.getId();
			smsg.addContent("<@!" + ID + ">" + " ,Your roll is: ");
			smsg.addContent(String.valueOf("`" + dice + "`" + "!"));
			MessageCreateData out = smsg.build();
			channel.sendMessage(out).queue();
			return new CommandResult(CommandResultType.SUCCESS);
		}
	}
}
