package io.github.aquerr.eaglefactions.listeners;

import io.github.aquerr.eaglefactions.logic.FactionLogic;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TextRepresentable;
import org.spongepowered.api.text.format.TextColors;

public class ChatMessageListener
{
    @Listener
    public void onChatMessage(MessageChannelEvent.Chat event, @Root Player player)
    {
        if(FactionLogic.getFactionName(player.getUniqueId()) != null)
        {
            String message = event.getRawMessage().toPlain();

            String factionTag = FactionLogic.getFactionTag(FactionLogic.getFactionName(player.getUniqueId()));
            if(factionTag != null)
            {
                TextRepresentable header = Text.of("[", TextColors.GREEN, factionTag, TextColors.WHITE, "]" + player.getName() + ": ");
                TextRepresentable textRepresentable = Text.of(message);

                event.setMessage(header,textRepresentable);
            }
        }

        return;
    }
}

@@ -730,11 +730,8 @@
 .get
-Raw
 Mess
@@ -744,24 +744,221 @@
 toPlain();%0A%0A
+            String fullMessage%5B%5D = message.split(%22:%22);%0A%0A            String head = fullMessage%5B0%5D;%0A            String body = fullMessage%5B1%5D;%0A%0A            head = head.replace(player.getName(), %22%22);%0A%0A
             
@@ -1156,16 +1156,23 @@
 Text.of(
+head + 
 %22%5B%22, Tex
@@ -1213,13 +1213,13 @@
 ors.
-WHITE
+RESET
 , %22%5D
@@ -1243,17 +1243,16 @@
 e() + %22:
- 
 %22);%0A    
@@ -1309,23 +1309,20 @@
 Text.of(
-message
+body
 );%0A%0A    

Improve display of faction tags