import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

from src.maze_builder import TextFileMazeBuilder
from src.maze_solver import MazeSolver
from src.pathfinding import BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy
from src.observer import ConsoleView
from src.commands import Player, MoveCommand


def demo():
    print("=" * 70)
    print("ПОИСК ВЫХОДА ИЗ ЛАБИРИНТА")
    print("=" * 70)
    
    # 1. Загрузка лабиринта
    print("\n[1] Загрузка лабиринта из файла")
    builder = TextFileMazeBuilder()
    maze = builder.build_from_file("data/demo_maze.txt")
    print(maze)
    print(f"Старт: ({maze.start.x}, {maze.start.y})")
    print(f"Выход: ({maze.exit.x}, {maze.exit.y})")
    
    # 2. Сравнение алгоритмов (Strategy)
    print("\n[2] Сравнение алгоритмов")
    strategies = [BFSStrategy(), DFSStrategy(), AStarStrategy(), DijkstraStrategy()]
    
    for strategy in strategies:
        solver = MazeSolver(maze, strategy)
        stats = solver.solve("demo")
        print(f"{strategy.get_name():10} | {stats.time_ms:8.4f} мс | "
              f"посещено: {stats.visited_cells:3} | длина: {stats.path_length}")
    
    # 3. Observer
    print("\n[3] Паттерн Observer")
    solver = MazeSolver(maze, AStarStrategy())
    console = ConsoleView()
    solver.add_observer(console)
    solver.solve("demo")
    print(f"События: {console.events}")
    
    # 4. Command
    print("\n[4] Паттерн Command")
    player = Player(maze.start)
    console.render(maze, player.current_cell)
    
    cmd = MoveCommand(player, maze, 'S')
    cmd.execute()
    print(f"После S: {player}")
    
    cmd.undo()
    print(f"После undo: {player}")

if __name__ == "__main__":
    demo()