import logging

from datetime import datetime
from logging import Logger
from typing import Optional

from cfgtools import read
from helper import config_logger

from services.model import (
    Configs,
    AlertsConfigurations,
)


class Configurator(object):

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

        self.config = Configs()
        self.config.loader_path = loader_path
        self.config.databases_path = databases_path
        self.config.backup_databases_path = backup_databases_path
        self.reader = read(file_or_path=loader_path)
        self.group = "Alerts"
        self.logger = None

    def read_loader_configs(
        self,
    ):
        # type: (...) -> Configs

        self.configure_loggers()
        self.logger.info("Start read_loader_configs")
        self.config.component_name = self.reader.find_value("Identification.ComponentName")

        maintenance_window_start = self._get_config(
            path="Alerts.Maintenance.Window.StartTime",
        )
        maintenance_window_end = self._get_config(
            path="Alerts.Maintenance.Window.EndTime",
        )

        if maintenance_window_start and maintenance_window_end:
            maintenance_window_start = datetime.strptime(maintenance_window_start, "%H:%M:%S").time()
            maintenance_window_end = datetime.strptime(maintenance_window_end, "%H:%M:%S").time()

        time_calling = self._get_config(
            path="Alerts.TimeCalling",
            default_value=1
        )
        configured_alerts_endpoint = self._get_config(
            path="Alerts.EndPoints.ConfiguredAlerts",
            default_value=""
        )
        days_to_keep_data = self._get_config(
            path="Alerts.Maintenance.DaysToKeepData",
            default_value=45
        )
        self.config.alerts = AlertsConfigurations(
            time_calling=int(time_calling),
            configured_alerts_endpoint=str(configured_alerts_endpoint),
            maintenance_window_start=maintenance_window_start,
            maintenance_window_end=maintenance_window_end,
            days_to_keep_data=int(days_to_keep_data),
        )

        self.logger.info("End read_loader_configs")

        return self.config

    def configure_loggers(
        self,
    ):
        # type: (...) -> None

        config_logger(
            loader_path=self.config.loader_path,
            log_name="Alerts",
            max_size=10485760,
            max_files=15,
        )
        self.logger = logging.getLogger(name="Alerts")

    def get_loggers(
        self,
    ):
        # type: (...) -> Logger

        return self.logger

    def _get_config(
        self,
        path,                       # type: str
        default_value=None,         # type: Optional[bool]
        multiple_values=False,      # type: Optional[bool]
    ):
        # type: (...) -> str

        if multiple_values:
            values = self.reader.find_values(
                path=path,
            )
            return values or default_value

        return self.reader.find_value(
            path=path,
            default=default_value,
        )
