import ctypes, sys
from ctypes import wintypes
import win32gui, win32con
import psutil

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

pids = [p.pid for p in psutil.process_iter(['pid', 'name']) if 'AutoVLBS' in (p.info['name'] or '')]
print('AutoVLBS PIDs:', pids)
if not pids: sys.exit(0)

target_pid = pids[0]
main_hwnd = 0
def cb(h, _):
    global main_hwnd
    pid = wintypes.DWORD()
    user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
    if pid.value == target_pid and user32.IsWindowVisible(h):
        txt_len = user32.GetWindowTextLengthW(h) + 1
        buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, buf, txt_len)
        if 'jx' in buf.value.lower() or 'vulan' in buf.value.lower():
            main_hwnd = h
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)
print('AutoVLBS HWND:', hex(main_hwnd))

btn_h = 0
def child_cb(ch, _):
    global btn_h
    txt_len = user32.GetWindowTextLengthW(ch) + 1
    buf = ctypes.create_unicode_buffer(txt_len)
    user32.GetWindowTextW(ch, buf, txt_len)
    if 'hỗ trợ' in buf.value.lower() or 'ho tro' in buf.value.lower():
        btn_h = ch
        print(f'Found Chiêu hỗ trợ button: {hex(ch)}, text="{buf.value}"')
    return True

user32.EnumChildWindows(main_hwnd, WNDENUMPROC(child_cb), 0)
if btn_h:
    win32gui.SendMessage(btn_h, win32con.BM_CLICK, 0, 0)
    print('Clicked Chiêu hỗ trợ button')
