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


package engine.net.client.handlers;

import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.gameManager.DispatchManager;
import engine.gameManager.SessionManager;
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.objects.Guild;
import engine.objects.GuildStatusController;
import engine.objects.PlayerCharacter;

import java.util.ArrayList;

public class AcceptSubInviteHandler extends AbstractClientMsgHandler {

    public AcceptSubInviteHandler() {
        super();
    }

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

        AcceptSubInviteMsg msg = (AcceptSubInviteMsg) baseMsg;
        PlayerCharacter sourcePlayer;
        Guild swornGuild;
        Guild nation;
        Dispatch dispatch;

        // get PlayerCharacter of person sending sub invite

        sourcePlayer = SessionManager.getPlayerCharacter(origin);

        if (sourcePlayer == null)
            return true;

        swornGuild = sourcePlayer.getGuild();
        nation = (Guild) DbManager.getObject(GameObjectType.Guild, msg.guildUUID());

        //must be source guild to sub to

        if (nation == null) {
            ErrorPopupMsg.sendErrorPopup(sourcePlayer, 45); // Failure to swear guild
            return true;
        }
        if (swornGuild == null) {
            ErrorPopupMsg.sendErrorPopup(sourcePlayer, 45); // Failure to swear guild
            return true;
        }

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

        if (GuildStatusController.isGuildLeader(sourcePlayer.getGuildStatus()) == false) {
            ErrorPopupMsg.sendErrorMsg(sourcePlayer, "Only a guild leader can accept fealty!");
            return true;
        }

        //source guild is limited to 7 subs
        //TODO this should be based on TOL rank

        if (!nation.canSubAGuild(swornGuild)) {
            ErrorPopupMsg.sendErrorPopup(sourcePlayer, 45); // Failure to swear guild
            return true;
        }

        //all tests passed, let's Handle code
        //Update Target Guild State.

        swornGuild.guildState = GuildState.Petitioner;

        //Add sub so GuildMaster can Swear in.

        ArrayList<Guild> subGuildList = nation.getSubGuildList();
        subGuildList.add(swornGuild);

        //Let's send the message back.

        ChatManager.chatGuildInfo(sourcePlayer, "Your guild is now a " + swornGuild.guildState.name() + '.');
        msg.setUnknown02(1);
        msg.setResponse("Your guild is now a " + swornGuild.guildState.name() + '.');
        dispatch = Dispatch.borrow(sourcePlayer, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
        return true;
    }
}
