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

package engine.net.client.handlers;

import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.UncommitToTradeMsg;
import engine.objects.CharacterItemManager;
import engine.objects.PlayerCharacter;

import static engine.objects.CharacterItemManager.canTrade;

public class UncommitToTradeMsgHandler extends AbstractClientMsgHandler {

    public UncommitToTradeMsgHandler() {
        super();
    }

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

        PlayerCharacter source = origin.getPlayerCharacter();

        // Member variable declaration

        UncommitToTradeMsg msg;

        // Member variable assignment

        msg = (UncommitToTradeMsg) baseMsg;

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

        CharacterItemManager sourceItemMan = source.charItemManager;

        if (sourceItemMan == null)
            return true;

        sourceItemMan.setTradeCommitted((byte) 0);

        ClientConnection ccOther = sourceItemMan.getTradingWith();

        if (ccOther == null)
            return true;

        PlayerCharacter other = ccOther.getPlayerCharacter();

        if (other == null)
            return true;

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

        source.charItemManager.modifyCommitToTrade();

        return true;
    }

}