from ..Logging import Logger

from .UI import UI
from .TermColors import *


class CLI(UI):
    def __enter__(self):
        Logger.add_hook(self.print_log)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        Logger.remove_hook(self.print_log)

    def print(self, message):
        print(message)

    def get_input(self, prompt: str) -> str:
        return input(f"{prompt} ")

    def get_password(self, prompt: str, verify=False) -> str:
        import getpass
        pass_1 = 'pass_1'
        pass_2 = 'pass_2'
        while pass_1 != pass_2:
            pass_1 = getpass.getpass(prompt=f"{prompt} ")
            if not verify:
                return pass_1
            pass_2 = getpass.getpass(prompt=f"Retype the password: ")
        return pass_1

    def confirm(self, prompt: str):
        input(prompt)

    @staticmethod
    def print_log(severity: Logger.Severity, caller: str, message: str):
        color_fg = COL_WHITE
        color_bg = COL_BG_BLACK
        color_extras: list[str] = list()

        if severity.value <= Logger.Severity.ERROR.value:
            color_fg = COL_LIGHT_RED
            if severity.value <= Logger.Severity.WTF.value:
                color_extras.append(COL_BLINK)
        elif severity == Logger.Severity.WARN:
            color_fg = COL_YELLOW
        elif severity == Logger.Severity.INFO:
            color_extras.append(COL_BOLD)
        elif severity.value >= Logger.Severity.DEBUG.value:
            color_fg = COL_LIGHT_GREY

        reset_sequence = f'{COL_END}{color_bg}'
        severity_message = f'{COL_BOLD}[{reset_sequence}{"".join(color_extras)}{color_fg}{severity.name}{reset_sequence}{COL_BOLD}]'
        caller_message = f'{COL_ITALIC}{caller.split(".")[-1]}'
        log_message = f'{severity_message}\t{reset_sequence} {caller_message}{reset_sequence}: {message}{COL_END}'
        print(log_message)