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

# Загружаем лабиринт
builder = TextFileMazeBuilder()
maze = builder.build_from_file("demo_maze.txt")

print("=" * 50)
print("ТЕСТ OBSERVER")
print("=" * 50)

solver = MazeSolver(maze, AStarStrategy())
console = ConsoleView()
solver.add_observer(console)

stats = solver.solve("demo_maze")
console.render_stats(stats)

print(f"\nСобытия: {console.events}")

print("\n" + "=" * 50)
print("ТЕСТ COMMAND")
print("=" * 50)

player = Player(maze.start)
print(f"Начальная позиция: {player}")
console.render(maze, player.current_cell)

cmd1 = MoveCommand(player, maze, 'S')
success = cmd1.execute()
print(f"Движение S: {'успешно' if success else 'не удалось'} → {player}")

cmd2 = MoveCommand(player, maze, 'S')
success = cmd2.execute()
print(f"Движение S: {'успешно' if success else 'не удалось'} → {player}")

console.render(maze, player.current_cell)

print("\nОтмена последнего хода:")
cmd2.undo()
print(f"После undo: {player}")
console.render(maze, player.current_cell)