#!/usr/bin/python3
"""GzWeb 迷你仓库场景：仅 Gazebo Sim + 机器人，无 RViz / Nav2 / SLAM。"""
from os.path import join

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import (
    AppendEnvironmentVariable,
    DeclareLaunchArgument,
    IncludeLaunchDescription,
    TimerAction,
)
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration, PythonExpression


def generate_launch_description():
    bcr_bot_path = get_package_share_directory('bcr_bot')
    world_file = LaunchConfiguration(
        'world_file',
        default=join(bcr_bot_path, 'worlds', 'small_warehouse.sdf'),
    )
    gz_sim_share = get_package_share_directory('ros_gz_sim')

    gz_sim = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            join(gz_sim_share, 'launch', 'gz_sim.launch.py')
        ),
        launch_arguments={
            'gz_args': PythonExpression(["'-r -s ", world_file, "'"]),
        }.items(),
    )

    spawn = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            join(bcr_bot_path, 'launch', 'warehouse_spawn.launch.py')
        ),
    )

    return LaunchDescription([
        AppendEnvironmentVariable(
            name='GZ_SIM_RESOURCE_PATH', value=join(bcr_bot_path, 'worlds')),
        AppendEnvironmentVariable(
            name='GZ_SIM_RESOURCE_PATH', value=join(bcr_bot_path, 'models')),
        AppendEnvironmentVariable(
            name='IGN_GAZEBO_RESOURCE_PATH', value=join(bcr_bot_path, 'worlds')),
        AppendEnvironmentVariable(
            name='IGN_GAZEBO_RESOURCE_PATH', value=join(bcr_bot_path, 'models')),
        DeclareLaunchArgument('world_file', default_value=world_file),
        gz_sim,
        TimerAction(period=8.0, actions=[spawn]),
    ])
