import time
import ctypes
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from src.assistant_tool.screen_capturer import ScreenCapturer

capturer = ScreenCapturer(output_dir="captures")
hwnd = capturer.find_poe2_window()
if not hwnd:
    print("Could not find POE2 window")
    sys.exit(1)

capturer.bring_to_foreground(hwnd)
time.sleep(0.2)

user32 = ctypes.windll.user32
VK_W = 0x57
KEYEVENTF_KEYUP = 0x0002

# Step 1: Walk North towards portal (Hold W for 0.4s)
print("Walking North towards portal...")
user32.keybd_event(VK_W, 0x11, 0, 0)
time.sleep(0.4)
user32.keybd_event(VK_W, 0x11, KEYEVENTF_KEYUP, 0)
time.sleep(0.1)

# Step 2: Set cursor directly on portal vortex (1280, 400) and click
print("Hovering and clicking portal...")
user32.SetCursorPos(1280, 400)
time.sleep(0.05)
user32.mouse_event(0x0001, 1, 0, 0, 0)
time.sleep(0.05)
user32.mouse_event(0x0001, -1, 0, 0, 0)
time.sleep(0.15)
user32.mouse_event(0x0002, 0, 0, 0, 0)
time.sleep(0.06)
user32.mouse_event(0x0004, 0, 0, 0, 0)
time.sleep(3.0)

img_path = capturer.capture_screenshot(event_name="WALK_AND_CLICK_PORTAL", capture_ram=False)
print(f"Captured: {img_path}")
