from observer.observer import Observer


class ConsoleView(Observer):

    def update(self, event):

        event_type = event.get("type")

        if event_type == "maze_loaded":
            print("[VIEW] Maze loaded")

        elif event_type == "search_started":
            print("[VIEW] Search started")

        elif event_type == "search_finished":
            print("[VIEW] Search completed")

        elif event_type == "path_found":
            print(
                f"[VIEW] Path length: "
                f"{event.get('length')}"
            )

    def render(
        self,
        maze,
        path=None
    ):
        route_marks = set()

        if path:
            for cell in path:
                route_marks.add(
                    cell.getPosition()
                )

        screen = []

        for row in maze.cells:

            visual_row = ""

            for cell in row:

                position = cell.getPosition()

                if (
                    position in route_marks
                    and not cell.isStart
                    and not cell.isExit
                ):
                    visual_row += "*"

                else:
                    visual_row += str(cell)

            screen.append(
                visual_row
            )

        print(
            "\n".join(screen)
        )