from typing import Literal

YES, NO, YES_FOR_ALL = "Yes", "No", "YesForAll"
ConfirmResponse = Literal["Yes", "No", "YesForAll"]


def ask_is_yes(question) -> ConfirmResponse:
    assert question.endswith("?"), f"This is not question: '{question}'"
    # todo mark colors
    response = input(question.strip() + " (y (yes: default) / n (no) / a (yes for all)) ")
    answer = response.strip().lower()
    if len(answer) == 0 or answer.startswith("y"):
        return "Yes"
    if answer.startswith("n"):
        return "No"
    if answer.startswith("a"):
        return "YesForAll"
    print(f"Failed to parse response: `{answer}, fallback to `no`...")
    return "No"


def _auto_yes(question) -> ConfirmResponse:
    assert question.endswith("?"), f"This is not question: '{question}'"
    print(question.strip() + " (y (default) / n) > AUTOYES")
    return "Yes"


class Asker:
    def __init__(self, is_auto_yes: bool = False):
        self.asker = _auto_yes if is_auto_yes else ask_is_yes

    def enable_autoyes(self):
        self.asker = _auto_yes

    def ask_is_yes(self, question) -> bool:
        response = self.asker(question)
        if response == YES:
            return True
        elif response == NO:
            return False
        elif response == YES_FOR_ALL:
            self.asker = _auto_yes
            return True
        else:
            raise ValueError(f"Should not happen: {response}")
