// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.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.InviteToSubMsg;
import engine.objects.Guild;
import engine.objects.GuildStatusController;
import engine.objects.PlayerCharacter;

public class InviteToSubHandler extends AbstractClientMsgHandler {

    public InviteToSubHandler() {
        super();
    }

    private static void sendChat(PlayerCharacter source, String msg) {
        ChatManager.chatGuildError(source, msg);
    }

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

        PlayerCharacter source;
        PlayerCharacter target;
        Guild nation;
        Guild protectorate;
        InviteToSubMsg msg = (InviteToSubMsg) baseMsg;
        Dispatch dispatch;

        source = SessionManager.getPlayerCharacter(origin);

        if (source == null)
            return true;

        target = (PlayerCharacter) DbManager.getObject(GameObjectType.PlayerCharacter, msg.getTargetUUID());

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

        //Ignore invites to sub if ignoring player

        if (target.isIgnoringPlayer(source))
            return true;

        nation = source.getGuild();
        protectorate = target.getGuild();

        //source must be in guild

        if (nation == null) {
            sendChat(source, "You must be in a guild to invite to sub.");
            return true;
        }

        if (nation.isEmptyGuild()) {
            sendChat(source, "You must be in a guild to invite to sub.");
            return true;
        }

        //source must be GL or IC

        if (GuildStatusController.isInnerCouncil(source.getGuildStatus()) == false) {
            sendChat(source, "Only guild leadership can invite to sub.");
            return true;
        }

        if (nation.getNation().isEmptyGuild())
            return true;

        //target must be in a guild

        if (protectorate == null)
            return true;

        if (nation.equals(protectorate))
            return true;

        //target must be GL or IC

        if (GuildStatusController.isInnerCouncil(target.getGuildStatus()) == false && GuildStatusController.isGuildLeader(target.getGuildStatus()) == false) {
            sendChat(source, "Target player is not guild leadership.");
            return true;
        }

        //cannot be subbed into a nation if you already have your own sub guilds
        if (protectorate.getSubGuildList() != null && protectorate.getSubGuildList().size() > 0) {
            sendChat(source, "This Guild is already a nation!");
            return true;
        }

        if (!nation.canSubAGuild(protectorate)) {
            sendChat(source, "This Guild can't be subbed.");
            return true;
        }

        if (nation.getSubGuildList().size() >= nation.getOwnedCity().getRank()) {
            sendChat(source, "Your TOL cannot support another subguild!");
            return true;
        }
        if (ConfigManager.MB_RULESET.getValue().equals("LORE")) {
            if (source.guild.getGuildType().equals(target.guild.getGuildType()) == false) {
                sendChat(source, "You Must Be The Same Charter To Form A Nation.");
                return true;
            }
        }
        //all tests passed, let's send invite.

        if (target.getClientConnection() != null) {
            msg.setGuildTag(nation.getGuildTag());
            msg.setGuildName(nation.getName());
            msg.setGuildUUID(nation.getObjectUUID());
            msg.setUnknown02(1);

            dispatch = Dispatch.borrow(target, msg);
            DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

        } else {
            sendChat(source, "Failed to send sub invite to target.");
            return true;
        }

        return true;
    }
}
