// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.SendGuildEntryMsg;
import engine.net.client.msg.guild.SwearInGuildMsg;
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;
        SwearInGuildMsg swearInMsg;
        Guild protectorate;
        Guild nation;
        Dispatch dispatch;

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

        if (player == null)
            return true;

        protectorate = (Guild) DbManager.getObject(GameObjectType.Guild, swearInMsg.getGuildUUID());

        if (protectorate == 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 (!nation.getSubGuildList().contains(protectorate)) {
                ErrorPopupMsg.sendErrorMsg(player, "Guild is not a petitioner!");
                return true;
            }

            if (protectorate.guildState.equals(GuildState.Petitioner) == false) {
                ErrorPopupMsg.sendErrorMsg(player, "Guild is not a petitioner!");
                return true;
            }

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

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

            // update Guild state.
            // R8 is a province

            if (protectorate.getNation().isEmptyGuild())
                protectorate.guildState = GuildState.Sworn;
            else
                protectorate.guildState = (protectorate.getOwnedCity().getTOL().getRank() == 8)
                        ? GuildState.Province
                        : GuildState.Protectorate;

            protectorate.setNation(nation);
            GuildManager.updateAllGuildTags(protectorate);

            // Upgrade to a nation if new sub is a landed guild

            if (nation.guildState == GuildState.Sovereign)
                if (protectorate.guildState.equals(GuildState.Protectorate) ||
                        protectorate.guildState.equals(GuildState.Province))
                    nation.guildState = GuildState.Nation;

            // Nation realm count update

            if (protectorate.guildState.equals(GuildState.Province))
                nation.realmsOwned = nation.realmsOwned + 1;

            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, protectorate.getName() + " has sworn fealty to you.");

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

            for (PlayerCharacter member : swornMembers)
                ChatManager.chatGuildInfo(member, "Your Guild has sworn fealty to " + nation.getName() + '.');

        } catch (Exception e) {
            Logger.error(e.getMessage());
            return true;
        }

        return true;
    }

}
