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

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.gameManager.MovementManager;
import engine.job.JobScheduler;
import engine.jobs.RefreshGroupJob;
import engine.mbEnums.DispatchChannel;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.MoveToPointMsg;
import engine.net.client.msg.RespawnMsg;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

public class RespawnMsgHandler extends AbstractClientMsgHandler {

    public RespawnMsgHandler() {
        super();
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        RespawnMsg msg;

        // Member variable assignment

        msg = (RespawnMsg) baseMsg;

        if (playerCharacter == null)
            return true;

        if (msg.getObjectType() != playerCharacter.getObjectType().ordinal() || msg.getObjectID() != playerCharacter.getObjectUUID()) {
            Logger.error("Player " + playerCharacter.getObjectUUID() + " respawning character of id " + msg.getObjectType() + ' '
                    + msg.getObjectID());
            return true;
        }

        if (playerCharacter.isAlive()) {
            Logger.error("Player " + playerCharacter.getObjectUUID() + " respawning while alive");
            return true;
        }

        // ResetAfterDeath player
        playerCharacter.respawnLock.writeLock().lock();
        try {
            playerCharacter.respawn(true, false, true);

        } catch (Exception e) {
            Logger.error(e);
        } finally {
            playerCharacter.respawnLock.writeLock().unlock();

        }
        // Echo ResetAfterDeath message back
        msg.setPlayerHealth(playerCharacter.getHealth());
        // TODO calculate any experience loss before this point
        msg.setPlayerExp(playerCharacter.getExp() + playerCharacter.getOverFlowEXP());
        Dispatch dispatch = Dispatch.borrow(playerCharacter, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.PRIMARY);

        MoveToPointMsg moveMsg = new MoveToPointMsg();
        moveMsg.setPlayer(playerCharacter);
        moveMsg.setStartCoord(playerCharacter.getLoc());
        moveMsg.setEndCoord(playerCharacter.getLoc());
        moveMsg.setInBuilding(-1);
        moveMsg.setInBuildingFloor(-1);

        dispatch = Dispatch.borrow(playerCharacter, moveMsg);
        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.PRIMARY);

        MovementManager.sendRWSSMsg(playerCharacter);

        // refresh the whole group with what just happened
        JobScheduler.getInstance().scheduleJob(new RefreshGroupJob(playerCharacter), MBServerStatics.LOAD_OBJECT_DELAY);

        return true;
    }

}