import sys, time, ctypes, shutil
import win32gui
from PIL import ImageGrab

sys.stdout.reconfigure(encoding='utf-8')
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk: user32.SetThreadDesktop(hdesk)

hwnd_tl = 0x0063003A # ConCuaBoMe

# Send ESC to dismiss system menu
sc = user32.MapVirtualKeyW(0x1B, 0)
lp_down = 1 | (sc << 16)
lp_up = 1 | (sc << 16) | (3 << 30)
user32.PostMessageW(hwnd_tl, 0x0100, 0x1B, lp_down)
time.sleep(0.05)
user32.PostMessageW(hwnd_tl, 0x0101, 0x1B, lp_up)
time.sleep(0.5)

# Capture all 4 windows
windows = [
    ("BoCuaCacCon_CaiBang", 0x002F0BF4),
    ("MeCuaCacCon_NgaMy", 0x00E207F6),
    ("ConCuaBoMe_ThieuLam", 0x0063003A),
    ("ConGailCung_NguDoc", 0x004804A8)
]

for name, h in windows:
    if win32gui.IsWindow(h):
        rect = win32gui.GetWindowRect(h)
        img = ImageGrab.grab(bbox=rect, all_screens=True)
        img_path = rf"c:\Projects\JX\logs\clean_{name}.png"
        img.save(img_path)
        art_path = rf"C:\Users\Admin\.gemini\antigravity\brain\1b4a0d01-a767-4d3f-966f-116a863fc8de\clean_{name}.png"
        shutil.copyfile(img_path, art_path)
        print(f"Captured clean {name}: {img_path}")

print("All 4 characters verified and captured cleanly!")
