package com.demigodsrpg.demigods.classic.listener;

import com.censoredsoftware.library.util.StringUtil2;
import com.demigodsrpg.demigods.classic.DGClassic;
import com.demigodsrpg.demigods.classic.deity.Deity;
import com.demigodsrpg.demigods.classic.model.PlayerModel;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.Random;
import java.util.Set;

public class PlayerListener implements Listener {
    Random random = new Random();

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        PlayerModel model = DGClassic.PLAYER_R.fromPlayer(event.getPlayer());

        // TODO REMOVE THIS DEBUG TESTING ONLY
        if (model.getMajorDeity().equals(Deity.HUMAN)) {
            model = new PlayerModel(event.getPlayer());
            DGClassic.PLAYER_R.register(model);

            if (random.nextBoolean()) {
                model.giveMajorDeity(Deity.ZEUS);
                model.giveDeity(Deity.HEPHAESTUS);
            } else {
                model.giveMajorDeity(Deity.CRONUS);
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        if (DGClassic.SERV_R.exists("alliance_chat", event.getPlayer().getUniqueId().toString())) {
            event.getRecipients().clear();

            PlayerModel model = DGClassic.PLAYER_R.fromPlayer(event.getPlayer());
            Set<PlayerModel> playerModelSet = DGClassic.PLAYER_R.getOnlineInAlliance(model.getAlliance());
            for (PlayerModel playerModel : playerModelSet) {
                event.getRecipients().add(playerModel.getOfflinePlayer().getPlayer());
            }

            String format = event.getFormat();
            event.setFormat(model.getMajorDeity().getColor() + "[" + model.getMajorDeity().getColor() + StringUtil2.beautify(model.getAlliance().name()) + "] " + format);
        }
    }
}

@@ -262,16 +262,45 @@
 rModel;%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -1339,32 +1339,111 @@
 tEvent event) %7B%0A
+        PlayerModel model = DGClassic.PLAYER_R.fromPlayer(event.getPlayer());%0A%0A
         if (DGCl
@@ -1572,91 +1572,8 @@
 r();
-%0A%0A            PlayerModel model = DGClassic.PLAYER_R.fromPlayer(event.getPlayer());
 %0A   
@@ -1834,24 +1834,283 @@
           %7D%0A
+            event.setFormat(model.getMajorDeity().getColor() + %22%5B%22 + model.getMajorDeity().getColor() + StringUtil2.beautify(model.getAlliance().name()) + %22%5D %22 + event.getPlayer().getDisplayName() + ChatColor.GRAY + %22:%22 + event.getMessage());%0A        %7D else %7B
 %0A           

Improve chat.