import ctypes, time
from ctypes import wintypes
import win32gui
from PIL import ImageGrab

user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

hwnd = win32gui.FindWindow(None, "JXHelper v2.6 [Admin]")
print("JXHelper window:", hex(hwnd))
if hwnd:
    # Find lvwAccounts
    lvw = [0]
    def cb(h, _):
        cls = win32gui.GetClassName(h)
        if "SysListView32" in cls or "ListView" in cls or "WindowsForms10.SysListView32" in cls:
            lvw[0] = h
            return False
        return True
    win32gui.EnumChildWindows(hwnd, cb, None)
    print("ListView HWND:", hex(lvw[0]))
    
    if lvw[0]:
        # Click on row 2 (BoCuaCacCon): Y roughly 24 + 18/2 = ~33px down from top
        # Send WM_LBUTTONDOWN and WM_LBUTTONUP at (50, 42) relative to ListView
        lParam = (42 << 16) | 50
        user32.PostMessageW(lvw[0], 0x0201, 0x0001, lParam) # WM_LBUTTONDOWN
        user32.PostMessageW(lvw[0], 0x0202, 0, lParam)      # WM_LBUTTONUP
        time.sleep(0.5)
        
        rect = win32gui.GetWindowRect(hwnd)
        img = ImageGrab.grab(bbox=rect)
        art_path = r'C:\Users\Admin\.gemini\antigravity\brain\5f637e30-06d3-43e8-a064-a259fea7be1a\jxhelper_selected_bocuacaccon.png'
        img.save(art_path)
        print("Saved screenshot of selected BoCuaCacCon to:", art_path)
