import datetime
import logging
import os
import sys

# ----------------------------------------------------------------------------
# env helpers


def getenv(key: str, default: bool = False) -> bool:
    """Convert '0','1' env values to bool {True, False}"""
    return bool(int(os.getenv(key, default)))


# ----------------------------------------------------------------------------
# constants

PY_VER_MINOR = sys.version_info.minor
COLOR = getenv("COLOR", default=True)

# ----------------------------------------------------------------------------
# logging config


class CustomFormatter(logging.Formatter):
    """custom logging formatting class"""

    white = "\x1b[97;20m"
    grey = "\x1b[38;20m"
    green = "\x1b[32;20m"
    cyan = "\x1b[36;20m"
    yellow = "\x1b[33;20m"
    red = "\x1b[31;20m"
    bold_red = "\x1b[31;1m"
    reset = "\x1b[0m"
    fmt = "%(delta)s - %(levelname)s - %(name)s.%(funcName)s - %(message)s"
    cfmt = (
        f"{white}%(delta)s{reset} - "
        f"{{}}%(levelname)s{{}} - "
        f"{white}%(name)s.%(funcName)s{reset} - "
        f"{grey}%(message)s{reset}"
    )

    FORMATS = {
        logging.DEBUG: cfmt.format(grey, reset),
        logging.INFO: cfmt.format(green, reset),
        logging.WARNING: cfmt.format(yellow, reset),
        logging.ERROR: cfmt.format(red, reset),
        logging.CRITICAL: cfmt.format(bold_red, reset),
    }

    def __init__(self, use_color=COLOR):
        self.use_color = use_color

    def format(self, record):
        """Custom logger formatting method"""
        if not self.use_color:
            log_fmt = self.fmt
        else:
            log_fmt = self.FORMATS.get(record.levelno, self.fmt)
        if PY_VER_MINOR > 10:
            duration = datetime.datetime.fromtimestamp(record.relativeCreated / 1000, datetime.UTC)
        else:
            duration = datetime.datetime.utcfromtimestamp(record.relativeCreated / 1000)
        record.delta = duration.strftime("%H:%M:%S")
        formatter = logging.Formatter(log_fmt)
        return formatter.format(record)


def config(name: str) -> logging.Logger:
    # Read DEBUG at config time, not import time, to allow CLI to set it first
    debug = getenv("DEBUG", default=False)
    strm_handler = logging.StreamHandler()
    strm_handler.setFormatter(CustomFormatter())
    # file_handler = logging.FileHandler("log.txt", mode='w')
    # file_handler.setFormatter(CustomFormatter(use_color=False))
    logging.basicConfig(
        level=logging.DEBUG if debug else logging.INFO,
        handlers=[strm_handler],
        # handlers=[strm_handler, file_handler],
    )
    return logging.getLogger(name)
