from pathlib import Path

from ament_index_python.packages import get_package_prefix, get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess, OpaqueFunction
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def _launch_processes(context):
    server_executable = LaunchConfiguration("server_executable").perform(context)
    model_dir = LaunchConfiguration("model_dir").perform(context)
    config_dir = LaunchConfiguration("config_dir").perform(context)
    params_file = LaunchConfiguration("params_file").perform(context)
    host = LaunchConfiguration("host").perform(context)
    port = LaunchConfiguration("port").perform(context)

    sidecar = ExecuteProcess(
        cmd=[server_executable],
        name="mtranserver",
        output="screen",
        additional_env={
            "MT_HOST": host,
            "MT_PORT": port,
            "MT_ENABLE_UI": "false",
            "MT_OFFLINE": "true",
            "MT_CHECK_UPDATE": "false",
            "MT_CONFIG_DIR": config_dir,
            "MT_MODEL_DIR": model_dir,
            "MT_WORKER_IDLE_TIMEOUT": "86400",
            "MT_CACHE_SIZE": "100",
        },
        respawn=True,
        respawn_delay=2.0,
    )

    bridge = Node(
        package="mtran",
        executable="mtran_bridge_node",
        name="mtran_bridge_node",
        output="screen",
        parameters=[
            params_file,
            {"server_url": f"http://{host}:{port}"},
        ],
    )

    return [sidecar, bridge]


def generate_launch_description() -> LaunchDescription:
    package_prefix = Path(get_package_prefix("mtran"))
    package_share = Path(get_package_share_directory("mtran"))

    return LaunchDescription(
        [
            DeclareLaunchArgument(
                "server_executable",
                default_value=str(package_prefix / "lib" / "mtran" / "mtranserver"),
                description="Path to the native MTranServer executable.",
            ),
            DeclareLaunchArgument(
                "model_dir",
                default_value=str(package_share / "models"),
                description="Directory containing the en_zh-Hans model.",
            ),
            DeclareLaunchArgument(
                "config_dir",
                default_value=str(package_share / "mtranserver_config"),
                description="Directory containing the offline model records file.",
            ),
            DeclareLaunchArgument(
                "params_file",
                default_value=str(package_share / "config" / "translator.yaml"),
                description="ROS2 parameter file for mtran_bridge_node.",
            ),
            DeclareLaunchArgument(
                "host",
                default_value="127.0.0.1",
                description="Loopback host used by MTranServer and the bridge.",
            ),
            DeclareLaunchArgument(
                "port",
                default_value="8989",
                description="HTTP port used by MTranServer and the bridge.",
            ),
            OpaqueFunction(function=_launch_processes),
        ]
    )
