// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.GuildHistoryType;
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.AcceptInviteToGuildMsg;
import engine.net.client.msg.guild.GuildInfoMsg;
import engine.objects.Guild;
import engine.objects.GuildHistory;
import engine.objects.PlayerCharacter;
import org.joda.time.DateTime;

public class AcceptInviteToGuildHandler extends AbstractClientMsgHandler {

    public AcceptInviteToGuildHandler() {
        super();
    }

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

        PlayerCharacter player;
        AcceptInviteToGuildMsg msg;
        Guild guild;

        msg = (AcceptInviteToGuildMsg) baseMsg;

        // get PlayerCharacter of person accepting invite

        player = SessionManager.getPlayerCharacter(origin);

        if (player == null)
            return true;

        guild = (Guild) DbManager.getObject(GameObjectType.Guild, msg.getGuildUUID());


        if (guild == null)
            return true;

        if (guild.getGuildType() == null) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.NO_CHARTER_FOUND);
            return true;
        }

        // verify they accepted for the correct guild

        if (player.getLastGuildToInvite() != msg.getGuildUUID())
            return true;

        if ((player.getGuild() != null) &&
                (player.getGuild().isEmptyGuild() == false)) {
            ChatManager.chatGuildError(player,
                    "You already belongs to a guild!");
            return true;
        }

        // verify they are acceptable level for guild

        if (player.getLevel() < guild.getRepledgeMin() || player.getLevel() > guild.getRepledgeMax())
            return true;

        // Add player to guild
        player.setGuild(guild);

        // Cleanup guild stuff
        player.resetGuildStatuses();

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

        DispatchManager.sendToAllInRange(player, new GuildInfoMsg(player, guild, 2));

        player.incVer();

        //Add to guild History

        if (player.getGuild() != null) {
            if (DbManager.GuildQueries.ADD_TO_GUILDHISTORY(player.getGuildUUID(), player, DateTime.now(), GuildHistoryType.JOIN)) {
                GuildHistory guildHistory = new GuildHistory(player.getGuildUUID(), player.getGuild().getName(), DateTime.now(), GuildHistoryType.JOIN);
                player.getGuildHistory().add(guildHistory);
            }
        }

        // Send guild join message

        ChatManager.chatGuildInfo(player, player.getFirstName() + " has joined the guild");
        return true;
    }

}
