from datetime import datetime

import pytz

from providers.alert.http_alert_provider import HTTPAlertProvider
from data.alert import Alert
from data.sig_ref import SIGRef


class WWFF(HTTPAlertProvider):
    """Alert provider for Worldwide Flora and Fauna"""

    POLL_INTERVAL_SEC = 1800
    ALERTS_URL = "https://spots.wwff.co/static/agendas.json"

    def __init__(self, provider_config):
        super().__init__("WWFF", provider_config, self.ALERTS_URL, self.POLL_INTERVAL_SEC)

    def _http_response_to_alerts(self, http_response):
        new_alerts = []
        # Iterate through source data
        for source_alert in http_response.json():
            # Convert to our alert format
            alert = Alert(source=self.name,
                          source_id=source_alert["id"],
                          dx_calls=[source_alert["activator_call"].upper()],
                          freqs_modes=source_alert["band"] + " " + source_alert["mode"],
                          comment=source_alert["remarks"],
                          sig_refs=[SIGRef(id=source_alert["reference"], sig="WWFF")],
                          start_time=datetime.strptime(source_alert["utc_start"],
                                                       "%Y-%m-%d %H:%M:%S").replace(tzinfo=pytz.UTC).timestamp(),
                          end_time=datetime.strptime(source_alert["utc_end"],
                                                     "%Y-%m-%d %H:%M:%S").replace(tzinfo=pytz.UTC).timestamp(),
                          is_dxpedition=False)

            # Add to our list
            new_alerts.append(alert)
        return new_alerts
