# Copyright 2024 Universidad Politécnica de Madrid
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#    * Neither the name of the Universidad Politécnica de Madrid nor the names of its
#      contributors may be used to endorse or promote products derived from
#      this software without specific prior written permission.
#
# 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 HOLDER 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.

"""Launch file for the state estimator node."""

__authors__ = 'Rafael Pérez Seguí, Pedro Arias Pérez'
__copyright__ = 'Copyright (c) 2024 Universidad Politécnica de Madrid'
__license__ = 'BSD-3-Clause'

import os

from ament_index_python.packages import get_package_share_directory
from as2_core.declare_launch_arguments_from_config_file import DeclareLaunchArgumentsFromConfigFile
from as2_core.launch_configuration_from_config_file import LaunchConfigurationFromConfigFile
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription, OpaqueFunction
from launch.conditions import LaunchConfigurationEquals
from launch.substitutions import EnvironmentVariable, LaunchConfiguration
from launch_ros.actions import Node


def get_package_config_file():
    """Return the package config file."""
    package_folder = get_package_share_directory('as2_platform_gazebo')
    return os.path.join(package_folder,
                        'config/platform_config_file.yaml')


def get_node(context, *args, **kwargs) -> list:
    """
    Get node.

    :param context: Launch context
    :type context: LaunchContext
    :return: List with node
    :rtype: list
    """
    # Get namespace
    namespace = LaunchConfiguration('namespace').perform(context)

    cmd_vel_topic = DeclareLaunchArgument(
        'cmd_vel_topic', default_value=f'/gz/{namespace}/cmd_vel')
    arm_topic = DeclareLaunchArgument(
        'arm_topic', default_value=f'/gz/{namespace}/arm')
    acro_topic = DeclareLaunchArgument(
        'acro_topic', default_value=f'/gz/{namespace}/acro')

    return [
        cmd_vel_topic,
        arm_topic,
        acro_topic,
        Node(
            package='as2_platform_gazebo',
            executable='as2_platform_gazebo_node',
            name='platform',
            namespace=LaunchConfiguration('namespace'),
            output='screen',
            arguments=['--ros-args', '--log-level',
                       LaunchConfiguration('log_level')],
            emulate_tty=True,
            parameters=[
                {
                    'use_sim_time': LaunchConfiguration('use_sim_time'),
                    'control_modes_file': LaunchConfiguration('control_modes_file'),
                    'cmd_vel_topic': LaunchConfiguration('cmd_vel_topic'),
                    'arm_topic': LaunchConfiguration('arm_topic'),
                    'acro_topic': LaunchConfiguration('acro_topic'),
                },
                LaunchConfigurationFromConfigFile(
                    'platform_config_file',
                    default_file=get_package_config_file())
            ]
        )
    ]


def generate_launch_description() -> LaunchDescription:
    """
    Entry point for launch file.

    :return: Launch description
    :rtype: LaunchDescription
    """
    package_folder = get_package_share_directory(
        'as2_platform_gazebo')
    control_modes = os.path.join(package_folder,
                                 'config/control_modes.yaml')

    # Get as2_gazebo_assets/launch/drone_bridges.py
    as2_gazebo_assets_folder = get_package_share_directory('as2_gazebo_assets')
    drone_bridges_exe = os.path.join(as2_gazebo_assets_folder,
                                     'launch/drone_bridges.py')

    return LaunchDescription([
        DeclareLaunchArgument('log_level',
                              description='Logging level',
                              default_value='info'),
        DeclareLaunchArgument('use_sim_time',
                              description='Use simulation clock if true',
                              default_value='true'),
        DeclareLaunchArgument('namespace',
                              description='Drone namespace',
                              default_value=EnvironmentVariable(
                                  'AEROSTACK2_SIMULATION_DRONE_ID')),
        DeclareLaunchArgument('control_modes_file',
                              default_value=control_modes,
                              description='Platform control modes file'),
        DeclareLaunchArgument('create_bridges',
                              default_value='true',
                              description='Create platform ROS<->Gazebo bridges'),
        IncludeLaunchDescription(
            drone_bridges_exe,
            launch_arguments={
                'namespace': LaunchConfiguration('namespace'),
                'simulation_config_file': LaunchConfiguration('simulation_config_file')
            }.items(),
            condition=LaunchConfigurationEquals('create_bridges', 'true')),
        DeclareLaunchArgumentsFromConfigFile(
            name='platform_config_file', source_file=get_package_config_file(),
            description='Configuration file'),
        OpaqueFunction(function=get_node)
    ])
