from commands.move_command import MoveCommand


class GameController:
    def __init__(self, maze, player, view):
        self.maze = maze
        self.player = player
        self.view = view
        self.history = []

    def move(self, direction):
        command = MoveCommand(self.player, self.maze, direction)
        if command.execute():
            self.history.append(command)
            self.view.update({"type": "move", "direction": direction})
            self.view.render(self.maze, player_position=self.player.currentCell)
            return True
        print("Cannot move there")
        return False

    def undo(self):
        if not self.history:
            print("Nothing to undo")
            return False
        command = self.history.pop()
        if command.undo():
            self.view.update({"type": "undo"})
            self.view.render(self.maze, player_position=self.player.currentCell)
            return True
        return False
