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

            self.render()

    def render(self):

        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:

                position = (cell.x, cell.y)

                if cell.is_wall:

                    line += "#"

                elif cell.is_start:

                    line += "S"

                elif cell.is_exit:

                    line += "E"

                elif position in path_positions:

                    line += "*"

                else:

                    line += " "

            print(line)