from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode

def generate_launch_description():
    # 1. 声明一个外部参数 agv_ip，默认值是本地
    agv_ip_arg = DeclareLaunchArgument(
        'agv_ip',
        default_value='127.0.0.1:50051',
        description='Windows 车端在 Wi-Fi 下的 IP 和端口'
    )
    
    agv_ip = LaunchConfiguration('agv_ip')

    # 2. 把参数动态注入给三个网关组件
    container = ComposableNodeContainer(
        name='agv_gateway_container',
        namespace='',
        package='rclcpp_components',
        executable='component_container_mt', 
        composable_node_descriptions=[
            ComposableNode(
                package='win_ubuntu_bridge', 
                plugin='win_ubuntu_bridge::ChassisGatewayNode', 
                name='chassis_gateway',
                parameters=[{'agv_ip': agv_ip}]  # 👈 动态注入真实 IP
            ),
            
            ComposableNode(
                package='win_ubuntu_bridge', 
                plugin='win_ubuntu_bridge::ControlGatewayNode', 
                name='control_gateway',
                parameters=[{'agv_ip': agv_ip}]  # 👈 动态注入真实 IP
            ),
            
            ComposableNode(
                package='win_ubuntu_bridge', 
                plugin='win_ubuntu_bridge::SensorGatewayNode', 
                name='sensor_gateway',
                parameters=[{'agv_ip': agv_ip}]  # 👈 动态注入真实 IP
            ),
        ],
        output='screen',
    )
    return LaunchDescription([agv_ip_arg, container])