"""
qr_usb_camera.launch.py — 启动 USB 相机 + 二维码识别
"""

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


def generate_launch_description():
    image_topic    = LaunchConfiguration("image_topic",    default="/image_raw")
    camera_device  = LaunchConfiguration("camera_device",  default="/dev/video0")
    result_legal   = LaunchConfiguration("result_legal",   default="true")
    sign_on_detect = LaunchConfiguration("sign_on_detect", default="true")
    use_buffer     = LaunchConfiguration("use_buffer",     default="true")

    # USB 相机驱动 (v4l2)
    # camera = Node(
    #     package="hobot_usb_cam",
    #     executable="hobot_usb_cam",
    #     name="hobot_usb_cam",
    #     parameters=[{
    #         "video_device": camera_device,
    #         "image_size": [640, 480],
    #         "pixel_format": "YUYV",
    #     }],
    #     remappings=[("image_raw", image_topic)],
    # )

    # QR 识别
    qr_detector = Node(
        package="qr_detection",
        executable="qr_usb_camera",
        name="qr_usb_camera",
        output="screen",
        parameters=[{
            "image_topic":     image_topic,
            "result_legal":    result_legal,
            "sign_on_detect":  sign_on_detect,
            "use_buffer":      use_buffer,
            "buffer_timeout":  3.0,
            "legal_strings":   ["1", "2", "顺时针", "逆时针", "顺", "逆"],
        }],
    )

    return LaunchDescription([
        DeclareLaunchArgument("image_topic",
            default_value="/image_raw",
            description="相机图像话题"),
        DeclareLaunchArgument("camera_device",
            default_value="/dev/video0",
            description="USB 相机设备"),
        DeclareLaunchArgument("result_legal",
            default_value="true",
            description="只发布合法结果"),
        DeclareLaunchArgument("sign_on_detect",
            default_value="true",
            description="检测到后发 sign4return=5"),
        DeclareLaunchArgument("use_buffer",
            default_value="true",
            description="信号消失后保持最近结果"),
        # v4l2_camera,
        qr_detector,
    ])
