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

package engine.net.client.handlers;

import engine.gameManager.ChatManager;
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.TradeRequestMsg;
import engine.objects.CharacterItemManager;
import engine.objects.PlayerCharacter;

public class TradeRequestMsgHandler extends AbstractClientMsgHandler {

    public TradeRequestMsgHandler() {
        super();
    }

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

        TradeRequestMsg msg = (TradeRequestMsg) baseMsg;

        PlayerCharacter source = origin.getPlayerCharacter();
        PlayerCharacter target = PlayerCharacter.getFromCache(msg.getPlayerID());
        Dispatch dispatch;

        if (!CharacterItemManager.canTrade(source, target)) {
            ChatManager.chatSystemError(source, "Can't currently trade with target player");
            return false;
        }

        // TODO uncomment this block after we determine when we
        // setBankOpen(false) and setVaultOpen(false)
        CharacterItemManager cim1 = source.charItemManager;
        CharacterItemManager cim2 = target.charItemManager;

        if (cim1 == null)
            return false;

        if (cim2 == null)
            return false;

        dispatch = Dispatch.borrow(target, msg);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
        return true;

    }

}