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

package engine.net.client.handlers;

import engine.gameManager.ChatManager;
import engine.gameManager.ConfigManager;
import engine.gameManager.SessionManager;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.PerformActionMsg;
import engine.net.client.msg.SendSummonsMsg;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;

import static engine.gameManager.PowersManager.*;

public class SendSummonsMsgHandler extends AbstractClientMsgHandler {

    public SendSummonsMsgHandler() {
        super();
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        SendSummonsMsg msg;

        // Member variable assignment

        msg = (SendSummonsMsg) baseMsg;

        if (playerCharacter == null)
            return true;

        PlayerCharacter target = SessionManager
                .getPlayerCharacterByLowerCaseName(msg.getTargetName());

        if (target == null || target.equals(playerCharacter) || target.isCombat()) {

            if (target == null) // Player not found. Send not found message
                ChatManager.chatInfoError(playerCharacter,
                        "Cannot find that player to summon.");
            else if (target.isCombat())
                ChatManager.chatInfoError(playerCharacter,
                        "Cannot summon player in combat.");

            // else trying to summon self, just fail

            // recycle summon
            sendRecyclePower(msg.getPowerToken(), origin);

            // TODO: client already subtracted 200 mana.. need to correct it
            // end cast
            PerformActionMsg pam = new PerformActionMsg(msg.getPowerToken(),
                    msg.getTrains(), msg.getSourceType(), msg.getSourceID(), 0,
                    0, 0f, 0f, 0f, 1, 0);
            sendPowerMsg(playerCharacter, 2, pam);

            return true;
        }
        if (ConfigManager.MB_RULESET.getValue().equals("LORE")) {
            if (playerCharacter.guild.getGuildType().equals(target.guild.getGuildType()) == false) {
                ChatManager.chatInfoError(playerCharacter,
                        "Cannot summon player outside your charter.");
                return true;
            }
        }
        PerformActionMsg pam = new PerformActionMsg(msg.getPowerToken(), msg
                .getTrains(), msg.getSourceType(), msg.getSourceID(), target
                .getObjectType().ordinal(), target.getObjectUUID(), 0f, 0f, 0f, 1, 0);

        // Client removes 200 mana on summon use.. so don't send message to self
        target.addSummoner(playerCharacter.getObjectUUID(), System.currentTimeMillis() + MBServerStatics.FOURTYFIVE_SECONDS);
        usePower(pam, origin, false);

        return true;
    }

}