import ctypes
import sys
from ctypes import wintypes
import win32gui
import win32con

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

# Find AutoVLBS window
target_hwnd = None
def find_vlbs(h, _):
    global target_hwnd
    if user32.IsWindowVisible(h):
        txt_len = user32.GetWindowTextLengthW(h) + 1
        txt_buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, txt_buf, txt_len)
        if 'vocong' in txt_buf.value.lower() or 'vlbs' in txt_buf.value.lower():
            target_hwnd = h
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(find_vlbs), 0)

if not target_hwnd:
    print("AutoVLBS window not found")
    sys.exit(1)

print(f"Target AutoVLBS HWND: {hex(target_hwnd)}")

children = []
def enum_child(h, _):
    txt_len = user32.GetWindowTextLengthW(h) + 1
    txt_buf = ctypes.create_unicode_buffer(txt_len)
    user32.GetWindowTextW(h, txt_buf, txt_len)
    
    cls_buf = ctypes.create_unicode_buffer(256)
    user32.GetClassNameW(h, cls_buf, 256)
    
    rect = wintypes.RECT()
    user32.GetWindowRect(h, ctypes.byref(rect))
    w = rect.right - rect.left
    h_val = rect.bottom - rect.top
    
    ctrl_id = user32.GetDlgCtrlID(h)
    style = user32.GetWindowLongW(h, -16) # GWL_STYLE
    is_visible = bool(style & 0x10000000)
    
    children.append((h, ctrl_id, cls_buf.value, txt_buf.value, is_visible, w, h_val, rect.left, rect.top))
    return True

user32.EnumChildWindows(target_hwnd, WNDENUMPROC(enum_child), 0)

print(f"Total controls found: {len(children)}")
for h, cid, cls_name, txt, vis, w, h_val, l, t in children:
    vis_str = "VIS" if vis else "HID"
    if txt or cls_name in ['Button', 'SysTabControl32', 'ComboBox', 'Edit']:
        print(f"[{vis_str}] ID: {cid:5d} | Class: {cls_name:20s} | Size: {w:3d}x{h_val:3d} | Pos: ({l},{t}) | Text: '{txt}'")
