// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.mbEnums.DispatchChannel;
import engine.net.Dispatch;
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.net.client.msg.TransferItemFromInventoryToEquipMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Effect;
import engine.objects.Item;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;

public class TransferItemFromInventoryToEquipHandler extends AbstractClientMsgHandler {

    public TransferItemFromInventoryToEquipHandler() {
        super();
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();
        TransferItemFromInventoryToEquipMsg transferMsg = (TransferItemFromInventoryToEquipMsg) baseMsg;
        CharacterItemManager itemManager = playerCharacter.charItemManager;

        if (itemManager == null) {
            forceTransferFromEquipToInventory(transferMsg, origin, "Can't find your item manager");
            return true;
        }

        int itemUUID = transferMsg.getUUID();
        mbEnums.EquipSlotType equipSlot = transferMsg.slot;

        Item item = itemManager.getItemByUUID(itemUUID);

        if (item == null) {
            forceTransferFromEquipToInventory(transferMsg, origin, "Item not found in your item manager");
            return true;
        }

        if (!itemManager.doesCharOwnThisItem(item.getObjectUUID())) {
            forceTransferFromEquipToInventory(transferMsg, origin, "You do not own this item");
            return true;
        }

        //dupe check
        if (!item.validForInventory(origin, playerCharacter, itemManager))
            return true;

        if (item.containerType == mbEnums.ItemContainerType.INVENTORY) {
            if (!itemManager.equipItem(item, equipSlot)) {
                forceTransferFromEquipToInventory(transferMsg, origin, "Failed to transfer item.");
                return true;
            }
        } else {
            forceTransferFromEquipToInventory(transferMsg, origin, "This item is not in your inventory");
            return true;
        }

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

        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(playerCharacter.getObjectType().ordinal());
            pum.setSourceID(playerCharacter.getObjectUUID());
            pum.setTargetType(playerCharacter.getObjectType().ordinal());
            pum.setTargetID(playerCharacter.getObjectUUID());
            pum.setNumTrains(eff.getTrains());
            pum.setUnknown05(1);
            pum.setUnknown06((byte) 1);
            pum.setEffectSourceType(item.getObjectType().ordinal());
            pum.setEffectSourceID(item.getObjectUUID());
            pum.setDuration(-1);

            DispatchManager.dispatchMsgToInterestArea(playerCharacter, pum, DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);
        }

        return true;
    }

    private static void forceTransferFromEquipToInventory(TransferItemFromInventoryToEquipMsg msg, ClientConnection origin, String reason) {

        PlayerCharacter player = origin.getPlayerCharacter();

        if (player == null)
            return;

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

        ChatManager.chatInfoError(player, "Can't equip item: " + reason);
    }

}