import time

class SearchStats:
    def __init__(self, time_ms, visited_cells, path_length):
        self.time_ms = time_ms
        self.visited_cells = visited_cells
        self.path_length = path_length
    def __str__(self):
        return (
            f"Время: "
            f"{self.time_ms:.3f} мс\n"

            f"Посещено клеток: "
            f"{self.visited_cells}\n"

            f"Длина пути: "
            f"{self.path_length}"
        )
    
class MazeSolver:
    def __init__(self,maze, strategy):
        self.maze = maze
        self.strategy = strategy
        self.observers = []
    def setStrategy(self, strategy
    ):
        self.strategy = strategy
    def solve(self):
        self.notify("Начат поиск")
        start_time = (time.perf_counter())
        path, visited = (self.strategy.findPath(self.maze,self.maze.start,self.maze.exit))
        end_time = (time.perf_counter())
        self.notify("Путь найден")
        time_ms = ((end_time-start_time)*1000)
        visited = len(path)
        stats = SearchStats(time_ms,visited,len(path))
        return stats
    def addObserver(self, observer):
        self.observers.append(observer)
    def notify(self, event):
        for observer in self.observers:
            observer.update(event)