# 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
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.



_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)

# 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

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.



_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)