package io.github.aquerr.eaglefactions.listeners;

import io.github.aquerr.eaglefactions.EagleFactions;
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.format.TextColors;

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

          //  //Get full formatted and colored message.
          //  Text fullMessage = event.getMessage();
          //  EagleFactions.getEagleFactions().getLogger().info(fullMessage.toPlain());
//
          //  //Get faction's tag
          //  Text factionTag = Text.builder()
          //          .append(Text.of("[" ,TextColors.GREEN, FactionLogic.getFactionTag(FactionLogic.getFactionName(player.getUniqueId())), TextColors.RESET, "]"))
          //          .build();
          //  EagleFactions.getEagleFactions().getLogger().info(factionTag.toPlain());
//
//
//
          //  //Get message content
          //  String message = event.getMessage().toPlain();
          //  String messages[] = message.split(":");
          //  String body = messages[1];
          //  EagleFactions.getEagleFactions().getLogger().info(body);
//
          //  //Get message header (Any tags that are before player's name)
          //  Text header = fullMessage.toBuilder()
          //          .remove(Text.of(messages[0].replace(player.getName() + ":", ""))).style(fullMessage.getStyle())
          //          .remove(Text.of(body))
          //          .build();
//
          //  //Text header = Text.of("");
//
          // // for (Text child: fullMessage.toBuilder().getChildren())
          // // {
          // //     for (Text nextChild: child.getChildren())
          // //     {
          // //         EagleFactions.getEagleFactions().getLogger().info("Child w childrenie to: " + nextChild.toPlain());
          // //     }
////
          // // }
//
          //  EagleFactions.getEagleFactions().getLogger().info(fullMessage.getChildren().toString());
          //  EagleFactions.getEagleFactions().getLogger().info(header.toPlain());
//
//
          //  //Create final message with factions tag.
          //  Text messageToPrint = Text.builder()
          //          .append(header)
          //          .append(factionTag)
          //          .append(Text.of(player.getName() + ":"))
          //          .append(Text.of(body))
          //          .build();
          //  EagleFactions.getEagleFactions().getLogger().info(messageToPrint.toPlain());
//
          //  event.setMessage(messageToPrint);

            Text factionPrefix = Text.builder().build();

            if(!FactionLogic.getFactionTag(factionName).equals("") && FactionLogic.getFactionTag(factionName) != null)
            {
                EagleFactions.getEagleFactions().getLogger().info("Tag has been found!");

                //Get faction's tag
                Text factionTag = Text.builder()
                        .append(Text.of("[" ,TextColors.GREEN, FactionLogic.getFactionTag(factionName), TextColors.RESET, "]"))
                        .build();

                factionPrefix.toBuilder().append(factionTag).build();
            }

            //Get leader prefix.
            if(FactionLogic.getLeader(factionName).equals(player.getUniqueId().toString()))
            {
                EagleFactions.getEagleFactions().getLogger().info("Leader has been found!");


                Text leaderPrefix = Text.builder()
                        .append(Text.of("[", TextColors.GOLD, "Leader", TextColors.RESET, "]"))
                        .build();

                factionPrefix.toBuilder().append(leaderPrefix).build();
            }

            //Get officer prefix.
            if(FactionLogic.getOfficers(factionName).contains(player.getUniqueId().toString()))
            {
                EagleFactions.getEagleFactions().getLogger().info("Officer has been found!");

                Text officerPrefix = Text.builder()
                        .append(Text.of("[", TextColors.GOLD, "Officer", TextColors.RESET, "]"))
                        .build();

                factionPrefix.toBuilder().append(officerPrefix).build();
            }

            EagleFactions.getEagleFactions().getLogger().info("Build the whole message!");

            //Build the whole message & print.
            Text messageToPrint = Text.builder()
                    .append(factionPrefix)
                    .append(event.getMessage())
                    .build();

            event.setMessage(messageToPrint);
        }

        return;
    }
}

@@ -3000,32 +3000,40 @@
             Text
+.Builder
  factionPrefix =
@@ -3047,24 +3047,16 @@
 uilder()
-.build()
 ;%0A%0A     
@@ -3187,99 +3187,8 @@
   %7B%0A
-                EagleFactions.getEagleFactions().getLogger().info(%22Tag has been found!%22);%0A%0A
     
@@ -3457,36 +3457,24 @@
 ctionPrefix.
-toBuilder().
 append(facti
@@ -3479,24 +3479,16 @@
 tionTag)
-.build()
 ;%0A      
@@ -3639,103 +3639,8 @@
   %7B%0A
-                EagleFactions.getEagleFactions().getLogger().info(%22Leader has been found!%22);%0A%0A%0A
     
@@ -3843,36 +3843,24 @@
 ctionPrefix.
-toBuilder().
 append(leade
@@ -3863,32 +3863,24 @@
 eaderPrefix)
-.build()
 ;%0A          
@@ -4032,103 +4032,8 @@
   %7B%0A
-                EagleFactions.getEagleFactions().getLogger().info(%22Officer has been found!%22);%0A%0A
     
@@ -4242,28 +4242,16 @@
 nPrefix.
-toBuilder().
 append(o
@@ -4263,24 +4263,16 @@
 rPrefix)
-.build()
 ;%0A      
@@ -4284,100 +4284,8 @@
  %7D%0A%0A
-            EagleFactions.getEagleFactions().getLogger().info(%22Build the whole message!%22);%0A%0A
     
@@ -4417,16 +4417,24 @@
 onPrefix
+.build()
 )%0A      

Fix Leader & Officer tag