# ==============================================================================
# Copyright (c) 2017-2026  Illeon Robotics, Inc. All rights reserved.
#
# Launch robot_state_publisher with X2 URDF.
# ==============================================================================
import os
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration, Command, FindExecutable
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
from ament_index_python.packages import get_package_share_directory

_NSP = os.environ.get('ROBOT_NS', 'robot_unit_0')
_RMP = [('/tf', 'tf'), ('/tf_static', 'tf_static')]


def generate_launch_description():
    pkg_dir = get_package_share_directory('x2_description')

    use_sim_arg = DeclareLaunchArgument('use_sim', default_value='false')
    use_fake_hardware_arg = DeclareLaunchArgument('use_fake_hardware', default_value='false')

    robot_description = ParameterValue(
        Command([
            FindExecutable(name='xacro'), ' ',
            os.path.join(pkg_dir, 'xacro', 'robot.xacro'),
            ' use_sim:=', LaunchConfiguration('use_sim'),
            ' use_fake_hardware:=', LaunchConfiguration('use_fake_hardware'),
        ]),
        value_type=str,
    )

    robot_state_publisher = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        namespace=_NSP,
        remappings=_RMP,
        parameters=[{'robot_description': robot_description}],
    )

    return LaunchDescription([
        use_sim_arg,
        use_fake_hardware_arg,
        robot_state_publisher,
    ])
