import os
import logging
from datetime import datetime


class LoggerHandler:
    """
        The following class holds static methods that are used to configure python logger
        to print log prints for log file
    """

    @staticmethod
    def init_logger(log_level: str, log_format: str, log_file_name: str):
        log_levels = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR}
        log_level = log_levels.get(log_level, logging.INFO)
        logging.basicConfig(
            filename=log_file_name,
            format=log_format,
            level=log_level)

    @staticmethod
    def print_message(message: str, logger_level='info', screen_print=False):
        """
        The following method is used to log messages in specific logger level.
        """
        log_level = getattr(logging, logger_level.upper(), logging.INFO)
        logging.log(level=log_level, msg=message)
        if screen_print:
            print(f"{datetime.now()} - {logger_level.upper()} - root - {message}")

    @staticmethod
    def is_logger_configured():
        """
        Check if the script logger is configured or not.
        """
        return any(handler for handler in logging.getLogger().handlers)

    @staticmethod
    def log_script_result(message: str, logger_level='info', screen_print=False):
        """
        The following method is used at the end of the script in order to print
        the final script results.
        """
        log_level = getattr(logging, logger_level.upper(), logging.INFO)
        logging.log(level=log_level, msg=f"export script results:\n message: {message}\n")
        if screen_print:
            print(f"{datetime.now()} - {logger_level.upper()} - root - export script results:\n"
                  f"message: {message}")
