package be.yurimoens.runemate.util;

import com.runemate.game.api.hybrid.entities.details.Interactable;
import com.runemate.game.api.hybrid.input.Mouse;
import com.runemate.game.api.hybrid.local.hud.InteractablePoint;
import com.runemate.game.api.hybrid.local.hud.Menu;
import com.runemate.game.api.hybrid.local.hud.MenuItem;
import com.runemate.game.api.hybrid.local.hud.interfaces.Interfaces;
import com.runemate.game.api.hybrid.util.calculations.Random;
import com.runemate.game.api.script.Execution;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CMouse {

    public static boolean fastInteract(Interactable interactable, String action) {
        if (interactable == null) {
            return false;
        }
        Mouse.move(interactable);
        return interact(interactable, action);
    }

    public static String getTooltip() {
        List<MenuItem> menuItems;

        return (menuItems = Menu.getItems()).isEmpty() ? "" : menuItems.get(0).getAction();
    }

    public static void moveRandomFromPosition() {
        moveRandomFromPosition(40, 40);
    }

    public static void moveRandomFromPosition(int xRandom, int yRandom) {
        concurrentlyMove(new InteractablePoint((int) Mouse.getPosition().getX() + Random.nextInt(-xRandom, xRandom), (int) Mouse.getPosition().getY() + Random.nextInt(-yRandom, yRandom)));
    }

    public static Future<Boolean> concurrentlyMove(Interactable target) {
        if (target == null || target.getInteractionPoint() == null) {
            return new FutureTask<>(() -> false);
        }

        FutureTask task = new FutureTask(() -> Mouse.move(target));

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(task);

        return task;
    }

    public static void moveToMinimap() {
        concurrentlyMove(Interfaces.getAt(1465, 0).getInteractionPoint());
    }

    private static boolean interact(Interactable interactable, String action) {
        Execution.delayUntil(() -> getTooltip().contains(action), 350, 550);

        if (getTooltip().contains(action)) {
            return interactable.click();
        } else {
            return interactable.interact(action);
        }
    }

}

@@ -1274,30 +1274,411 @@
 nt x
-Random, int yRandom) %7B
+, int y) %7B%0A        moveRandomFromPosition(0, x, 0, y);%0A    %7D%0A%0A    public static void moveRandomFromPosition(int xLower, int xUpper, int yLower, int yUpper) %7B%0A        int x = (int) Random.nextGaussian((double) xLower, (double) xUpper);%0A        int y = (int) Random.nextGaussian((double) yLower, (double) yUpper);%0A%0A        if (Random.nextInt(2) == 1) %7B%0A            x *= -1;%0A            y *= -1;%0A        %7D%0A
 %0A   
@@ -1760,41 +1760,9 @@
 ) + 
-Random.nextInt(-xRandom, xRandom)
+x
 , (i
@@ -1798,41 +1798,9 @@
 ) + 
-Random.nextInt(-yRandom, yRandom)
+y
 ));%0A

Refactor moveRandomFromPosition().