package be.yurimoens.runemate.cabysscrafter.task.walking;

import be.yurimoens.runemate.cabysscrafter.Constants;
import com.runemate.game.api.hybrid.entities.Actor;
import com.runemate.game.api.hybrid.entities.Npc;
import com.runemate.game.api.hybrid.entities.Player;
import com.runemate.game.api.hybrid.location.Coordinate;
import com.runemate.game.api.hybrid.location.navigation.basic.PredefinedPath;
import com.runemate.game.api.hybrid.region.Npcs;
import com.runemate.game.api.hybrid.region.Players;
import com.runemate.game.api.rs3.local.hud.interfaces.eoc.ActionBar;
import com.runemate.game.api.rs3.local.hud.interfaces.eoc.SlotAction;
import com.runemate.game.api.script.Execution;
import com.runemate.game.api.script.framework.task.Task;

class WalkToMage extends Task {

    private final SlotAction surge;

    public WalkToMage() {
        surge = ActionBar.getFirstAction("Surge");
    }

    @Override
    public boolean validate() {
        Player player = Players.getLocal();

        return (getParent().validate()
                && !((WalkToAbyss) getParent()).underAttack
                && (player.getPosition().getY() >= Constants.wildernessWall.getY() || player.getAnimationId() == 18224)
                && !Constants.mageArea.contains(player));
    }

    @Override
    public void execute() {
        Player player = Players.getLocal();

        if (surge != null && !surge.isCoolingDown()) {
            surge.activate(false);
            Execution.delayUntil(surge::isCoolingDown, 1000);
        }

        PredefinedPath path = PredefinedPath.create(
                new Coordinate(3103, 3545, 0).randomize(2, 2),
                new Coordinate(3102, 3558, 0).randomize(2, 2)
        );

        Execution.delayUntil(() -> {
            if (!player.isMoving() || player.distanceTo(path.getNext()) < 17.5D) {
                path.step(true);
                Execution.delayUntil(player::isMoving, 1500, 2500);
            }

            for (Player otherPlayers : Players.getLoaded()) {
                Actor target;
                if ((target = otherPlayers.getTarget()) != null && target.getName() != null && target.getName().equals(player.getName())) {
                    ((WalkToAbyss) getParent()).underAttack = true;
                    return false;
                }
            }

            Npc zamorakMage = Npcs.newQuery().models(Constants.MAGE_MODEL).results().first();

            return Constants.mageArea.contains(player)
                    || (zamorakMage != null && zamorakMage.isVisible());
        }, 10000, 13000);
    }
}

@@ -261,16 +261,132 @@
 Player;%0A
+import com.runemate.game.api.hybrid.entities.details.Interactable;%0Aimport com.runemate.game.api.hybrid.input.Mouse;%0A
 import c
@@ -434,16 +434,16 @@
 dinate;%0A
-
 import c
@@ -857,16 +857,177 @@
 .Task;%0A%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.FutureTask;%0A%0A
 class Wa
@@ -1789,32 +1789,50 @@
 00);%0A        %7D%0A%0A
+        surge();%0A%0A
         Predefin
@@ -2827,24 +2827,24 @@
 Visible());%0A
-
         %7D, 1
@@ -2862,11 +2862,383 @@
 );%0A    %7D
+%0A%0A    private Future%3CBoolean%3E surge() %7B%0A        if (surge == null %7C%7C surge.isCoolingDown()) %7B%0A            return new FutureTask%3C%3E(() -%3E false);%0A        %7D%0A%0A        FutureTask task = new FutureTask(() -%3E surge.activate(false));%0A%0A        ExecutorService executorService = Executors.newSingleThreadExecutor();%0A%0A        executorService.submit(task);%0A%0A        return task;%0A    %7D
 %0A%7D%0A

Optimize usage of surge.