#!/usr/bin/env python3
"""
REPL Demo - Interactive command-line interface for pipeline control

This demo shows how to use the REPL effect plugin to interact with
the Mainline pipeline in real-time.

Features:
- HUD-style overlay showing FPS, frame time, command history
- Command history navigation (Up/Down arrows)
- Pipeline inspection and control commands
- Parameter adjustment in real-time

Usage:
    python examples/repl_demo.py

Keyboard Controls:
    Enter     - Execute command
    Up/Down   - Navigate command history
    Backspace - Delete character
    Ctrl+C    - Exit
"""

import sys
import time
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from engine.effects.plugins import discover_plugins
from engine.pipeline.hybrid_config import PipelineConfig


def main():
    """Run the REPL demo."""
    print("REPL Demo - Interactive Pipeline Control")
    print("=" * 50)
    print()
    print("This demo will:")
    print("1. Create a pipeline with REPL effect")
    print("2. Enable raw terminal mode for input")
    print("3. Show REPL interface with HUD overlay")
    print()
    print("Keyboard controls:")
    print("  Enter     - Execute command")
    print("  Up/Down   - Navigate command history")
    print("  Backspace - Delete character")
    print("  Ctrl+C    - Exit")
    print()
    print("Commands to try:")
    print("  help - Show available commands")
    print("  status - Show pipeline status")
    print("  effects - List effects")
    print("  pipeline - Show pipeline order")
    print()
    input("Press Enter to start...")

    # Discover plugins
    discover_plugins()

    # Create pipeline with REPL effect
    config = PipelineConfig(
        source="headlines",
        camera={"mode": "scroll", "speed": 1.0},
        effects=[
            {"name": "noise", "intensity": 0.3},
            {"name": "fade", "intensity": 0.5},
            {"name": "repl", "intensity": 1.0},  # Add REPL effect
        ],
        display={"backend": "terminal", "positioning": "mixed"},
    )

    pipeline = config.to_pipeline(viewport_width=80, viewport_height=24)

    # Initialize pipeline
    if not pipeline.initialize():
        print("Failed to initialize pipeline")
        return

    # Get the REPL effect instance
    repl_effect = None
    for stage in pipeline._stages.values():
        if hasattr(stage, "_effect") and stage._effect.name == "repl":
            repl_effect = stage._effect
            break

    if not repl_effect:
        print("REPL effect not found in pipeline")
        return

    # Enable raw mode for input
    display = pipeline.context.get("display")
    if display and hasattr(display, "set_raw_mode"):
        display.set_raw_mode(True)

    # Main loop
    try:
        frame_count = 0
        while True:
            # Get keyboard input
            if display and hasattr(display, "get_input_keys"):
                keys = display.get_input_keys(timeout=0.01)
                for key in keys:
                    if key == "return":
                        repl_effect.process_command(
                            repl_effect.state.current_command, pipeline.context
                        )
                    elif key == "up":
                        repl_effect.navigate_history(-1)
                    elif key == "down":
                        repl_effect.navigate_history(1)
                    elif key == "backspace":
                        repl_effect.backspace()
                    elif key == "ctrl_c":
                        raise KeyboardInterrupt
                    elif len(key) == 1:
                        repl_effect.append_to_command(key)

            # Execute pipeline
            result = pipeline.execute([])

            if not result.success:
                print(f"Pipeline error: {result.error}")
                break

            # Check for pending commands
            pending = repl_effect.get_pending_command()
            if pending:
                print(f"\nPending command: {pending}\n")

            frame_count += 1
            time.sleep(0.033)  # ~30 FPS

    except KeyboardInterrupt:
        print("\n\nExiting REPL demo...")
    finally:
        # Restore terminal mode
        if display and hasattr(display, "set_raw_mode"):
            display.set_raw_mode(False)
        # Cleanup pipeline
        pipeline.cleanup()


if __name__ == "__main__":
    main()
