"""
Launch audio direction estimator node.

Typical usage:
  ros2 launch saltybot_audio_direction audio_direction.launch.py
"""

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


def generate_launch_description():
    """Generate launch description for audio direction node."""

    # Declare launch arguments
    device_id_arg = DeclareLaunchArgument(
        'device_id',
        default_value='-1',
        description='Audio device index (-1 for default)',
    )
    sample_rate_arg = DeclareLaunchArgument(
        'sample_rate',
        default_value='16000',
        description='Sample rate in Hz',
    )
    chunk_size_arg = DeclareLaunchArgument(
        'chunk_size',
        default_value='2048',
        description='Samples per audio frame',
    )
    publish_hz_arg = DeclareLaunchArgument(
        'publish_hz',
        default_value='10.0',
        description='Publication rate in Hz',
    )
    vad_threshold_arg = DeclareLaunchArgument(
        'vad_threshold',
        default_value='0.02',
        description='RMS energy threshold for voice activity detection',
    )
    gcc_max_lag_arg = DeclareLaunchArgument(
        'gcc_phat_max_lag',
        default_value='64',
        description='Max lag for GCC-PHAT correlation',
    )
    self_noise_filter_arg = DeclareLaunchArgument(
        'self_noise_filter',
        default_value='true',
        description='Apply robot self-noise suppression',
    )

    # Audio direction node
    audio_direction_node = Node(
        package='saltybot_audio_direction',
        executable='audio_direction_node',
        name='audio_direction_estimator',
        output='screen',
        parameters=[
            {'device_id': LaunchConfiguration('device_id')},
            {'sample_rate': LaunchConfiguration('sample_rate')},
            {'chunk_size': LaunchConfiguration('chunk_size')},
            {'publish_hz': LaunchConfiguration('publish_hz')},
            {'vad_threshold': LaunchConfiguration('vad_threshold')},
            {'gcc_phat_max_lag': LaunchConfiguration('gcc_max_lag')},
            {'self_noise_filter': LaunchConfiguration('self_noise_filter')},
        ],
    )

    return LaunchDescription(
        [
            device_id_arg,
            sample_rate_arg,
            chunk_size_arg,
            publish_hz_arg,
            vad_threshold_arg,
            gcc_max_lag_arg,
            self_noise_filter_arg,
            audio_direction_node,
        ]
    )
