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

package engine.net.client.handlers;

import engine.Enum;
import engine.exception.MsgSendException;
import engine.gameManager.ChatManager;
import engine.net.Dispatch;
import engine.net.DispatchMessage;
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(TradeRequestMsg.class);
    }

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

        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);
        DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY);
        return true;

    }

}