//  • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
//  ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
//  ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
//  ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
//  ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2026
//                https://www.magicbane.com

package engine.net.client.handlers;

import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.IgnoreMsg;
import engine.objects.Account;
import engine.objects.PlayerCharacter;

public class IgnoreMsgHandler extends AbstractClientMsgHandler {

    public IgnoreMsgHandler() {
        super();
    }

    @Override
    protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) {

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        IgnoreMsg msg;

        // Member variable assignment

        msg = (IgnoreMsg) baseMsg;

        if (msg.nameToIgnore.isEmpty()) { // list ignored players
            String[] ignoredPlayers = playerCharacter.getIgnoredPlayerNames();
            String crlf = "\r\n";
            String out = "Ignored players (" + ignoredPlayers.length + "):";

            for (String name : ignoredPlayers)
                out += crlf + name;

            ChatManager.chatSystemInfo(playerCharacter, out);
            return true;
        }

        //FIX THIS, USE OUR CACHE!
        PlayerCharacter pcToIgnore = PlayerCharacter.getByFirstName(msg.nameToIgnore);

        if (playerCharacter == null)
            return true;


        if (pcToIgnore == null || pcToIgnore.getAccount() == null) {
            ChatManager.chatSystemError(playerCharacter, "Character name " + msg.nameToIgnore + " does not exist and cannot be ignored.");
            return true;
        }
        if (pcToIgnore.getObjectUUID() == playerCharacter.getObjectUUID()) {
            ChatManager.chatSystemError(playerCharacter, "Try as you might, you are unable to ignore yourself!");
            return true;
        }
        String firstName = pcToIgnore.getFirstName();

        Account account = playerCharacter.getAccount();

        if (account == null)
            return true;

        if (playerCharacter.isIgnoringPlayer(pcToIgnore)) {

            if (account != null) {
                if (!DbManager.PlayerCharacterQueries.SET_IGNORE_LIST(account.getObjectUUID(), pcToIgnore.getObjectUUID(), false, pcToIgnore.getFirstName())) {
                    ChatManager.chatSystemError(playerCharacter, "Unable to update database ignore list.");
                }
            } else {
                ChatManager.chatSystemError(playerCharacter, "Unable to update database ignore list.");
            }

            playerCharacter.removeIgnoredPlayer(pcToIgnore.getAccount());
            ChatManager.chatSystemInfo(playerCharacter, "Character " + firstName + " is no longer ignored.");
        } else {

            if (!PlayerCharacter.isIgnorable()) {
                ChatManager.chatSystemError(playerCharacter, "This character cannot be ignored.");
                return true;
            }

            if (PlayerCharacter.isIgnoreListFull()) {
                ChatManager.chatSystemError(playerCharacter, "Your ignore list is already full.");
                return true;
            }

            if (account != null) {
                if (!DbManager.PlayerCharacterQueries.SET_IGNORE_LIST(account.getObjectUUID(), pcToIgnore.getObjectUUID(), true, pcToIgnore.getFirstName())) {
                    ChatManager.chatSystemError(playerCharacter, "Unable to update database ignore list. This ignore will not persist past server down.");
                }
            } else {
                ChatManager.chatSystemError(playerCharacter, "Unable to update database ignore list.");
            }

            playerCharacter.addIgnoredPlayer(pcToIgnore.getAccount(), pcToIgnore.getFirstName());
            ChatManager.chatSystemInfo(playerCharacter, "Character " + firstName + " is now being ignored.");
        }

        return true;
    }

}