import ctypes
import os
import win32con
import win32gui
import win32process
from PIL import ImageGrab, Image

user32 = ctypes.windll.user32

# Attach to interactive desktop
hdesk = user32.OpenDesktopW("Default", 0, False, 0x10000000)
if not hdesk:
    hdesk = user32.OpenInputDesktop(0, False, 0x10000000)
if hdesk:
    user32.SetThreadDesktop(hdesk)
    print("Attached to Desktop:", hdesk)
else:
    print("Could not attach to desktop, last error:", ctypes.get_last_error())

# Check windows now
hwnds = []
def enum_cb(hwnd, extra):
    if win32gui.IsWindowVisible(hwnd):
        txt = win32gui.GetWindowText(hwnd)
        cls = win32gui.GetClassName(hwnd)
        _, pid = win32process.GetWindowThreadProcessId(hwnd)
        if pid == 49116 or "exile" in txt.lower() or "poe" in txt.lower():
            print(f"Match: HWND={hwnd:#x}, PID={pid}, Class='{cls}', Title='{txt}'")
            hwnds.append(hwnd)

win32gui.EnumWindows(enum_cb, None)
print("Found windows:", len(hwnds))

# Capture fullscreen screenshot
os.makedirs("captures", exist_ok=True)
try:
    img = ImageGrab.grab(all_screens=True)
    img_path = "captures/live_monitor_zoom.png"
    img.save(img_path)
    print(f"Fullscreen screenshot captured: {img.size} saved to {img_path}")
except Exception as e:
    print("ImageGrab error:", e)
