#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.actions import Node


def generate_launch_description():
    use_vlm = LaunchConfiguration('use_vlm')
    use_tts = LaunchConfiguration('use_tts')
    use_qr_tts = LaunchConfiguration('use_qr_tts')
    config_file = LaunchConfiguration('config_file')

    vlm_host = LaunchConfiguration('vlm_host')
    vlm_model = LaunchConfiguration('vlm_model')
    image_topic = LaunchConfiguration('image_topic')
    trigger_topic = LaunchConfiguration('trigger_topic')
    trigger_sign = LaunchConfiguration('trigger_sign')
    result_topic = LaunchConfiguration('result_topic')
    prompt_text = LaunchConfiguration('prompt_text')
    max_tokens = LaunchConfiguration('max_tokens')
    image_max_dim = LaunchConfiguration('image_max_dim')
    use_tcp = LaunchConfiguration('use_tcp')
    use_serial = LaunchConfiguration('use_serial')
    tcp_host = LaunchConfiguration('tcp_host')
    tcp_port = LaunchConfiguration('tcp_port')
    serial_port = LaunchConfiguration('serial_port')
    serial_baud = LaunchConfiguration('serial_baud')

    audio_sink = LaunchConfiguration('audio_sink')
    tts_speed = LaunchConfiguration('tts_speed')

    declare_use_vlm = DeclareLaunchArgument('use_vlm', default_value='true')
    declare_use_tts = DeclareLaunchArgument('use_tts', default_value='true')
    declare_use_qr_tts = DeclareLaunchArgument('use_qr_tts', default_value='false')
    declare_config_file = DeclareLaunchArgument('config_file',
        default_value=PathJoinSubstitution([
            get_package_share_directory('vlm_detect'), 'config', 'vlm_detect.yaml']))

    # RDK X5 BPU server (default). For WSL server, use: vlm_host:=http://192.168.175.111:8000
    declare_vlm_host = DeclareLaunchArgument('vlm_host', default_value='http://192.168.175.64:8000')
    declare_vlm_model = DeclareLaunchArgument('vlm_model', default_value='internvl2.5-qwen2.5-0.5b')
    declare_image_topic = DeclareLaunchArgument('image_topic', default_value='/image')
    declare_trigger_topic = DeclareLaunchArgument('trigger_topic', default_value='/sign4return')
    declare_trigger_sign = DeclareLaunchArgument('trigger_sign', default_value='9')
    declare_result_topic = DeclareLaunchArgument('result_topic', default_value='/vlm_result')
    declare_prompt_text = DeclareLaunchArgument('prompt_text', default_value='请描述这个病人的状态。不要描述边框、背景。15字以内。')
    declare_max_tokens = DeclareLaunchArgument('max_tokens', default_value='30')
    declare_image_max_dim = DeclareLaunchArgument('image_max_dim', default_value='448')
    declare_use_tcp = DeclareLaunchArgument('use_tcp', default_value='true')
    declare_use_serial = DeclareLaunchArgument('use_serial', default_value='false')
    declare_tcp_host = DeclareLaunchArgument('tcp_host', default_value='192.168.127.1')
    declare_tcp_port = DeclareLaunchArgument('tcp_port', default_value='9216')
    declare_serial_port = DeclareLaunchArgument('serial_port', default_value='/dev/ttyUSB0')
    declare_serial_baud = DeclareLaunchArgument('serial_baud', default_value='921600')
    declare_audio_sink = DeclareLaunchArgument('audio_sink',
        default_value='alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo')
    declare_tts_speed = DeclareLaunchArgument('tts_speed', default_value='1.5')

    vlm_node = Node(
        package='vlm_detect',
        executable='vlm_node',
        name='vlm_detect',
        output='screen',
        condition=IfCondition(use_vlm),
        parameters=[config_file,
            {
                'vlm_host': vlm_host,
                'vlm_model': vlm_model,
                'image_topic': image_topic,
                'trigger_topic': trigger_topic,
                'trigger_sign': trigger_sign,
                'result_topic': result_topic,
                'prompt_text': prompt_text,
                'max_tokens': max_tokens,
                'image_max_dim': image_max_dim,
                'use_tcp': use_tcp,
                'use_serial': use_serial,
                'tcp_host': tcp_host,
                'tcp_port': tcp_port,
                'serial_port': serial_port,
                'serial_baud': serial_baud,
            }],
    )

    tts_server = Node(
        package='vlm_detect',
        executable='tts_server',
        name='tts_server',
        output='screen',
        condition=IfCondition(use_tts),
        parameters=[config_file,
            {
                'audio_sink': audio_sink,
                'tts_speed': tts_speed,
            }],
    )

    qr_tts_bridge = Node(
        package='vlm_detect',
        executable='qr_tts_bridge',
        name='qr_tts_bridge',
        output='screen',
        condition=IfCondition(use_qr_tts),
    )

    return LaunchDescription([
        declare_use_vlm,
        declare_use_tts,
        declare_use_qr_tts,
        declare_config_file,
        declare_vlm_host,
        declare_vlm_model,
        declare_image_topic,
        declare_trigger_topic,
        declare_trigger_sign,
        declare_result_topic,
        declare_prompt_text,
        declare_max_tokens,
        declare_image_max_dim,
        declare_use_tcp,
        declare_use_serial,
        declare_tcp_host,
        declare_tcp_port,
        declare_serial_port,
        declare_serial_baud,
        declare_audio_sink,
        declare_tts_speed,
        LogInfo(msg=['Config: ', config_file]),
        LogInfo(msg=['VLM Host: ', vlm_host]),
        vlm_node,
        tts_server,
        qr_tts_bridge,
    ])
