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

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.mbEnums.DispatchChannel;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.DeleteItemMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
import engine.objects.PlayerCharacter;

public class DeleteItemMsgHandler extends AbstractClientMsgHandler {

    public DeleteItemMsgHandler() {
        super();
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();

        // Member variable declaration

        DeleteItemMsg msg;

        // Member variable assignment

        msg = (DeleteItemMsg) baseMsg;
        CharacterItemManager itemManager = origin.getPlayerCharacter().charItemManager;
        int uuid = msg.getUUID();

        if (player == null)
            return true;

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

        Item item = Item.getFromCache(msg.getUUID());

        if (item == null)
            return true;

        if (!itemManager.doesCharOwnThisItem(item.getObjectUUID()))
            return true;

        if (!itemManager.inventoryContains(item))
            return true;

        if (item.isCanDestroy())
            if (itemManager.delete(item) == true) {
                Dispatch dispatch = Dispatch.borrow(player, msg);
                DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
            }

        return true;
    }

}