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


package engine.net.client.handlers;

import engine.gameManager.*;
import engine.mbEnums;
import engine.mbEnums.GameObjectType;
import engine.mbEnums.GuildState;
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.AcceptSubInviteMsg;
import engine.net.client.msg.guild.SendGuildEntryMsg;
import engine.objects.City;
import engine.objects.Guild;
import engine.objects.GuildStatusController;
import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger;

import java.util.ArrayList;

public class SwearInGuildHandler extends AbstractClientMsgHandler {

    public SwearInGuildHandler() {
        super();
    }

    @Override
    protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) {
        PlayerCharacter player;
        AcceptSubInviteMsg swearInMsg;
        Guild targetGuild;
        Guild nation;
        Dispatch dispatch;

        swearInMsg = (AcceptSubInviteMsg) baseMsg;
        player = SessionManager.getPlayerCharacter(origin);

        if (player == null)
            return true;

        targetGuild = (Guild) DbManager.getObject(GameObjectType.Guild, swearInMsg.guildUUID());

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

        nation = player.getGuild();

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

        try {
            if (!nation.isNation()) {
                ErrorPopupMsg.sendErrorMsg(player, "Your guild is not a nation!");
                return true;
            }

            if (GuildStatusController.isGuildLeader(player.getGuildStatus()) == false) {
                ErrorPopupMsg.sendErrorMsg(player, "Your do not have such authority!");
                return true;
            }

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

            if (targetGuild.getGuildState().equals(GuildState.Errant))
                GuildManager.updateAllGuildBinds(targetGuild, nation.getOwnedCity());

            //update Guild state.
            targetGuild.setNation(nation);
            GuildManager.updateAllGuildTags(targetGuild);
            targetGuild.upgradeGuildState(false);

            if (nation.getGuildState() == GuildState.Sovereign)
                nation.upgradeGuildState(true);

            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(nation.getObjectUUID());

            for (PlayerCharacter member : guildMembers) {
                ChatManager.chatGuildInfo(member, "Your Guild is now a Nation!");
            }

            ArrayList<PlayerCharacter> swornMembers = SessionManager.getActivePCsInGuildID(targetGuild.getObjectUUID());

            for (PlayerCharacter member : swornMembers) {
                ChatManager.chatGuildInfo(member, "Your Guild has sword fealty to " + nation.getName() + '.');
            }
        } catch (Exception e) {
            Logger.error(e.getMessage());
            return true;
        }

        return true;
    }

}
