package me.innectic.permissify.api.util;

import me.innectic.permissify.api.PermissifyAPI;
import me.innectic.permissify.api.database.DatabaseHandler;
import me.innectic.permissify.api.permission.PermissionGroup;

import java.util.List;
import java.util.UUID;

/**
 * @author Innectic
 * @since 7/1/2017
 */
public class ChatFormatter {

    /**
     * Format a chat message into the requested format.
     *
     * @param uuid     the uuid of the player who sent
     * @param username the username of the player who sent
     * @param message  the message that was sent
     * @return         the formatted string
     */
    public static String formatChat(UUID uuid, String username, String message) {
        if (!PermissifyAPI.get().isPresent()) return username + ": " + message;
        if (!PermissifyAPI.get().get().getDatabaseHandler().isPresent()) return username + ": " + message;
        DatabaseHandler handler = PermissifyAPI.get().get().getDatabaseHandler().get();

        // TODO: Do something about primary groups that would be the "display" group
        List<PermissionGroup> group = handler.getGroups(uuid);
        String formatter = handler.getChatFormat(false);
        return formatter.replace("{group}", group.get(0).getName())
                .replace("{username}", username).replace("{message}", message);
    }

    /**
     * Format a whisper message
     *
     * @param senderUuid   the uuid of the player who sent it
     * @param senderName   the name of the player who sent it
     * @param receiverUuid the uuid of the receiver of the message
     * @param receiverName the name of the receiver of the message
     * @param message      the message sent
     * @return             the final formatted message
     */
    public static String formatWhisper(UUID senderUuid, String senderName, UUID receiverUuid, String receiverName, String message) {
        if (!PermissifyAPI.get().isPresent()) return senderName + " > " + receiverName + ": " + message;
        if (!PermissifyAPI.get().get().getDatabaseHandler().isPresent()) return senderName + " > " + receiverName + ": " + message;
        DatabaseHandler handler = PermissifyAPI.get().get().getDatabaseHandler().get();

        // TODO: Do something about primary groups that would be the "display" group
        List<PermissionGroup> senderGroups = handler.getGroups(senderUuid);
        List<PermissionGroup> receiverGroups = handler.getGroups(receiverUuid);
        String formatter = handler.getWhisperFormat(false);
        return formatter.replace("{senderGroup}", senderGroups.get(0).getName())
                .replace("{username}", senderName).replace("{message}", message)
                .replace("{to}", receiverName).replace("{receiverGroup}", receiverGroups.get(0).getName());
    }
}

@@ -237,16 +237,44 @@
 .List;%0D%0A
+import java.util.Optional;%0D%0A
 import j
@@ -1043,98 +1043,16 @@
     
-// TODO: Do something about primary groups that would be the %22display%22 group%0D%0A        List
+Optional
 %3CPer
@@ -1084,22 +1084,28 @@
 dler.get
+Primary
 Group
-s
 (uuid);%0D
@@ -1101,24 +1101,91 @@
 oup(uuid);%0D%0A
+        if (!group.isPresent()) return username + %22: %22 + message;%0D%0A
         Stri
@@ -1280,25 +1280,24 @@
 , group.get(
-0
 ).getName())
@@ -2286,84 +2286,182 @@
     
-// TODO: Do something about p
+Optional%3CPermissionGroup%3E senderGroup = handler.getP
 rimary
- g
+G
 roup
-s that would be the %22display%22 group
+(senderUuid);%0D%0A        if (!senderGroup.isPresent()) return senderName + %22 %3E %22 + receiverName + %22: %22 + message;
 %0D%0A  
@@ -2458,36 +2458,40 @@
 ssage;%0D%0A        
-List
+Optional
 %3CPermissionGroup
@@ -2492,28 +2492,29 @@
 nGroup%3E 
-send
+receiv
 erGroup
-s
  = handl
@@ -2519,22 +2519,28 @@
 dler.get
+Primary
 Group
-s
 (senderU
@@ -2558,78 +2558,97 @@
     
-List%3CPermissionGroup%3E receiverGroups = ha
+if (!receiverGroup.isPresent()) return se
 nd
-l
 er
-.getGroups(receiverUuid)
+Name + %22 %3E %22 + receiverName + %22: %22 + message
 ;%0D%0A 
@@ -2764,31 +2764,29 @@
  senderGroup
-s
 .get(
-0
 ).getName())
@@ -2960,15 +2960,13 @@
 roup
-s
 .get(
-0
 ).ge

Support primary groups formatting