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

package engine.net.client.handlers;

import engine.InterestManagement.WorldGrid;
import engine.gameManager.DispatchManager;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.PetCmdMsg;
import engine.net.client.msg.UpdateStateMsg;
import engine.objects.Mob;
import engine.objects.PlayerCharacter;

public class PetCmdMsgHandler extends AbstractClientMsgHandler {

    public PetCmdMsgHandler() {
        super();
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        PetCmdMsg msg;

        // Member variable assignment

        msg = (PetCmdMsg) baseMsg;

        if (playerCharacter == null)
            return true;

        Mob pet = playerCharacter.getPet();

        if (pet == null)
            return true;

        if (!pet.isAlive())
            return true;

        //if (pet.state == STATE.Disabled)
        //	return;

        int type = msg.getType();

        if (type == 1) { //stop attack
            pet.setCombatTarget(null);
            playerCharacter.setCombat(false);

        } else if (type == 2) { //dismiss
            pet.dismiss();
            playerCharacter.dismissPet();

            if (pet.isAlive())
                WorldGrid.updateObject(pet);
        } else if (type == 3) //toggle assist
            pet.toggleAssist();
        else if (type == 5) { //rest
            boolean sit = (!(pet.isSit()));
            pet.setSit(sit);

            // cancel effects that break on sit
            if (pet.isSit())
                pet.cancelOnSit();

            UpdateStateMsg rwss = new UpdateStateMsg();
            rwss.setPlayer(pet);
            DispatchManager.sendToAllInRange(pet, rwss);
        }

        return true;
    }

}