import os
from observer.observer import Observer


class ConsoleView(Observer):
    def update(self, event):
        if isinstance(event, str):
            print(f"[EVENT] {event}")
        elif isinstance(event, dict):
            event_type = event.get("type", "unknown")
            if event_type == "search_finished":
                stats = event.get("stats")
                print(f"[EVENT] search finished: {stats}")
            else:
                print(f"[EVENT] {event_type}: {event}")
        else:
            print("[EVENT] unknown")

    def clear(self):
        os.system("cls" if os.name == "nt" else "clear")

    def render(self, maze, player_position=None, path=None, clear_screen=False):
        if clear_screen:
            self.clear()
        print(maze.render(player_position=player_position, path=path))
        print()
