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.Camera;
import com.runemate.game.api.hybrid.local.hud.InteractablePoint;
import com.runemate.game.api.hybrid.local.hud.interfaces.InterfaceComponent;
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.awt.*;
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() {
        InterfaceComponent tooltip = Interfaces.getAt(1477, 805, 0);
        if (tooltip == null || !tooltip.isValid()) {
            return "";
        } else {
            return tooltip.getText();
        }
    }

    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);
        }
    }

}

@@ -192,14 +192,29 @@
 cal.
-Camera
+hud.InteractablePoint
 ;%0Aim
@@ -257,33 +257,20 @@
 cal.hud.
-InteractablePoint
+Menu
 ;%0Aimport
@@ -313,37 +313,16 @@
 hud.
-interfaces.InterfaceComponent
+MenuItem
 ;%0Aim
@@ -514,13 +514,17 @@
 ava.
-awt.*
+util.List
 ;%0Aim
@@ -1000,209 +1000,126 @@
     
-InterfaceComponent tooltip = Interfaces.getAt(1477, 805, 0);%0A        if (tooltip == null %7C%7C !tooltip.isValid()) %7B%0A            return %22%22;%0A        %7D else %7B%0A            return tooltip.getText();%0A        %7D
+List%3CMenuItem%3E menuItems;%0A%0A        return (menuItems = Menu.getItems()).isEmpty() ? %22%22 : menuItems.get(0).getAction();
 %0A   

Optimize getTooltip().