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

package engine.net.client.handlers;

import engine.gameManager.SessionManager;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.login.GameServerIPRequestMsg;
import engine.net.client.msg.login.GameServerIPResponseMsg;
import engine.objects.Account;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import engine.server.login.LoginServer;
import engine.session.CSSession;
import engine.session.Session;
import engine.util.ByteUtils;
import org.pmw.tinylog.Logger;

public class GameServerIPRequestMsgHandler extends AbstractClientMsgHandler {

    public GameServerIPRequestMsgHandler() {
        super();
    }

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

        Session session = SessionManager.getSession(origin);

        // Member variable declaration

        GameServerIPRequestMsg msg;

        // Member variable assignment

        msg = (GameServerIPRequestMsg) baseMsg;

        PlayerCharacter playerCharacter = PlayerCharacter.getPlayerCharacter(msg.getCharacterUUID());

        if (playerCharacter == null) {
            Logger.info("Unable to find character ID " + msg.getCharacterUUID());
            LoginServer.KickToLogin(MBServerStatics.LOGINERROR_UNABLE_TO_LOGIN, "PlayerCharacter lookup failed in .RequestGameServer().", origin);
            return true;
        }

        try {
            if (!CSSession.updateCrossServerSession(ByteUtils.byteArrayToSafeStringHex(origin.getSecretKeyBytes()), msg.getCharacterUUID())) {
                Logger.info("Failed to update Cross server session, Kicking to Login for Character " + playerCharacter.getObjectUUID());
                LoginServer.KickToLogin(MBServerStatics.LOGINERROR_UNABLE_TO_LOGIN, "Failed to update Session Information", origin);
                return true;
            }
        } catch (Exception e) {
            Logger.info("Failed to update Cross server session, Kicking to Login for Character " + playerCharacter.getObjectUUID());
            Logger.error(e);
        }

        // Set the last character used.
        Account account = session.getAccount();
        account.setLastCharIDUsed(msg.getCharacterUUID());

        GameServerIPResponseMsg gameServerIPResponseMsg = new GameServerIPResponseMsg();

        if (!origin.sendMsg(gameServerIPResponseMsg)) {
            Logger.error("Failed to send message");
            LoginServer.KickToLogin(MBServerStatics.LOGINERROR_UNABLE_TO_LOGIN, "Unable to send GameServerIPResponseMsg to client.", origin);
        }


        return true;
    }

}