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


package engine.net.client.handlers;

import engine.gameManager.*;
import engine.mbEnums;
import engine.mbEnums.GameObjectType;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ErrorPopupMsg;
import engine.net.client.msg.guild.BreakFealtyMsg;
import engine.net.client.msg.guild.SendGuildEntryMsg;
import engine.objects.*;
import engine.server.world.WorldServer;
import engine.session.Session;

import java.util.ArrayList;

public class BreakFealtyHandler extends AbstractClientMsgHandler {

    public BreakFealtyHandler() {
        super();
    }

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

        BreakFealtyMsg bfm;
        PlayerCharacter player;
        Guild nation;
        Guild protectorate;
        Dispatch dispatch;

        bfm = (BreakFealtyMsg) baseMsg;

        // get PlayerCharacter of person accepting invite

        player = SessionManager.getPlayerCharacter(
                origin);

        if (player == null)
            return true;

        nation = (Guild) DbManager.getObject(GameObjectType.Guild, bfm.getGuildUUID());

        if (nation == null) {
            ErrorPopupMsg.sendErrorMsg(player, "A Serious error has occured. Please post details for to ensure transaction integrity");
            return true;
        }

        protectorate = player.getGuild();

        if (protectorate == null) {
            ErrorPopupMsg.sendErrorMsg(player, "You do not belong to a guild!");
            return true;
        }

        if (nation.isNPCGuild()) {
            if (!GuildStatusController.isGuildLeader(player.getGuildStatus())) {
                ErrorPopupMsg.sendErrorMsg(player, "Only guild leader can break fealty!");
                return true;
            }


            if (!DbManager.GuildQueries.UPDATE_PARENT(protectorate.getObjectUUID(), WorldServer.worldUUID)) {
                ErrorPopupMsg.sendErrorMsg(player, "A Serious error has occurred. Please post details for to ensure transaction integrity");
                return true;
            }

            switch (protectorate.guildState) {
                case Sworn:
                    protectorate.setNation(Guild.getErrantNation());
                    GuildManager.updateAllGuildTags(protectorate);
                    GuildManager.updateAllGuildBinds(protectorate, null);
                    protectorate.guildState = mbEnums.GuildState.Errant;
                    break;
                case Province:
                case Protectorate:
                    protectorate.setNation(protectorate);
                    GuildManager.updateAllGuildTags(protectorate);
                    GuildManager.updateAllGuildBinds(protectorate, protectorate.getOwnedCity());
                    protectorate.guildState = mbEnums.GuildState.Sovereign;
                    break;
            }

            SendGuildEntryMsg msg = new SendGuildEntryMsg(player);
            dispatch = Dispatch.borrow(player, msg);
            DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

            City.lastCityUpdate = System.currentTimeMillis();
            ArrayList<PlayerCharacter> guildMembers = SessionManager.getActivePCsInGuildID(protectorate.getObjectUUID());

            for (PlayerCharacter member : guildMembers)
                ChatManager.chatGuildInfo(member, protectorate.getName() + " has broke fealty from " + nation.getName() + '!');

            ArrayList<PlayerCharacter> breakFealtyMembers = SessionManager.getActivePCsInGuildID(nation.getObjectUUID());

            for (PlayerCharacter member : breakFealtyMembers)
                ChatManager.chatGuildInfo(member, protectorate.getName() + " has broken fealty from " + nation.getName() + '!');

            return true;
        }

        if (!nation.getSubGuildList().contains(protectorate)) {
            ErrorPopupMsg.sendErrorMsg(player, "Failure to break fealty!");
            return true;
        }

        if (!GuildStatusController.isGuildLeader(player.getGuildStatus())) {
            ErrorPopupMsg.sendErrorMsg(player, "Only guild leader can break fealty!");
            return true;
        }

        if (Bane.getBaneByAttackerGuild(protectorate) != null) {
            ErrorPopupMsg.sendErrorMsg(player, "You may break fealty with active bane!");
            return true;
        }

        if (!DbManager.GuildQueries.UPDATE_PARENT(protectorate.getObjectUUID(), WorldServer.worldUUID)) {
            ErrorPopupMsg.sendErrorMsg(player, "A Serious error has occurred. Please post details for to ensure transaction integrity");
            return true;
        }

        switch (protectorate.guildState) {
            case Sworn:
                protectorate.setNation(Guild.getErrantNation());
                GuildManager.updateAllGuildTags(protectorate);
                GuildManager.updateAllGuildBinds(protectorate, null);
                break;
            case Province:
                protectorate.setNation(protectorate);
                GuildManager.updateAllGuildTags(protectorate);
                GuildManager.updateAllGuildBinds(protectorate, protectorate.getOwnedCity());
                break;
        }

        nation.getSubGuildList().remove(protectorate);

        SendGuildEntryMsg msg = new SendGuildEntryMsg(player);
        dispatch = Dispatch.borrow(player, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

        //Update Map.

        final Session s = SessionManager.getSession(player);

        City.lastCityUpdate = System.currentTimeMillis();

        ArrayList<PlayerCharacter> guildMembers = SessionManager.getActivePCsInGuildID(protectorate.getObjectUUID());

        for (PlayerCharacter member : guildMembers) {
            ChatManager.chatGuildInfo(member, protectorate.getName() + " has broke fealty from " + nation.getName() + '!');
        }

        ArrayList<PlayerCharacter> breakFealtyMembers = SessionManager.getActivePCsInGuildID(nation.getObjectUUID());

        for (PlayerCharacter member : breakFealtyMembers) {
            ChatManager.chatGuildInfo(member, protectorate.getName() + " has broken fealty from " + nation.getName() + '!');
        }

        return true;
    }
}
