import os
from builders import TextFileMazeBuilder
from strategies import BFSStrategy, DFSStrategy, AStarStrategy
from solver import MazeSolver
from observers import ConsoleView
from commands import Player
from experiments import run_all_experiments, save_results_to_csv, print_results_table


def create_test_mazes():
    os.makedirs("mazes", exist_ok=True)
    
    small = """##########
#S       #
# ### ## #
#   #    #
### # ####
#   #    #
# ### #  #
#     #  #
#     # E#
##########"""
    
    medium = """####################
#S                 #
# # # # # # # # # #
#                 #
# # # # # # # # # #
#                 #
# # # # # # # # # #
#                 #
# # # # # # # # # #
#                 E#
####################"""
    
    large = """##############################
#S                           #
# # # # # # # # # # # # # # #
#                           #
# # # # # # # # # # # # # # #
#                           #
# # # # # # # # # # # # # # #
#                           #
# # # # # # # # # # # # # # #
#                           #
# # # # # # # # # # # # # # #
#                           #
# # # # # # # # # # # # # # #
#                           E#
##############################"""
    
    empty = "S" + " " * 28 + "E"
    
    no_exit = """#######
#S    #
# ### #
#   # #
#######"""
    
    with open("mazes/small.txt", "w") as f:
        f.write(small)
    with open("mazes/medium.txt", "w") as f:
        f.write(medium)
    with open("mazes/large.txt", "w") as f:
        f.write(large)
    with open("mazes/empty.txt", "w") as f:
        f.write(empty)
    with open("mazes/no_exit.txt", "w") as f:
        f.write(no_exit)
    


def demo_maze_solver():
    print("\n" + "=" * 60)
    print("ДЕМОНСТРАЦИЯ РАБОТЫ MAZE SOLVER")
    print("=" * 60)
    
    builder = TextFileMazeBuilder()
    view = ConsoleView()
    
    maze = builder.build_from_file("mazes/small.txt")
    view.update("maze_loaded", {"maze": maze})
    
    strategies = [
        ("BFS", BFSStrategy(), "BFS"),
        ("DFS", DFSStrategy(), "DFSs"),
        ("A*", AStarStrategy(), "A*")
    ]
    
    for name, strategy, description in strategies:
        solver = MazeSolver(maze, strategy)
        view.update("search_start", {"algorithm": description})
        
        path, stats = solver.solve()
        
        if stats.path_found:
            view.update("path_found", {"maze": maze, "path": path, "stats": stats})
        else:
            view.update("no_path", {"stats": stats})


def demo_player_controls():
    print("\n" + "=" * 60)
    print("Command + Observer")
    print("=" * 60)
    
    builder = TextFileMazeBuilder()
    view = ConsoleView()
    maze = builder.build_from_file("mazes/small.txt")
    
    player = Player(maze.start)
    
    view.update("maze_loaded", {"maze": maze})
    view.render(maze, player_position=player.current_cell)


def run_experiments():
    print("\n" + "=" * 60)
    print("ЭКСПЕРИМЕНТАЛЬНОЕ СРАВНЕНИЕ АЛГОРИТМОВ")
    print("=" * 60)
    
    maze_files = [
        "mazes/small.txt",
        "mazes/medium.txt",
        "mazes/large.txt",
        "mazes/empty.txt",
        "mazes/no_exit.txt"
    ]
    
    results = run_all_experiments(maze_files, repeats=5)
    save_results_to_csv(results)
    print_results_table(results)


def main():
    print("Объектно-ориентированная реализация с паттернами")
    print("Паттерны: Builder, Strategy, Observer, Command")
    
    create_test_mazes()
    demo_maze_solver()
    demo_player_controls()
    run_experiments()


if __name__ == "__main__":
    main()