from builders import (TextFileMazeBuilder)
from strategies import (BFSStrategy, DFSStrategy, AStarStrategy)
from solver import (MazeSolver)
from observer_command import ConsoleView, Player, MoveCommand
import os

builder = TextFileMazeBuilder()
maze = builder.buildFromFile("no_exit_maze.txt")
print("Лабиринт:\n")
maze.printMaze()
print("Выберете алгоритм")
print("1 - BFS")
print("2 - DFS")
print("3 - A*")
choice = input()
if choice == "1":
    strategy = BFSStrategy()
elif choice == "2":
    strategy = DFSStrategy()
elif choice == "3":
    strategy = AStarStrategy()
else:
    print("Неверный выбор")
    exit()
    
solver = MazeSolver(maze, strategy)
view = ConsoleView()
solver.addObserver(view)
stats = solver.solve()
print("Результат:")
print(stats)
path, _ = strategy.findPath(
    maze,
    maze.start,
    maze.exit
)

if not path:
    print("\nПуть не найден")
    exit()

print("\nНайденный путь:")
for cell in path:
    print(f"({cell.x}, {cell.y})")

print("\nПошаговое движение игрока")
player = Player(maze.start)

history = []
passed_path = [maze.start]

view.render(maze, player, passed_path)

for cell in path[1:]:

    input("\nEnter -> следующий шаг")

    command = MoveCommand(player, cell)
    command.execute()

    history.append(command)

    passed_path.append(cell)

    os.system('cls' if os.name == 'nt' else 'clear')

    view.render(maze, player, passed_path)
    