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

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.mbEnums;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.AddItemToTradeWindowMsg;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.UpdateTradeWindowMsg;
import engine.objects.CharacterItemManager;
import engine.objects.Item;
import engine.objects.PlayerCharacter;

import static engine.objects.CharacterItemManager.canTrade;

public class AddItemToTradeWindowMsgHandler extends AbstractClientMsgHandler {

    public AddItemToTradeWindowMsgHandler() {
        super();
    }

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

        // Member variable declaration

        AddItemToTradeWindowMsg msg;

        // Member variable assignment

        msg = (AddItemToTradeWindowMsg) baseMsg;

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

        if (source == null || !source.isAlive())
            return false;

        ClientConnection ccOther = source.charItemManager.tradingWith;

        if (ccOther == null)
            return false;

        PlayerCharacter other = ccOther.getPlayerCharacter();

        if (other == null || !other.isAlive())
            return false;

        CharacterItemManager tradingWith = other.charItemManager;

        if (tradingWith == null)
            return false;

        if (!canTrade(source, other))
            return false;

        Item item = Item.getFromCache(msg.getItemID());

        if (item == null)
            return false;

        if (!source.charItemManager.doesCharOwnThisItem(item.getObjectUUID()))
            return false;

        //can't add item to trade window twice
        if (source.charItemManager.tradingContains(item))
            return false;

        //dupe check
        if (!item.validForInventory(source.getClientConnection(), source, source.charItemManager))
            return false;

        if (!tradingWith.hasRoomTrade(item.template.item_wt)) {
            dispatch = Dispatch.borrow(source, msg);
            DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);
            return false;
        }

        UpdateTradeWindowMsg utwm = new UpdateTradeWindowMsg(source, other);

        source.charItemManager.setTradeCommitted((byte) 0);
        tradingWith.setTradeCommitted((byte) 0);

        source.charItemManager.addItemToTrade(item);

        dispatch = Dispatch.borrow(other, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        source.charItemManager.modifyCommitToTrade();

        dispatch = Dispatch.borrow(other, utwm);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        return true;
    }

}