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 = 0x0063003A
print(f"Target HWND: 0x{hwnd:X}, Valid: {win32gui.IsWindow(hwnd)}")

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.05)
    user32.PostMessageW(hwnd, 0x0203, 1, lp)
    time.sleep(0.05)
    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)

def send_char(c):
    lp = (ord(c) << 16) | 1
    user32.PostMessageW(hwnd, 0x0102, ord(c), lp)
    time.sleep(0.05)

def capture(name):
    if win32gui.IsWindow(hwnd):
        rect = win32gui.GetWindowRect(hwnd)
        img = ImageGrab.grab(bbox=rect, all_screens=True)
        path = rf"c:\Projects\JX\logs\{name}"
        img.save(path)
        print(f"Captured: {path}")

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

# Step 1: Confirm server selection
print("Step 1: Confirming server 'Trọn Vẹn Chữ Tình'...")
click(610, 209)
time.sleep(0.2)
dblclick(610, 209)
time.sleep(0.3)
click(260, 503)
time.sleep(0.1)
send_key(0x0D)
time.sleep(2.0)

# Step 2: Fill Username at calibrated (400, 254)
print("Step 2: Clicking Username box at (400, 254)...")
click(400, 254)
time.sleep(0.1)
send_key(0x23) # VK_END
for _ in range(25): send_key(0x08)
send_key(0x24) # VK_HOME
for _ in range(25): send_key(0x2E)
time.sleep(0.1)
print("Typing username 'nguvansi1'...")
for c in "nguvansi1":
    send_char(c)
time.sleep(0.2)

# Step 3: Fill Password at calibrated (400, 299)
print("Step 3: Clicking Password box at (400, 299)...")
click(400, 299)
time.sleep(0.1)
send_key(0x23) # VK_END
for _ in range(25): send_key(0x08)
send_key(0x24) # VK_HOME
for _ in range(25): send_key(0x2E)
time.sleep(0.1)
print("Typing password 'Ngu123@@'...")
for c in "Ngu123@@":
    send_char(c)
time.sleep(0.2)

capture("nguvansi1_calibrated_typed.png")

# Step 4: Submit Login
print("Step 4: Submitting login credentials...")
click(320, 362)
time.sleep(0.1)
send_key(0x0D)
time.sleep(4.0)

capture("nguvansi1_calibrated_char_select.png")

# Step 5: Select Slot 1 (ConCuaBoMe)
print("Step 5: Selecting Slot 1 and entering game...")
click(250, 350)
time.sleep(0.2)
dblclick(250, 350)
time.sleep(0.3)
click(92, 561) # Vào trò chơi
time.sleep(0.1)
send_key(0x0D)
time.sleep(5.0)

# Step 6: Dismiss in-game dialog
click(300, 570)
time.sleep(0.3)
send_key(0x1B) # ESC
time.sleep(0.5)

capture("in_game_nguvansi1_empirical.png")
shutil.copyfile(r"c:\Projects\JX\logs\in_game_nguvansi1_empirical.png", r"C:\Users\Admin\.gemini\antigravity\brain\1b4a0d01-a767-4d3f-966f-116a863fc8de\in_game_nguvansi1_empirical.png")
print("ALL DONE FOR NGUVANSI1!")
