#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Launch local VLM adapter backed by hobot_llamacpp.
"""

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
from ament_index_python.packages import get_package_share_directory


def generate_launch_description():
    use_tts = LaunchConfiguration("use_tts")
    use_hobot_llamacpp = LaunchConfiguration("use_hobot_llamacpp")
    config_file = LaunchConfiguration("config_file")

    image_topic = LaunchConfiguration("image_topic")
    trigger_topic = LaunchConfiguration("trigger_topic")
    trigger_sign = LaunchConfiguration("trigger_sign")
    prompt_text = LaunchConfiguration("prompt_text")
    result_topic = LaunchConfiguration("result_topic")

    llamacpp_prompt_topic = LaunchConfiguration("llamacpp_prompt_topic")
    llamacpp_image_topic = LaunchConfiguration("llamacpp_image_topic")
    llamacpp_result_topic = LaunchConfiguration("llamacpp_result_topic")
    llamacpp_text_topic = LaunchConfiguration("llamacpp_text_topic")
    llamacpp_vit_model_file_name = LaunchConfiguration("llamacpp_vit_model_file_name")
    llamacpp_gguf_model_file_name = LaunchConfiguration("llamacpp_gguf_model_file_name")
    llamacpp_model_type = LaunchConfiguration("llamacpp_model_type")
    llamacpp_threads = LaunchConfiguration("llamacpp_threads")

    audio_sink = LaunchConfiguration("audio_sink")
    tts_speed = LaunchConfiguration("tts_speed")

    local_adapter = Node(
        package="vlm_detect",
        executable="local_vlm_adapter",
        name="local_vlm_adapter",
        output="screen",
        parameters=[
            config_file,
            {
                "image_topic": image_topic,
                "trigger_topic": trigger_topic,
                "trigger_sign": trigger_sign,
                "prompt_text": prompt_text,
                "result_topic": result_topic,
                "llamacpp_prompt_topic": llamacpp_prompt_topic,
                "llamacpp_image_topic": llamacpp_image_topic,
                "llamacpp_result_topic": llamacpp_result_topic,
            },
        ],
    )

    hobot_llamacpp = Node(
        package="hobot_llamacpp",
        executable="hobot_llamacpp",
        name="hobot_llamacpp",
        output="screen",
        condition=IfCondition(use_hobot_llamacpp),
        parameters=[
            {
                "feed_type": 1,
                "is_shared_mem_sub": 0,
                "pre_infer": 0,
                "llm_threads": llamacpp_threads,
                "model_type": llamacpp_model_type,
                "user_prompt": "",
                "system_prompt": "You are a helpful assistant.",
                "ros_img_sub_topic_name": llamacpp_image_topic,
                "ros_string_sub_topic_name": llamacpp_prompt_topic,
                "ai_msg_pub_topic_name": llamacpp_result_topic,
                "text_msg_pub_topic_name": llamacpp_text_topic,
                "model_file_name": llamacpp_vit_model_file_name,
                "llm_model_name": llamacpp_gguf_model_file_name,
            }
        ],
        arguments=["--ros-args", "--log-level", "warn"],
    )

    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,
            },
        ],
    )

    return LaunchDescription(
        [
            DeclareLaunchArgument("use_tts", default_value="true"),
            DeclareLaunchArgument("use_hobot_llamacpp", default_value="true"),
            DeclareLaunchArgument(
                "config_file",
                default_value=PathJoinSubstitution(
                    [
                        get_package_share_directory("vlm_detect"),
                        "config",
                        "vlm_detect.yaml",
                    ]
                ),
            ),
            DeclareLaunchArgument("image_topic", default_value="/image_mjpeg"),
            DeclareLaunchArgument("trigger_topic", default_value="/sign4return"),
            DeclareLaunchArgument("trigger_sign", default_value="9"),
            DeclareLaunchArgument(
                "prompt_text",
                default_value="描述图片中有一个病人的特征，字数控制在20字以内。",
            ),
            DeclareLaunchArgument("result_topic", default_value="/vlm_result"),
            DeclareLaunchArgument("llamacpp_prompt_topic", default_value="/prompt_text"),
            DeclareLaunchArgument("llamacpp_image_topic", default_value="/llamacpp/image"),
            DeclareLaunchArgument("llamacpp_result_topic", default_value="/llama_cpp_node"),
            DeclareLaunchArgument("llamacpp_text_topic", default_value="/tts_text"),
            DeclareLaunchArgument(
                "llamacpp_vit_model_file_name",
                default_value="/home/sunrise/hobot_llamacpp/src/models/vit_model_int16_v2.bin",
            ),
            DeclareLaunchArgument(
                "llamacpp_gguf_model_file_name",
                default_value="/home/sunrise/hobot_llamacpp/src/models/Qwen2.5-0.5B-Instruct-Q4_0.gguf",
            ),
            DeclareLaunchArgument("llamacpp_model_type", default_value="0"),
            DeclareLaunchArgument("llamacpp_threads", default_value="6"),
            DeclareLaunchArgument(
                "audio_sink",
                default_value="alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo",
            ),
            DeclareLaunchArgument("tts_speed", default_value="1.5"),
            LogInfo(msg=["Local VLM backend: hobot_llamacpp"]),
            local_adapter,
            hobot_llamacpp,
            tts_server,
        ]
    )
