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 make_lparam(x, y):
    return ((y & 0xFFFF) << 16) | (x & 0xFFFF)

def click(x, y):
    lp = make_lparam(x, y)
    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)

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)

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

# Click "Kết thúc đối thoại" at (300, 570) or send ESC
print("Clicking 'Kết thúc đối thoại' at (300, 570)...")
click(300, 570)
time.sleep(0.5)
send_key(0x1B) # VK_ESCAPE
time.sleep(0.8)

rect = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox=rect, all_screens=True)
path = r"c:\Projects\JX\logs\clean_in_game_mecuacaccon.png"
img.save(path)
print(f"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_world_success.png"
shutil.copyfile(path, artifact_path)
print(f"Copied to artifact path: {artifact_path}")
