# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from ros2bag.verb import VerbExtension


class InfoVerb(VerbExtension):
    """Print information about a bag to the screen."""

    def add_arguments(self, parser, cli_name):  # noqa: D102
        parser.add_argument(
            'bag_file', help='bag file to introspect')
        parser.add_argument(
            '-s', '--storage', default='sqlite3',
            help='storage identifier to be used to open storage, if no yaml file exists.'
                 ' Defaults to "sqlite3"')

    def main(self, *, args):  # noqa: D102
        bag_file = args.bag_file
        if not os.path.exists(bag_file):
            return "[ERROR] [ros2bag]: bag file '{}' does not exist!".format(bag_file)
        # NOTE(hidmic): in merged install workspaces on Windows, Python entrypoint lookups
        #               combined with constrained environments (as imposed by colcon test)
        #               may result in DLL loading failures when attempting to import a C
        #               extension. Therefore, do not import rosbag2_transport at the module
        #               level but on demand, right before first use.
        from rosbag2_py._info import Info
        try:
            m = Info().read_metadata(bag_file, args.storage)
            print(m)
        except RuntimeError:
            return ('Could not read metadata for {}.'
                    'Please specify the path to the folder containing '
                    "an existing 'metadata.yaml' file or provide correct storage id "
                    "if metadata file doesn't exist (see help).".format(bag_file))
