#!/usr/bin/python3
# -- coding: utf-8 --**
# Triple-camera (cam1 + cam2 + cam3) launch. Same multi-camera VIOManager path
# as mapping_mid360s_dualcam.launch.py, extended to a 3rd camera.
# See docs/OMNI_LIVO_DUAL_CAMERA_PORTING_PLAN.md.

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

def generate_launch_description():

    pkg_dir = get_package_share_directory("fast_livo")
    config_file_dir = os.path.join(pkg_dir, "config")
    rviz_config_file = os.path.join(pkg_dir, "rviz_cfg", "fast_livo2.rviz")
    urdf_file = os.path.join(pkg_dir, "urdf", "fori_robot.urdf")

    mid360s_config_cmd = os.path.join(config_file_dir, "mid360s.yaml")
    camera_config_cmd = os.path.join(config_file_dir, "camera_triplecam_cam1_cam2_cam3.yaml")
    camera_array_config_cmd = os.path.join(config_file_dir, "extrin_triplecam_cam1_cam2_cam3.yaml")

    use_rviz_arg = DeclareLaunchArgument(
        "use_rviz", default_value="False", description="Whether to launch Rviz2"
    )
    mid360s_config_arg = DeclareLaunchArgument(
        "mid360s_params_file", default_value=mid360s_config_cmd,
        description="Full path to mid360s.yaml (scalar/common parameters)"
    )
    camera_config_arg = DeclareLaunchArgument(
        "camera_params_file", default_value=camera_config_cmd,
        description="Full path to the cam_0/cam_1/cam_2 intrinsics yaml for parameter_blackboard"
    )
    camera_array_config_arg = DeclareLaunchArgument(
        "camera_array_config_file", default_value=camera_array_config_cmd,
        description="Full path to the yaml-cpp-parsed multi-camera extrinsics/topics array"
    )
    use_respawn_arg = DeclareLaunchArgument(
        "use_respawn", default_value="True", description="Whether to respawn if a node crashes."
    )

    mid360s_params_file = LaunchConfiguration("mid360s_params_file")
    camera_params_file = LaunchConfiguration("camera_params_file")
    camera_array_config_file = LaunchConfiguration("camera_array_config_file")
    use_respawn = LaunchConfiguration("use_respawn")

    return LaunchDescription([
        use_rviz_arg,
        mid360s_config_arg,
        camera_config_arg,
        camera_array_config_arg,
        use_respawn_arg,

        Node(
            package="robot_state_publisher",
            executable="robot_state_publisher",
            name="robot_state_publisher",
            parameters=[{"robot_description": open(urdf_file).read()}],
            output="screen"
        ),

        # Camera intrinsics for all three cameras (cam_0/cam_1/cam_2 namespaces),
        # read remotely by vk::camera_loader::loadFromRosNs.
        Node(
            package="demo_nodes_cpp",
            executable="parameter_blackboard",
            name="parameter_blackboard",
            parameters=[camera_params_file],
            output="screen"
        ),

        Node(
            package="fast_livo",
            executable="fastlivo_mapping",
            name="laserMapping",
            parameters=[
                mid360s_params_file,
                {
                    "common": {
                        "camera_config_path": camera_array_config_file,
                        # See mapping_mid360s_dualcam.launch.py — same shared
                        # LiDAR-packet-timestamp quantization applies with 3 cameras.
                        "camera_time_tolerance": 0.12,
                    }
                },
                {"vio": {"enable_cross_camera_tracking": True}},
            ],
            additional_env={
                "LD_PRELOAD": "/usr/lib/x86_64-linux-gnu/libusb-1.0.so.0"
            },
            output="screen"
        ),

        Node(
            condition=IfCondition(LaunchConfiguration("use_rviz")),
            package="rviz2",
            executable="rviz2",
            name="rviz2",
            arguments=["-d", rviz_config_file],
            output="screen"
        ),
    ])
