package me.despawningbone.discordbot.command.misc;

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

import me.despawningbone.discordbot.command.Command;
import me.despawningbone.discordbot.command.CommandResult;
import me.despawningbone.discordbot.command.CommandResult.CommandResultType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;

public class Choose extends Command {
	public Choose() {
		this.desc = "Let me choose the best for you!";
		this.usage = "<choice> | <choice> [...]";
		this.examples = Arrays.asList("eggs | ham | sausage | tomato");
	}

	@Override  //tags might get echoed if someone did "markdown injection" lmao  //nvm fixed
	public CommandResult execute(TextChannel channel, User author, Message msg, String[] args) {
		if (args.length < 1) {
			return new CommandResult(CommandResultType.INVALIDARGS, "Please enter something for me to choose from lol");
		}
		//String stripped = String.join(" ", args);
		String stripped = msg.getContentStripped().replaceAll("\\s\\s+", " ").split(" ", 3)[2];
		if (stripped.contains("|")) {
			String[] split = stripped.split(" *\\| *");
			Random randomno = new Random();
			//System.out.println(split.length);
			if (split.length < 2 || (split.length == 2 && (split[0].isEmpty() || split[1].isEmpty()))) {
				return new CommandResult(CommandResultType.INVALIDARGS, "At least put 2 choices for me to choose from lol");
			} else if (Arrays.asList(split).contains("")) {
				return new CommandResult(CommandResultType.INVALIDARGS, "Please do not input empty choices.");
			}
			int ran = randomno.nextInt(split.length);
			channel.sendMessage("<@!" + author.getId() + ">: If I were you, I would have chosen `" + split[ran]
					+ "` :stuck_out_tongue:").queue();
			return new CommandResult(CommandResultType.SUCCESS);
		} else {
			return new CommandResult(CommandResultType.INVALIDARGS, "At least put 2 choices for me to choose from lol");
		}
	}

}
