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

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ApplyEffectMsg;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.TransferItemFromEquipToInventoryMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Effect;
import engine.objects.Item;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;

public class TransferItemFromEquipToInventoryHandler extends AbstractClientMsgHandler {

    public TransferItemFromEquipToInventoryHandler() {
        super();
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();

        if (player == null)
            return true;

        TransferItemFromEquipToInventoryMsg msg = (TransferItemFromEquipToInventoryMsg) baseMsg;

        CharacterItemManager itemManager = player.charItemManager;

        if (itemManager == null)
            return true;

        mbEnums.EquipSlotType slot = msg.slot;

        Item item = itemManager.getItemFromEquipped(slot);

        if (item == null)
            return true;

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

        //dupe check

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

        if (item.containerType == mbEnums.ItemContainerType.EQUIPPED)
            itemManager.moveItemToInventory(item);

        int ItemType = item.getObjectType().ordinal();
        int ItemID = item.getObjectUUID();

        for (String name : item.getEffects().keySet()) {

            Effect eff = item.getEffects().get(name);

            if (eff == null)
                return true;

            ApplyEffectMsg pum = new ApplyEffectMsg();

            pum.setEffectID(eff.getEffectToken());
            pum.setSourceType(player.getObjectType().ordinal());
            pum.setSourceID(player.getObjectUUID());
            pum.setTargetType(player.getObjectType().ordinal());
            pum.setTargetID(player.getObjectUUID());
            pum.setNumTrains(eff.getTrains());
            pum.setUnknown05(1);
            pum.setUnknown02(2);
            pum.setUnknown06((byte) 1);
            pum.setEffectSourceType(ItemType);
            pum.setEffectSourceID(ItemID);
            pum.setDuration(-1);

            DispatchManager.dispatchMsgToInterestArea(player, pum, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true, false);

        }

        // Update player formulas
        player.applyBonuses();
        DispatchManager.dispatchMsgToInterestArea(player, msg, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);

        return true;
    }

}