from PIL import Image

from GramAddict.core.perception.intent_resolver import IntentResolver
from GramAddict.core.perception.spatial_parser import SpatialParser


def inspect_nodes(base_name):
    print(f"\n--- INSPECT FOR {base_name} ---")
    xml_path = f"tests/fixtures/{base_name}.xml"
    jpg_path = f"tests/fixtures/{base_name}.jpg"

    with open(xml_path, "r", encoding="utf-8") as f:
        xml = f.read()

    parser = SpatialParser()
    root = parser.parse(xml)
    candidates = parser.get_clickable_nodes(root)

    # We want to use the EXACT intent resolver logic
    resolver = IntentResolver()

    # Let's mock the device using DeviceFacade
    from GramAddict.core.device_facade import DeviceFacade

    class MockU2Device:
        def __init__(self, img_path):
            self.img = Image.open(img_path)
            self.info = {"sdkInt": 30, "displaySizeDpX": 400, "displayWidth": 1080, "screenOn": True}

        def screenshot(self):
            return self.img

    device = object.__new__(DeviceFacade)
    device.device_id = "test_device"
    device.app_id = "com.instagram.android"
    device.args = None
    device.deviceV2 = MockU2Device(jpg_path)
    b64, box_map = resolver._annotate_screenshot_with_candidates(device, candidates)

    for idx in sorted(box_map.keys()):
        node = box_map[idx]
        label_parts = []
        if node.content_desc:
            label_parts.append(f"desc='{node.content_desc[:50]}'")
        if node.text and node.text != node.content_desc:
            label_parts.append(f"text='{node.text[:50]}'")
        if node.resource_id:
            label_parts.append(f"id='{node.resource_id.split('/')[-1]}'")
        if not label_parts:
            label_parts.append("(no visible text)")
        print(f"  [{idx}] {', '.join(label_parts)}")


inspect_nodes("comment_sheet")
