"""
demo.py - короткая демонстрация всех паттернов на маленьком лабиринте.
"""

from maze_solver import (
    TextFileMazeBuilder, MazeSolver, ConsoleView,
    BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy,
    Player, MoveCommand, CommandHistory,
)


def main():
    print("=== Builder: загружаем small_10x10.txt ===")
    builder = TextFileMazeBuilder()
    maze = builder.build_from_file("mazes/small_10x10.txt")

    view = ConsoleView(verbose=True)
    view.update({"type": "maze_loaded", "maze": maze})

    print("\nСам лабиринт:")
    print(maze.render_text())

    print("\n=== Strategy: пробуем все 4 алгоритма ===")
    solver = MazeSolver(maze)
    solver.attach(view)

    for cls in (BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy):
        solver.set_strategy(cls())
        stats = solver.solve()
        print(f"--- {stats['strategy']} путь длиной {stats['path_length']} ---")
        print(maze.render_text(path=stats['path']))
        print()

    print("=== Command: пройдёмся вручную и сделаем undo ===")
    player = Player(maze.start)
    history = CommandHistory()
    print(f"стартовая позиция: ({player.x},{player.y})")

    # Несколько шагов вправо
    for d in "DDDD":
        ok = history.do(MoveCommand(maze, player, d))
        print(f"  move {d}: {'ok' if ok else 'blocked'} -> ({player.x},{player.y})")

    print("Откатываем 2 хода (undo, undo):")
    history.undo()
    history.undo()
    print(f"  теперь игрок в ({player.x},{player.y})")

    print("\nЛабиринт с игроком:")
    print(maze.render_text(player=player))


if __name__ == "__main__":
    main()
