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

package engine.net.client.handlers;

import engine.gameManager.ChatManager;
import engine.gameManager.DispatchManager;
import engine.mbEnums;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ItemFromVaultMsg;
import engine.net.client.msg.ItemToVaultMsg;
import engine.objects.PlayerCharacter;

public class ItemFromVaultMsgHandler extends AbstractClientMsgHandler {

    public ItemFromVaultMsgHandler() {
        super();
    }

    //call this if the transfer fails server side to kick the item back to inventory from vault
    public static void forceTransferFromInventoryToVault(ItemFromVaultMsg msg, ClientConnection origin, String reason) {

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

        if (player == null)
            return;

        ItemToVaultMsg back = new ItemToVaultMsg(msg);
        dispatch = Dispatch.borrow(player, back);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

        ChatManager.chatInfoError(player, "Can't transfer to inventory: " + reason);
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();

        ItemFromVaultMsg msg = (ItemFromVaultMsg) baseMsg;

        if (player == null)
            return true;
        ;

        if (player.getAccount() == null)
            return true;
        ;

        player.getAccount().transferItemFromVaultToInventory(msg, origin);

        return true;
    }

}