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

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.gameManager.NPCManager;
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.TransferItemFromBankMsg;
import engine.net.client.msg.UpdateGoldMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
import engine.objects.PlayerCharacter;

public class TransferItemFromBankMsgHandler extends AbstractClientMsgHandler {

    public TransferItemFromBankMsgHandler() {
        super();
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();
        Dispatch dispatch;

        if (player == null)
            return true;
        ;

        TransferItemFromBankMsg msg = (TransferItemFromBankMsg) baseMsg;

        if (!NPCManager.NPCVaultBankRangeCheck(player, origin, "bank"))
            return true;
        ;

        CharacterItemManager itemManager = player.charItemManager;

        if (itemManager == null)
            return true;
        ;

        int uuid = msg.getUUID();

        Item item = itemManager.getItemByUUID(uuid);

        if (item == null)
            return true;
        ;

        //dupe check
        // WTF Checking but not logging?

        if (!item.validForBank(origin, player, itemManager))
            return true;
        ;

        if (item.containerType == mbEnums.ItemContainerType.BANK && itemManager.isBankOpen() == false)
            return true;
        ;

        if (item.template.item_type.equals(mbEnums.ItemType.GOLD)) {

            if (!itemManager.moveGoldToInventory(item, msg.getNumItems()))
                return true;
            ;

            UpdateGoldMsg goldMes = new UpdateGoldMsg(player);
            goldMes.configure();

            dispatch = Dispatch.borrow(player, goldMes);
            DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

            return true;
        }

        // Not gold, process update here

        if (!itemManager.hasRoomInventory(item.template.item_wt))
            return true;

        if (itemManager.moveItemToInventory(item) == false)
            return true;

        dispatch = Dispatch.borrow(player, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
        return true;
    }

}