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

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

hwnd = 0x00E207F6 # PID 30076

def send_key(vk):
    lp = (0x1C << 16) | 1
    user32.PostMessageW(hwnd, 0x0100, vk, lp)
    time.sleep(0.04)
    user32.PostMessageW(hwnd, 0x0101, vk, lp | (3 << 30))
    time.sleep(0.04)

def click(x, y):
    lp = ((y & 0xFFFF) << 16) | (x & 0xFFFF)
    user32.PostMessageW(hwnd, 0x0200, 0, lp)
    time.sleep(0.04)
    user32.PostMessageW(hwnd, 0x0201, 1, lp)
    time.sleep(0.08)
    user32.PostMessageW(hwnd, 0x0202, 0, lp)
    time.sleep(0.05)

user32.SetForegroundWindow(hwnd)
time.sleep(0.2)

# Click 'TRỞ LẠI' button on System Menu (approx X=498, Y=618 or center (500, 618) in client coords) or send ESC
print("Clicking 'TRỞ LẠI' at (500, 618)...")
click(500, 618)
time.sleep(0.2)
send_key(0x1B) # VK_ESCAPE
time.sleep(0.5)

# Click "Kết thúc đối thoại" on the daily notice if still visible (around 300, 570)
click(300, 570)
time.sleep(0.5)

rect = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox=rect, all_screens=True)
path = r"c:\Projects\JX\logs\in_game_verified_final.png"
img.save(path)
print(f"Final clean in-game screenshot saved: {path}")

# Copy to brain artifact directory
artifact_path = r"C:\Users\Admin\.gemini\antigravity\brain\1b4a0d01-a767-4d3f-966f-116a863fc8de\in_game_verified_final.png"
shutil.copyfile(path, artifact_path)
print(f"Copied to artifact path: {artifact_path}")
