import logging
from logging import Logger

from cfgtools import read
from helper import config_logger
from model.config import Config


class Configurator(object):

    def __init__(self, loader_path):
        # type: (str) -> None

        self.config = Config()
        self.config.loader_path = loader_path
        self.reader = read(loader_path)

    def read_loader_configs(self):
        # type: () -> Config

        group = "AuditLogger."

        self.config.component_name = str(self.reader.find_value("Identification.ComponentName"))
        self.config.output_folder = self.reader.find_value(group + "output_folder")
        self.config.encode = (self.reader.find_value(group + "encode") or "true").lower() == "true"
        self.config.log_level = (self.reader.find_value(group + "LogLevel") or "INFO").upper()
        self.config.connection_timeout = int(self.reader.find_value(group + "ConnectionTimeout") or "5")

        return self.config

    def configure_logger(self):
        # type: () -> Logger

        config_logger(self.config.loader_path, self.config.component_name, root_level=logging.DEBUG)
        return logging.getLogger(self.config.component_name)
