# Path: rosapi/src/rosapi/glob_helper.py
# def any_match(query, globs):
#     return (
#         globs is None or len(globs) == 0 or any(fnmatch.fnmatch(str(query), glob) for glob in globs)
#     )
# 
# def filter_globs(globs, full_list):
#     # If the globs are empty (weren't defined in the params), return the full list
#     if globs is not None and len(globs) > 0:
#         return list(filter(lambda x: any_match(x, globs), full_list))
#     else:
#         return full_list


from ros2node.api import (
    get_node_names,
    get_publisher_info,
    get_service_server_info,
    get_subscriber_info,
)
from ros2service.api import get_service_names, get_service_names_and_types
from ros2topic.api import get_topic_names, get_topic_names_and_types
from .glob_helper import any_match, filter_globs
_node = None


def init(node):
    """
    Initializes proxy module with a rclpy.node.Node for further use.
    This function has to be called before any other for the module to work.
    """
    global _node
    _node = node


def get_topics(topics_glob, include_hidden=False):
    """Returns a list of all the active topics in the ROS system"""
    topic_names = get_topic_names(node=_node, include_hidden_topics=include_hidden)
    return filter_globs(topics_glob, topic_names)


def get_topics_and_types(topics_glob, include_hidden=False):
    return get_publications_and_types(
        topics_glob, get_topic_names_and_types, include_hidden_topics=include_hidden
    )


def get_topics_for_type(topic_type, topics_glob, include_hidden=False):
    topic_names_and_types = get_topic_names_and_types(
        node=_node, include_hidden_topics=include_hidden
    )
    # topic[0] has the topic name and topic[1] has the type wrapped in a list.
    topics_for_type = [topic[0] for topic in topic_names_and_types if topic[1][0] == topic_type]

# Path: rosapi/src/rosapi/glob_helper.py
# def any_match(query, globs):
#     return (
#         globs is None or len(globs) == 0 or any(fnmatch.fnmatch(str(query), glob) for glob in globs)
#     )
# 
# def filter_globs(globs, full_list):
#     # If the globs are empty (weren't defined in the params), return the full list
#     if globs is not None and len(globs) > 0:
#         return list(filter(lambda x: any_match(x, globs), full_list))
#     else:
#         return full_list


# Path: rosapi/src/rosapi/proxy.py
from ros2node.api import (
    get_node_names,
    get_publisher_info,
    get_service_server_info,
    get_subscriber_info,
)
from ros2service.api import get_service_names, get_service_names_and_types
from ros2topic.api import get_topic_names, get_topic_names_and_types
from .glob_helper import any_match, filter_globs

_node = None


def init(node):
    """
    Initializes proxy module with a rclpy.node.Node for further use.
    This function has to be called before any other for the module to work.
    """
    global _node
    _node = node


def get_topics(topics_glob, include_hidden=False):
    """Returns a list of all the active topics in the ROS system"""
    topic_names = get_topic_names(node=_node, include_hidden_topics=include_hidden)
    return filter_globs(topics_glob, topic_names)


def get_topics_and_types(topics_glob, include_hidden=False):
    return get_publications_and_types(
        topics_glob, get_topic_names_and_types, include_hidden_topics=include_hidden
    )


def get_topics_for_type(topic_type, topics_glob, include_hidden=False):
    topic_names_and_types = get_topic_names_and_types(
        node=_node, include_hidden_topics=include_hidden
    )
    # topic[0] has the topic name and topic[1] has the type wrapped in a list.
    topics_for_type = [topic[0] for topic in topic_names_and_types if topic[1][0] == topic_type]

    return filter_globs(topics_glob, topics_for_type)