#!/usr/bin/env python3
"""
Cage Wayland + Chromium kiosk launch configuration for MageDok 7" display.

Lightweight alternative to X11 desktop environment:
- Cage: Minimal Wayland compositor (replaces GNOME/Mutter)
- Chromium: Fullscreen kiosk browser for SaltyFace web UI
- PulseAudio: HDMI audio routing

Memory savings vs GNOME:
- GNOME + Mutter: ~650MB RAM
- Cage + Chromium: ~200MB RAM
- Savings: ~450MB RAM for other ROS2 workloads

Issue #374: Replace GNOME with Cage + Chromium kiosk
"""

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument, ExecuteProcess
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    """Generate ROS2 launch description for Cage + Chromium kiosk."""

    # Launch arguments
    url_arg = DeclareLaunchArgument(
        'kiosk_url',
        default_value='http://localhost:3000',
        description='URL for Chromium kiosk (SaltyFace web app)'
    )

    debug_arg = DeclareLaunchArgument(
        'debug',
        default_value='false',
        description='Enable debug logging'
    )

    ld = LaunchDescription([url_arg, debug_arg])

    # Start touch monitor (from Issue #369 - reused)
    # Monitors MageDok USB touch device availability
    touch_monitor = Node(
        package='saltybot_bringup',
        executable='touch_monitor.py',
        name='touch_monitor',
        output='screen',
    )
    ld.add_action(touch_monitor)

    # Start audio router (from Issue #369 - reused)
    # Routes HDMI audio to built-in speakers via PulseAudio
    audio_router = Node(
        package='saltybot_bringup',
        executable='audio_router.py',
        name='audio_router',
        output='screen',
    )
    ld.add_action(audio_router)

    # Start Cage Wayland compositor with Chromium kiosk
    # Replaces X11 server + GNOME desktop environment
    cage_chromium = ExecuteProcess(
        cmd=[
            '/opt/saltybot/scripts/chromium_kiosk.sh',
            '--url', LaunchConfiguration('kiosk_url'),
        ],
        condition_condition=None,  # Always start
        name='cage_chromium',
        shell=True,
    )
    ld.add_action(cage_chromium)

    return ld


if __name__ == '__main__':
    print(generate_launch_description())
