import os
from observer.observer import Observer
from observer.maze_event import MazeEventType


class ConsoleView(Observer):
    def __init__(self, maze=None):
        self.maze = maze
        self.path = []

    def update(self, event):
        if event.event_type == MazeEventType.PATH_FOUND:
            self.path = event.data if event.data else []
            self.render()

    def render(self):
        if self.maze is None:
            return
            
        os.system("cls" if os.name == "nt" else "clear")
        path_positions = {(cell.x, cell.y) for cell in self.path}

        for row in self.maze.cells:
            line = ""
            for cell in row:
                pos = (cell.x, cell.y)
                if cell.is_wall:
                    line += "#"
                elif cell.is_start:
                    line += "S"
                elif cell.is_exit:
                    line += "E"
                elif pos in path_positions:
                    line += "*"
                else:
                    line += " "
            print(line)