package com.Acrobot.Breeze.Utils;

import com.Acrobot.Breeze.Utils.MojangAPI.NameFetcher;
import com.Acrobot.Breeze.Utils.MojangAPI.UUIDFetcher;
import com.google.common.collect.ImmutableMap;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.UUID;

/**
 * Provides methods to handle usernames/UUIDs
 * @author Andrzej Pomirski (Acrobot)
 */
public class NameUtil {
    private static final UUID INVALID_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
    private static final String PROFILE_AGENT = "minecraft";

    public static UUID getUUID(Player player) {
        return player.getUniqueId();
    }

    public static UUID getUUID(String username) {
        UUIDFetcher fetcher = new UUIDFetcher(username);

        try {
            Map<String, UUID> uuidMap = fetcher.call();

            if (uuidMap.size() < 1) {
                return INVALID_UUID;
            }

            return uuidMap.get(username);
        } catch (Exception exception){
            return INVALID_UUID;
        }
    }

    public static Map<String, UUID> getUUID(String... usernames) {
        UUIDFetcher fetcher = new UUIDFetcher(usernames);

        try {
            return fetcher.call();
        } catch (Exception exception){
            return ImmutableMap.of();
        }
    }

    public static String getName(UUID uuid) {
        NameFetcher fetcher = new NameFetcher(uuid);

        try {
            Map<UUID, String> uuidMap = fetcher.call();

            return uuidMap.get(uuid);
        } catch (Exception e) {
            return null;
        }
    }

    public static Map<UUID, String> getName(UUID... uuids) {
        NameFetcher fetcher = new NameFetcher(uuids);

        try {
            return fetcher.call();
        } catch (Exception e) {
            return ImmutableMap.of();
        }
    }

    public static String stripUsername(String username) {
        if (username.length() > 15) {
            return username.substring(0, 15);
        }

        return username;
    }

    public static boolean isInvalid(UUID uuid) {
        return uuid.equals(INVALID_UUID);
    }
}

@@ -315,16 +315,19 @@
 s/UUIDs%0A
+ *%0A
  * @auth
@@ -556,95 +556,426 @@
     
-public static UUID getUUID(Player player) %7B%0A        return player.getUniqueId();%0A    %7D%0A
+/**%0A     * Get the UUID of a player%0A     *%0A     * @param player Player whose UUID to get%0A     * @return The player's UUID%0A     */%0A    public static UUID getUUID(Player player) %7B%0A        return player.getUniqueId();%0A    %7D%0A%0A    /**%0A     * Gets the UUID of a specified username (fetches it from Mojang's servers)%0A     *%0A     * @param username Username whose UUID to get%0A     * @return UUID of a specified username%0A     */
 %0A   
@@ -1311,32 +1311,33 @@
 ption exception)
+ 
 %7B%0A            re
@@ -1364,32 +1364,235 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Gets the UUID of specified usernames (fetches them from Mojang's servers)%0A     *%0A     * @param usernames Usernames whose UUID to get%0A     * @return UUID of the specified usernames%0A     */%0A
     public stati
@@ -1787,16 +1787,17 @@
 ception)
+ 
 %7B%0A      
@@ -1837,32 +1837,190 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Fetches a name from UUID from Mojang's servers%0A     *%0A     * @param uuid UUID to check%0A     * @return The name associated with an UUID%0A     */%0A
     public stati
@@ -2278,32 +2278,151 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Fetches names from UUIDs%0A     *%0A     * @param uuids UUIDs to check%0A     * @return Fetched names%0A     */%0A
     public stati
@@ -2649,32 +2649,437 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Check if the UUID is invalid%0A     *%0A     * @param uuid UUID to check%0A     * @return Is the UUID invalid?%0A     */%0A    public static boolean isInvalid(UUID uuid) %7B%0A        return uuid.equals(INVALID_UUID);%0A    %7D%0A%0A    /**%0A     * Strip the username to 15 characters (number of characters a sign can hold)%0A     *%0A     * @param username Username to strip%0A     * @return Stripped username%0A     */%0A
     public stati
@@ -3132,109 +3132,248 @@
     
-if (username.length() %3E 15) %7B
+return stripUsername(username, 15);%0A    %7D%0A
 %0A    
+/**%0A
      
-   return username.substring(0, 15);%0A        %7D%0A%0A        return
+* Strips the username to a specified number of characters%0A     *%0A     * @param username Username to strip%0A     * @param length   Length of the expected username%0A     * @return Stripped
  use
@@ -3377,24 +3377,24 @@
 username
-;
 %0A    
-%7D%0A
+ */
 %0A    pub
@@ -3408,79 +3408,186 @@
 tic 
-boolean isInvalid(UUID uuid) %7B%0A        return uuid.equals(INVALID_UUID)
+String stripUsername(String username, int length) %7B%0A        if (username.length() %3E length) %7B%0A            return username.substring(0, length);%0A        %7D%0A%0A        return username
 ;%0A  

Document the functions