class Menu:
    def __init__(self, title="Menu", prompt="?"):
        self.title = title
        self.prompt_text = prompt
        self.options = []


    def get_option(self, id):
        option = [x for x in self.options if x.id == id]
        if len(option) == 0:
            return None
        else:
            return option[0]

    
    def add(self, id, name, func=None):
        self.options.append(MenuEntry(id, name, func))


    def prompt(self, mode="run", loop=False):
        print(f"\n------{self.title}------")
        for opt in self.options:
            print(f"  [{opt.id}] {opt.name}")
        while True:
            user_in = input(self.prompt_text).lower().strip()
            if self.get_option(user_in) != None:
                break
        if mode == "run":
            if self.get_option(user_in).func != None:
                self.get_option(user_in).func()
            else:
                return user_in
        elif mode == "return":
            return user_in
        


class MenuEntry:
    def __init__(self, id, name, func):
        self.id = id
        self.name = name
        self.func = func