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

package engine.net.client.handlers;

import engine.InterestManagement.WorldGrid;
import engine.gameManager.DispatchManager;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
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.LootWindowRequestMsg;
import engine.net.client.msg.LootWindowResponseMsg;
import engine.objects.*;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

import static engine.math.FastMath.sqr;

public class LootWindowRequestMsgHandler extends AbstractClientMsgHandler {

    public LootWindowRequestMsgHandler() {
        super();
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();

        // Member variable declaration

        LootWindowRequestMsg msg;

        // Member variable assignment

        msg = (LootWindowRequestMsg) baseMsg;

        if (player == null)
            return true;

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

        if (msg.getSourceType() != player.getObjectType().ordinal() || msg.getSourceID() != player.getObjectUUID()) {
            Logger.error("Player " + player.getObjectUUID() + " looting from character of id "
                    + msg.getSourceType() + ' ' + msg.getSourceID());
            return true;
        }

        if (player.getAltitude() > 0)
            return true;

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

        LootWindowResponseMsg lwrm = null;
        mbEnums.GameObjectType targetType = mbEnums.GameObjectType.values()[msg.getTargetType()];
        AbstractCharacter characterTarget = null;
        Corpse corpseTarget;

        switch (targetType) {
            case PlayerCharacter:

                characterTarget = PlayerCharacter.getFromCache(msg.getTargetID());

                if (characterTarget == null)
                    return true;

                if (characterTarget.isAlive())
                    return true;

                if (player.getLoc().distanceSquared2D(characterTarget.getLoc()) > sqr(MBServerStatics.LOOT_RANGE)) {
                    ErrorPopupMsg.sendErrorMsg(player, "You are too far away to loot this corpse.");
                    Logger.info(player.getFirstName() + " tried looting at " + player.getLoc().distance2D(characterTarget.getLoc()) + " distance.");
                    return true;
                }

                lwrm = new LootWindowResponseMsg(characterTarget.getObjectType().ordinal(), characterTarget.getObjectUUID(), characterTarget.getInventory(true));
                break;
            case NPC:
                characterTarget = NPC.getFromCache(msg.getTargetID());

                if (characterTarget == null)
                    return true;
                break;
            case Mob:
                characterTarget = Mob.getFromCache(msg.getTargetID());

                if ((characterTarget == null) || characterTarget.isAlive())
                    return true;

                if (player.getLoc().distanceSquared2D(characterTarget.getLoc()) > sqr(MBServerStatics.LOOT_RANGE)) {
                    ErrorPopupMsg.sendErrorMsg(player, "You are too far away to loot this corpse.");

                    Logger.info(player.getFirstName() + " tried looting at " + player.getLoc().distance2D(characterTarget.getLoc()) + " distance.");

                    if (!((Mob) characterTarget).isLootSync()) {
                        ((Mob) characterTarget).setLootSync(true);
                        WorldGrid.updateObject(characterTarget, player);
                    }

                    return true;
                }

                lwrm = new LootWindowResponseMsg(characterTarget.getObjectType().ordinal(), characterTarget.getObjectUUID(), characterTarget.getInventory());
                break;
            case Corpse:
                corpseTarget = Corpse.getCorpse(msg.getTargetID());

                if ((corpseTarget == null))
                    return true;

                if (player.getLoc().distanceSquared(corpseTarget.getLoc()) > sqr(MBServerStatics.LOOT_RANGE)) {
                    ErrorPopupMsg.sendErrorMsg(player, "You are too far away to loot this corpse.");
                    Logger.info(player.getFirstName() + " tried looting at " + player.getLoc().distance2D(characterTarget.getLoc()) + " distance.");
                    return true;
                }
                lwrm = new LootWindowResponseMsg(corpseTarget.getObjectType().ordinal(), msg.getTargetID(), corpseTarget.getInventory());
                break;
        }

        if (lwrm == null)
            return true;

        DispatchManager.dispatchMsgToInterestArea(player, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);
        Dispatch dispatch = Dispatch.borrow(player, lwrm);
        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.PRIMARY);

        return true;
    }

}