// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.ClientNetMsg;
import engine.net.client.msg.CloseTradeWindowMsg;
import engine.objects.CharacterItemManager;
import engine.objects.PlayerCharacter;

public class CloseTradeWindowMsgHandler extends AbstractClientMsgHandler {

    public CloseTradeWindowMsgHandler() {
        super();
    }

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

        PlayerCharacter source = origin.getPlayerCharacter();

        // Member variable declaration

        CloseTradeWindowMsg msg;

        // Member variable assignment

        msg = (CloseTradeWindowMsg) baseMsg;

        Dispatch dispatch;

        if (source == null)
            return false;

        CharacterItemManager sourceItemMan = source.charItemManager;

        if (sourceItemMan == null)
            return false;

        int tradeID = source.charItemManager.tradeID;
        CloseTradeWindowMsg closeMsg = new CloseTradeWindowMsg(source, tradeID);

        dispatch = Dispatch.borrow(source, closeMsg);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

        ClientConnection cc2 = sourceItemMan.getTradingWith();

        if (cc2 == null || cc2.getPlayerCharacter() == null) {
            sourceItemMan.endTrade();
            return true;
        }

        sourceItemMan.endTrade();
        cc2.getPlayerCharacter().charItemManager.closeTradeWindow(msg, false);

        return true;
    }

}