import os

from launch.substitutions import LaunchConfiguration
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
from launch.conditions import IfCondition
from ament_index_python.packages import get_package_share_directory

def generate_launch_description():
    
    use_l2_rviz = LaunchConfiguration('use_rviz')

    declare_rviz_arg = DeclareLaunchArgument(
        'use_l2_rviz',
        default_value='false',
        description='Whether to launch RViz for Unitree L2 LiDAR visualization'
    )

    # The Unitree L2 node and its RViz config are provided by the (vanilla)
    # upstream unitree_lidar_ros2 package.
    pkg_share = get_package_share_directory('unitree_lidar_ros2')
    rviz_config_file = os.path.join(pkg_share, 
                                    'rviz',
                                    'view.rviz')

    # Run unitree lidar
    node1 = Node(
        package='unitree_lidar_ros2',
        executable='unitree_lidar_ros2_node',
        name='unitree_lidar_ros2_node',
        output='screen',
        parameters= [
                {'initialize_type': 1},
                {'work_mode': 8},
                {'use_system_timestamp': True},
                {'range_min': 0.0},
                {'range_max': 100.0},
                {'cloud_scan_num': 18},

                {'serial_port': '/dev/ttyACM1'},
                {'baudrate': 4000000},

                {'lidar_port': 6101},
                {'lidar_ip': '192.168.1.62'},
                {'local_port': 6201},
                {'local_ip': '192.168.1.2'},
                
                {'cloud_frame': "unilidar_lidar"},
                {'cloud_topic': "unilidar/cloud"},
                {'imu_frame': "unilidar_imu"},
                {'imu_topic': "unilidar/imu"},
                ]
    )

    # Run Rviz
    rviz_node = Node(
       package='rviz2',
       executable='rviz2',
       name='rviz2',
       arguments=['-d', rviz_config_file],
       condition=IfCondition(use_l2_rviz),
       output='log'
    )
    return LaunchDescription(
        [
            declare_rviz_arg,
            node1, 
            rviz_node,
        ]
    )
