//  • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
//  ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
//  ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
//  ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
//  ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2026
//                https://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.ErrorPopupMsg;
import engine.net.client.msg.TransferItemToBankMsg;
import engine.net.client.msg.UpdateGoldMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
import engine.objects.PlayerCharacter;

public class TransferItemToBankMsgHandler extends AbstractClientMsgHandler {

    public TransferItemToBankMsgHandler() {
        super();
    }

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

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

        if (player == null)
            return true;

        TransferItemToBankMsg msg = (TransferItemToBankMsg) baseMsg;

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

        CharacterItemManager itemManager = player.charItemManager;

        if (itemManager == null)
            return true;

        if (itemManager.getBankWeight() > 500) {
            ErrorPopupMsg.sendErrorPopup(player, 21);
            return true;
        }

        int uuid = msg.getUUID();

        Item item = itemManager.getItemByUUID(uuid);

        if (item == null)
            return true;

        //dupe check  WTF CHECK BUT NO LOGGING?

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

        if (item.containerType == mbEnums.ItemContainerType.INVENTORY && itemManager.isBankOpen())
            if (item.template.item_type.equals(mbEnums.ItemType.GOLD)) {
                if (!itemManager.moveGoldToBank(item, msg.getNumItems()))
                    return true;
                UpdateGoldMsg goldMes = new UpdateGoldMsg(player);
                goldMes.configure();

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

            } else {

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

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

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

            }
        return true;
    }

}