"""Use UIAutomation via pywinauto to find and click the Add button in MCSJ PO Maintenance."""
import pywinauto
from pywinauto import Desktop
import time

# Connect to MCSJ using UIA backend
print("Connecting to MCSJ via UIA backend...")
try:
    app = pywinauto.Application(backend='uia').connect(title_re='MCSJ.*', timeout=5)
    print(f"Connected: {app}")

    # Get the main window
    win = app.top_window()
    print(f"Main window: {win.window_text()!r}")

    # Print all children of top window
    print("\nTop-level children:")
    for c in win.children():
        try:
            print(f"  type={c.element_info.control_type!r:20} name={c.window_text()!r:30} enabled={c.is_enabled()}")
        except Exception as e:
            print(f"  ERROR: {e}")

except Exception as e:
    print(f"UIA connect failed: {e}")

print("\n---\nTrying win32 backend...")
try:
    app2 = pywinauto.Application(backend='win32').connect(title_re='MCSJ.*', timeout=5)
    win2 = app2.top_window()
    print(f"Win32 main: {win2.window_text()!r}")

    print("\nWin32 top-level children:")
    for c in win2.children():
        try:
            print(f"  class={c.class_name()!r:20} text={c.window_text()!r:30}")
        except Exception as e:
            print(f"  ERROR: {e}")
except Exception as e:
    print(f"Win32 connect failed: {e}")
