// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      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.job.JobContainer;
import engine.job.JobScheduler;
import engine.jobs.FinishSummonsJob;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ErrorPopupMsg;
import engine.net.client.msg.RecvSummonsMsg;
import engine.objects.BaseClass;
import engine.objects.PlayerCharacter;
import engine.objects.PromotionClass;

import java.util.concurrent.ConcurrentHashMap;

public class RecvSummonsMsgHandler extends AbstractClientMsgHandler {

    public RecvSummonsMsgHandler() {
        super(RecvSummonsMsg.class);
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        RecvSummonsMsg msg;

        // Member variable assignment

        msg = (RecvSummonsMsg) baseMsg;

        if (playerCharacter == null)
            return true;

        PlayerCharacter source = PlayerCharacter.getFromCache(msg.getSourceID());

        if (source == null)
            return true;

        long tooLate = playerCharacter.getSummoner(source.getObjectUUID());

        if (tooLate < System.currentTimeMillis()) {
            ChatManager.chatInfoError(playerCharacter, "You waited too long to " + (msg.accepted() ? "accept" : "decline") + " the summons.");
            playerCharacter.removeSummoner(source.getObjectUUID());
            return true;
        }

        if (playerCharacter.getBonuses() != null && playerCharacter.getBonuses().getBool(Enum.ModType.BlockedPowerType, Enum.SourceType.SUMMON)) {
            ErrorPopupMsg.sendErrorMsg(playerCharacter, "You have been blocked from receiving summons!");
            ErrorPopupMsg.sendErrorMsg(source, "Target is blocked from receiving summons!");
            playerCharacter.removeSummoner(source.getObjectUUID());
            return true;
        }

        playerCharacter.removeSummoner(source.getObjectUUID());

        // Handle Accepting or Denying a summons.
        // set timer based on summon type.

        boolean wentThrough = false;

        if (msg.accepted())   // summons accepted, let's move the player if within time
            if (source.isAlive()) {

                //make sure summons handled in time
                ConcurrentHashMap<String, JobContainer> timers;
                timers = playerCharacter.getTimers();

                if (timers != null && timers.containsKey("Summon"))
                    timers.get("Summon").cancelJob();

                // get time to wait before summons goes through

                BaseClass base = source.baseClass;
                PromotionClass promo = source.getPromotionClass();
                int duration;

                //determine if in combat with another player

                //comment out this block to disable combat timer
                //				if (lastAttacked < 60000) {
                //					if (pc.inSafeZone()) //player in safe zone, no need for combat timer
                //						combat = false;
                //					else if (source.inSafeZone()) //summoner in safe zone, apply combat timer
                //						combat = true;
                //					else if ((source.getLoc().distance2D(pc.getLoc())) > 6144f)
                //						combat = true; //more than 1.5x width of zone, not tactical summons
                //				}

                if (promo != null && promo.getObjectUUID() == 2519)
                    duration = 10000; // Priest summons, 10 seconds
                else if (base != null && base.getObjectUUID() == 2501)
                    duration = 15000; // Healer Summons, 15 seconds
                else
                    duration = 45000; // Belgosh Summons, 45 seconds

                // Teleport to summoners location

                FinishSummonsJob fsj = new FinishSummonsJob(source, playerCharacter);
                JobContainer jc = JobScheduler.getInstance().scheduleJob(fsj,
                        duration);

                if (timers != null)
                    timers.put("Summon", jc);

                wentThrough = true;
            }

        // Summons failed
        if (!wentThrough)    // summons refused. Let's be nice and reset recycle timer
        {
            // Send summons refused Message
            ErrorPopupMsg.sendErrorPopup(source, 29);
            // recycle summons power
            //finishRecycleTime(428523680, source, true);
        }

        return true;
    }

}