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

def main():
    user32 = ctypes.windll.user32
    hwinsta = user32.OpenWindowStationW("WinSta0", False, 0x10000000)
    if hwinsta:
        user32.SetProcessWindowStation(hwinsta)
    hdesk = user32.OpenDesktopW("Default", 0, False, 0x10000000)
    if hdesk:
        user32.SetThreadDesktop(hdesk)

    hwnd = user32.FindWindowW("POEWindowClass", None)
    if not hwnd:
        hwnd = user32.FindWindowW(None, "Path of Exile 2")
    if hwnd:
        user32.ShowWindow(hwnd, 9)
        user32.SetForegroundWindow(hwnd)
        time.sleep(0.3)

    # Click on slot E: (2028, 1365)
    print("Moving cursor to slot E (2028, 1365)...")
    user32.SetCursorPos(2028, 1365)
    time.sleep(0.15)
    print("Left clicking slot E...")
    user32.mouse_event(0x0002, 0, 0, 0, 0)  # LEFTDOWN
    time.sleep(0.08)
    user32.mouse_event(0x0004, 0, 0, 0, 0)  # LEFTUP
    time.sleep(0.6)

    c = ScreenCapturerGDI()
    out_dir = r"C:\Projects\AutoPOE2\captures"
    os.makedirs(out_dir, exist_ok=True)
    out_path = os.path.join(out_dir, "test_click_e.png")
    c.capture(out_path)
    print("Saved", out_path)

if __name__ == "__main__":
    main()
