// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.AddGoldToTradeWindowMsg;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.UpdateGoldMsg;
import engine.net.client.msg.UpdateTradeWindowMsg;
import engine.objects.CharacterItemManager;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

import static engine.objects.CharacterItemManager.canTrade;

public class AddGoldToTradeWindowMsgHandler extends AbstractClientMsgHandler {

    public AddGoldToTradeWindowMsgHandler() {
        super();
    }

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

        PlayerCharacter source = origin.getPlayerCharacter();

        // Member variable declaration

        AddGoldToTradeWindowMsg msg;

        // Member variable assignment

        msg = (AddGoldToTradeWindowMsg) baseMsg;
        Dispatch dispatch;

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

        ClientConnection ccOther = source.charItemManager.tradingWith;

        if (ccOther == null)
            return false;

        PlayerCharacter other = ccOther.getPlayerCharacter();

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

        CharacterItemManager tradingWith = other.charItemManager;

        if (tradingWith == null)
            return false;

        UpdateTradeWindowMsg utwm = new UpdateTradeWindowMsg(other, source);
        UpdateTradeWindowMsg utwmOther = new UpdateTradeWindowMsg(source, other);

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

        source.charItemManager.setTradeCommitted((byte) 0);
        tradingWith.setTradeCommitted((byte) 0);

        int amt = msg.getAmount();

        if (amt <= 0) {
            Logger.info(source.getFirstName() + " added negative gold to trade window. Dupe attempt FAILED!");
            return false;
        }

        if (amt > MBServerStatics.PLAYER_GOLD_LIMIT)
            return false;

        if (source.charItemManager.getGoldInventory().getNumOfItems() - amt < 0)
            return false;

        source.charItemManager.addGoldToTrade(amt);

        // BONUS CODE BELOW:  Thanks some unknown retard!
        //		sourceItemMan.updateInventory(sourceItemMan.getInventory(), true);

        UpdateGoldMsg ugm = new UpdateGoldMsg(source);
        ugm.configure();

        source.charItemManager.modifyCommitToTrade();

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

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

        dispatch = Dispatch.borrow(other, utwmOther);
        DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

        return true;
    }

}