import ctypes, time
from PIL import ImageGrab
import win32gui

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

hwnd = 0xe207f6

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 dblclick(x, y):
    lp = make_lparam(x, y)
    user32.PostMessageW(hwnd, 0x0200, 0, lp)
    time.sleep(0.03)
    user32.PostMessageW(hwnd, 0x0201, 1, lp)
    time.sleep(0.05)
    user32.PostMessageW(hwnd, 0x0202, 0, lp)
    time.sleep(0.06)
    user32.PostMessageW(hwnd, 0x0203, 1, lp)
    time.sleep(0.05)
    user32.PostMessageW(hwnd, 0x0202, 0, lp)
    time.sleep(0.05)

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

# Select Trọn Vẹn Chữ Tình
print("Double clicking 'Trọn Vẹn Chữ Tình' at (610, 209)...")
click(610, 209)
time.sleep(0.2)
dblclick(610, 209)
time.sleep(0.5)

# Click "Xác định" bottom-left ONLY (NO Enter key!)
print("Clicking 'Xác định' at (260, 503)...")
click(260, 503)
time.sleep(2.0)

rect = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox=rect, all_screens=True)
img.save(r"c:\Projects\JX\logs\p30076_clean_login_screen.png")
print("Saved p30076_clean_login_screen.png")
