package com.tisawesomeness.minecord.command.player;

import java.awt.Color;
import java.util.Arrays;

import com.tisawesomeness.minecord.command.Command;
import com.tisawesomeness.minecord.database.Database;
import com.tisawesomeness.minecord.util.DateUtils;
import com.tisawesomeness.minecord.util.MessageUtils;
import com.tisawesomeness.minecord.util.NameUtils;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

public class UuidCommand extends Command {
	
	public CommandInfo getInfo() {
		return new CommandInfo(
			"uuid",
			"Gets the UUID of a player.",
			"<username> [date]",
			null,
			2000,
			false,
			false,
			true
		);
	}
	
	public Result run(String[] args, MessageReceivedEvent e) {
		long id = e.getGuild().getIdLong();
		
		//No arguments message
		if (args.length == 0) {
			String m = ":warning: Incorrect arguments." +
				"\n" + Database.getPrefix(id) + "uuid <username> [date]" +
				"\n" + MessageUtils.dateHelp;
			return new Result(Outcome.WARNING, m, 5);
		}
		
		String username = args[0];
		String uuid = null;
		
		//Parse date argument
		if (args.length > 1) {
			long timestamp = DateUtils.getTimestamp(Arrays.copyOfRange(args, 1, args.length));
			if (timestamp == -1) {
				return new Result(Outcome.WARNING, MessageUtils.dateErrorString(id, "uuid"));
			}
			
		//Get the UUID
			uuid = NameUtils.getUUID(username, timestamp);
		} else {
			uuid = NameUtils.getUUID(username);
		}
		
		//Check for errors
		if (uuid == null) {
			String m = ":x: The Mojang API could not be reached." +
				"\n" + "Are you sure that username exists?" +
				"\n" + "Usernames are case-sensitive.";
			return new Result(Outcome.WARNING, m, 2);
		} else if (!uuid.matches(NameUtils.uuidRegex)) {
			String m = ":x: The API responded with an error:\n" + uuid;
			return new Result(Outcome.ERROR, m, 3);
		}
		
		//Get NameMC url
		String url = "https://namemc.com/profile/" + uuid;
		
		//PROPER APOSTROPHE GRAMMAR THANK THE LORD
		String title = username;
		if (title.endsWith("s")) {
			title = title + "' UUID";
		} else {
			title = title + "'s UUID";
		}
		
		MessageEmbed me = MessageUtils.embedMessage(title, url, uuid, Color.GREEN);
		
		return new Result(Outcome.SUCCESS, new EmbedBuilder(me).build());
	}
	
}

@@ -846,16 +846,17 @@
 %0A%09%09%0A%09%09//
+ 
 No argum
@@ -868,16 +868,16 @@
 message%0A
-
 %09%09if (ar
@@ -1150,16 +1150,17 @@
 %0A%09%09%0A%09%09//
+ 
 Parse da
@@ -1399,24 +1399,25 @@
 %09%09%7D%0A%09%09%09%0A%09%09//
+ 
 Get the UUID
@@ -1528,16 +1528,17 @@
 %0A%09%09%0A%09%09//
+ 
 Check fo
@@ -1934,16 +1934,17 @@
 %0A%09%09%0A%09%09//
+ 
 Get Name
@@ -2014,48 +2014,34 @@
 %09%09//
-PROPER APOSTROPHE GRAMMAR THANK THE LORD
+ Proper apostrophe grammar
 %0A%09%09S
@@ -2170,16 +2170,105 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09String m = String.format(%22Short: %60%25s%60%5CnLong: %60%25s%60%22, uuid, NameUtils.formatUUID(uuid));%0A
 %09%09Messag
@@ -2317,20 +2317,17 @@
 e, url, 
-uuid
+m
 , Color.

Include short and long &uuid