"""panoramic.launch.py — Launch equirect stitcher + RTSP server."""

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


def generate_launch_description():
    # Declare launch arguments
    calibration_dir_arg = DeclareLaunchArgument(
        'calibration_dir',
        default_value='/mnt/nvme/saltybot/calibration',
        description='Path to calibration YAML directory',
    )
    output_width_arg = DeclareLaunchArgument(
        'output_width',
        default_value='1920',
        description='Panoramic output width in pixels',
    )
    output_height_arg = DeclareLaunchArgument(
        'output_height',
        default_value='960',
        description='Panoramic output height in pixels',
    )
    publish_rate_arg = DeclareLaunchArgument(
        'publish_rate',
        default_value='15.0',
        description='Stitching + publish rate in Hz',
    )
    use_cuda_arg = DeclareLaunchArgument(
        'use_cuda',
        default_value='true',
        description='Use cv2.cuda for GPU-accelerated remap (Jetson)',
    )
    use_nvenc_arg = DeclareLaunchArgument(
        'use_nvenc',
        default_value='true',
        description='Use nvv4l2h264enc NVENC encoder for RTSP (Jetson)',
    )
    rtsp_port_arg = DeclareLaunchArgument(
        'rtsp_port',
        default_value='8554',
        description='RTSP server port',
    )
    blend_margin_px_arg = DeclareLaunchArgument(
        'blend_margin_px',
        default_value='30',
        description='Alpha blending margin in pixels for overlap zones',
    )
    use_rtsp_arg = DeclareLaunchArgument(
        'use_rtsp',
        default_value='true',
        description='Launch the RTSP server node alongside the stitcher',
    )

    # Equirectangular stitcher node
    stitcher_node = Node(
        package='saltybot_panoramic',
        executable='equirect_stitcher',
        name='equirect_stitcher',
        output='screen',
        parameters=[{
            'calibration_dir':  LaunchConfiguration('calibration_dir'),
            'output_width':     LaunchConfiguration('output_width'),
            'output_height':    LaunchConfiguration('output_height'),
            'publish_rate':     LaunchConfiguration('publish_rate'),
            'blend_margin_px':  LaunchConfiguration('blend_margin_px'),
            'use_cuda':         LaunchConfiguration('use_cuda'),
        }],
    )

    # RTSP server node (conditional on use_rtsp)
    rtsp_node = Node(
        package='saltybot_panoramic',
        executable='rtsp_server',
        name='rtsp_server',
        output='screen',
        condition=IfCondition(LaunchConfiguration('use_rtsp')),
        parameters=[{
            'rtsp_port':    LaunchConfiguration('rtsp_port'),
            'use_nvenc':    LaunchConfiguration('use_nvenc'),
        }],
    )

    return LaunchDescription([
        calibration_dir_arg,
        output_width_arg,
        output_height_arg,
        publish_rate_arg,
        use_cuda_arg,
        use_nvenc_arg,
        rtsp_port_arg,
        blend_margin_px_arg,
        use_rtsp_arg,
        stitcher_node,
        rtsp_node,
    ])
