import time
import PytorchBoot.namespace as namespace
from PytorchBoot.status import status_manager

class Log:
    MAX_TITLE_LENGTH:int = 7
    TYPE_COLOR_MAP = {
        namespace.LogType.INFO: "\033[94m",
        namespace.LogType.ERROR: "\033[91m",
        namespace.LogType.WARNING: "\033[93m",
        namespace.LogType.SUCCESS: "\033[92m",
        namespace.LogType.DEBUG: "\033[95m",
        namespace.LogType.TERMINATE: "\033[96m"
    }
    def get_time():
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    
    def blue(message):
        # blue
        print(f"\033[94m{message}\033[0m")
    def red(message):
        # red
        print(f"\033[91m{message}\033[0m")
    def yellow(message):
        # yellow
        print(f"\033[93m{message}\033[0m")
    def green(message):
        # green
        print(f"\033[92m{message}\033[0m")
        
    def log(message, log_type: str):
        time_str = Log.get_time()
        space = ""
        if len(log_type) < Log.MAX_TITLE_LENGTH:
            space = " " * (Log.MAX_TITLE_LENGTH - len(log_type))
        
        print (f"\033[1m\033[4m({time_str})\033[0m \033[1m{Log.TYPE_COLOR_MAP[log_type]}[{log_type.capitalize()}]\033[0m{space} {Log.TYPE_COLOR_MAP[log_type]}{message}\033[0m")  
        status_manager.add_log(time_str, log_type, message)
    
    def bold(message):
        print(f"\033[1m{message}\033[0m")
    def underline(message):
        print(f"\033[4m{message}\033[0m")
        
    def info(message):
        Log.log(message, namespace.LogType.INFO)
        
    def error(message, terminate=False):
        Log.log(message, namespace.LogType.ERROR)
        if terminate:
            Log.terminate("Application Terminated.")
            
            
    def warning(message):
        Log.log(message, namespace.LogType.WARNING)
    def success(message):
        Log.log(message, namespace.LogType.SUCCESS)
        
    def debug(message):
        Log.log(message, namespace.LogType.DEBUG)

    def terminate(message):
        Log.log(message, namespace.LogType.TERMINATE)
        exit(1)  

if __name__ == "__main__":
    Log.info("This is a info message")
    Log.error("This is a error message")
    Log.warning("This is a warning message")
    Log.success("This is a success message")
    Log.debug("This is a debug message")
    Log.blue("This is a blue message")
    Log.red("This is a red message")
    Log.yellow("This is a yellow message")
    Log.green("This is a green message")
    
    Log.bold("This is a bold message")
    Log.underline("This is a underline message")
    Log.error("This is a terminate message", True)
        
        