"""Scan a range of physical coordinates to find the Add button via info-at."""
import requests, json

HOST = "http://127.0.0.1:8765"

# From JAB cache:
# Window physical: x=713, y=0, w=1249, h=1037
# Dialog internal frame: x=852, y=214, w=887, h=576
# Dialog layered pane (content): x=854, y=235

# Try scanning x range and y range for push buttons
print("Scanning for accessible elements near the toolbar...")
results = []
for x in range(852, 1000, 15):
    for y in range(214, 320, 10):
        try:
            r = requests.post(f"{HOST}/info-at", json={"x": x, "y": y}, timeout=5)
            d = r.json()
            found = d.get("found", {})
            role = found.get("role", "")
            name = found.get("name", "")
            if role not in ("unknown", ""):
                results.append((x, y, role, name, found.get("bounds"), found.get("enabled")))
                print(f"  ({x},{y}): role={role!r} name={name!r} bounds={found.get('bounds')} enabled={found.get('enabled')}")
        except Exception as e:
            print(f"  ({x},{y}): ERROR {e}")

print(f"\nFound {len(results)} non-unknown elements")
push_buttons = [(x,y,r,n,b,e) for x,y,r,n,b,e in results if 'button' in r.lower()]
print(f"Push buttons: {len(push_buttons)}")
for item in push_buttons:
    print(f"  {item}")
