import re

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


def main():
    with open("tests/fixtures/user_profile_dump.xml", "r", encoding="utf-8") as f:
        xml = f.read()

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

    intent_description = "tap 'following' list"
    quotes = re.findall(r"['\"](.*?)['\"]", intent_description)
    target_text = quotes[0].lower()
    localized_targets = [target_text]

    semantic_candidates = []
    for node in candidates:
        n_text = _humanize_desc((node.text or "").lower())
        n_desc = _humanize_desc((node.content_desc or "").lower())

        for loc_target in localized_targets:
            pattern = r"\b" + re.escape(loc_target) + r"\b"
            if (
                re.search(pattern, n_text)
                or re.search(pattern, n_desc)
                or loc_target in (node.resource_id or "").lower()
            ):
                semantic_candidates.append(node)
                break

    print(f"Quotes: {quotes}")
    print(f"Num semantic candidates: {len(semantic_candidates)}")
    for i, n in enumerate(semantic_candidates):
        print(f"[{i}] id={n.resource_id} desc={n.content_desc} text={n.text}")


if __name__ == "__main__":
    main()
