from .observer import Observable, Event, EventType
from .command import MoveCommand, Player

class GameController(Observable):
    def __init__(self, maze):
        super().__init__()
        self.maze = maze
        self.player = Player(maze.start)
        self.command_history = []
    
    def move(self, direction):
        """Перемещает игрока в направлении"""
        cmd = MoveCommand(self.player, direction, self.maze, self)
        if cmd.execute():
            self.command_history.append(cmd)
            # Правильный вызов notify с одним аргументом Event
            self.notify(Event(EventType.PLAYER_MOVED, self.player.current_cell))
            return True
        return False
    
    def undo(self):
        """Отменяет последнее действие"""
        if self.command_history:
            cmd = self.command_history.pop()
            cmd.undo()
            self.notify(Event(EventType.UNDO, None))
            return True
        return False
    
    def reset(self):
        """Сбрасывает игру"""
        self.player.reset()
        self.command_history.clear()
        self.notify(Event(EventType.PLAYER_MOVED, self.player.current_cell))
    
    def get_player_position(self):
        """Возвращает позицию игрока"""
        return self.player.current_cell