#!/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')

    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']))

    declare_vlm_host = DeclareLaunchArgument('vlm_host', default_value='http://192.168.175.111:8000')
    declare_vlm_model = DeclareLaunchArgument('vlm_model', default_value='/home/wisdom/models/gguf/Qwen2-VL-2B-Instruct-Q4_K_M.gguf')
    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='图中是一个2D动漫插画风格的医院病房，有一个病人。请描述这个病人的状态。不要描述边框、背景、环境。20字以内。')
    declare_max_tokens = DeclareLaunchArgument('max_tokens', default_value='100')
    declare_image_max_dim = DeclareLaunchArgument('image_max_dim', default_value='128')
    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,
            }],
    )

    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_audio_sink,
        declare_tts_speed,
        LogInfo(msg=['Config: ', config_file]),
        LogInfo(msg=['VLM Host: ', vlm_host]),
        vlm_node,
        tts_server,
        qr_tts_bridge,
    ])
