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

import static engine.math.FastMath.sqr;
import static engine.objects.CharacterItemManager.canTrade;

public class AcceptTradeRequestMsgHandler extends AbstractClientMsgHandler {

    public AcceptTradeRequestMsgHandler() {
        super();
    }

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

        // Member variable declaration

        AcceptTradeRequestMsg msg;

        // Member variable assignment

        msg = (AcceptTradeRequestMsg) baseMsg;

        PlayerCharacter source = (PlayerCharacter) origin.getPlayerCharacter();
        PlayerCharacter target = PlayerCharacter.getFromCache(msg.getTargetID());

        Dispatch dispatch;

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

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

        if (source.charItemManager.tradingWith != null)
            return false;

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

        // verify characterTarget is in range
        if (source.getLoc().distanceSquared2D(target.getLoc()) > sqr(MBServerStatics.TRADE_RANGE))
            return false;

        // TODO uncomment this block after we determine when we
        // setBankOpen(false) and setVaultOpen(false)
        /*
         * CharacterItemManager cim1 = source.getCharItemManager();
         * CharacterItemManager cim2 = characterTarget.getCharItemManager(); if (cim1 ==
         * null) return false; if (cim2 == null) return false; if (cim1.isBankOpen())
         * return false; if (cim2.isVaultOpen()) return false;
         */
        ClientConnection sourceConn = origin;
        ClientConnection targetConn = target.getClientConnection();

        if (sourceConn == null)
            return false;

        if (targetConn == null)
            return false;

        CharacterItemManager toTradeWith = target.charItemManager;

        if (toTradeWith == null)
            return false;

        Account sourceAccount = source.getAccount();
        Account targetAccount = target.getAccount();

        UpdateVaultMsg uvmSource = new UpdateVaultMsg(sourceAccount);
        UpdateVaultMsg uvmTarget = new UpdateVaultMsg(targetAccount);

        dispatch = Dispatch.borrow(source, uvmSource);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        dispatch = Dispatch.borrow(target, uvmTarget);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        source.charItemManager.setVaultOpen(false);
        toTradeWith.setVaultOpen(false);
        source.charItemManager.setBankOpen(false);
        toTradeWith.setBankOpen(false);

        OpenTradeWindowMsg otwm = new OpenTradeWindowMsg(msg.getUnknown01(), source, target);

        // Only start trade if both players aren't already trading with
        // someone

        if (source.charItemManager.tradingWith != null || toTradeWith.getTradingWith() != null)
            return false;

        source.charItemManager.initializeTrade();
        toTradeWith.initializeTrade();
        source.charItemManager.setTradingWith(targetConn);
        toTradeWith.setTradingWith(sourceConn);
        source.charItemManager.tradeID = msg.getUnknown01();
        toTradeWith.tradeID = msg.getUnknown01();

        dispatch = Dispatch.borrow(source, otwm);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        dispatch = Dispatch.borrow(target, otwm);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);

        return true;
    }

}