import logging
import threading

from core.config import config, create_provider_from_config


class DataProviders:
    """Global object for storing data providers."""

    def __init__(self):
        self.spot_providers = []
        self.alert_providers = []
        self.solar_condition_providers = []
        self.static_data_providers = []
        self.sig_ref_data_providers = []
        self.callsign_data_providers = []


    def setup(self):
        for entry in config["spot_providers"]:
            self.spot_providers.append(create_provider_from_config("providers.spot", entry))
        for entry in config["alert_providers"]:
            self.alert_providers.append(create_provider_from_config("providers.alert", entry))
        for entry in config.get("solar_condition_providers", []):
            self.solar_condition_providers.append(create_provider_from_config("providers.solarconditions", entry))
        for entry in config.get("static_data_providers", []):
            self.static_data_providers.append(create_provider_from_config("providers.staticdata", entry))
        for entry in config.get("sig_ref_data_providers", []):
            self.sig_ref_data_providers.append(create_provider_from_config("providers.sigrefdata", entry))
        for entry in config.get("callsign_data_providers", []):
            self.callsign_data_providers.append(create_provider_from_config("providers.callsigndata", entry))

    @staticmethod
    def start_providers(providers, type):
        """Helper method to activate enabled providers in the list."""

        logging.info(f"Starting %s providers...", type)
        for p in providers:
            if p.enabled:
                p.start()

    def start(self):
        # Start data providers before spot/alert providers so the lookup data is there already for incoming spots.
        # Each category is fired off after a small delay to give the rest of Spothole chance to start up.
        threading.Timer(5.0, lambda: self.start_providers(self.static_data_providers, "static data")).start()
        threading.Timer(10.0, lambda: self.start_providers(self.callsign_data_providers, "callsign data")).start()
        threading.Timer(15.0, lambda: self.start_providers(self.spot_providers, "spot")).start()
        threading.Timer(20.0, lambda: self.start_providers(self.alert_providers, "alert")).start()
        threading.Timer(25.0, lambda: self.start_providers(self.solar_condition_providers, "solar condition")).start()
        threading.Timer(30.0, lambda: self.start_providers(self.sig_ref_data_providers, "SIG ref data")).start()

    def stop(self):
        for sp in self.spot_providers:
            if sp.enabled:
                sp.stop()
        for ap in self.alert_providers:
            if ap.enabled:
                ap.stop()
        for scp in self.solar_condition_providers:
            if scp.enabled:
                scp.stop()
        for srdp in self.sig_ref_data_providers:
            if srdp.enabled:
                srdp.stop()
        for sdp in self.static_data_providers:
            if sdp.enabled:
                sdp.stop()
        for cdp in self.callsign_data_providers:
            if cdp.enabled:
                cdp.stop()

# Global object
DATA_PROVIDERS = DataProviders()