from datetime import datetime

from data.sig_ref import SIGRef
from data.spot import Spot
from spotproviders.http_spot_provider import HTTPSpotProvider


class Tiles(HTTPSpotProvider):
    """Spot provider for Tiles on the Air"""

    POLL_INTERVAL_SEC = 120
    SPOTS_URL = "https://icneuzxitdqtofutxbla.supabase.co/functions/v1/spots?active_hours=24"

    def __init__(self, provider_config):
        super().__init__(provider_config, self.SPOTS_URL, self.POLL_INTERVAL_SEC)

    def _http_response_to_spots(self, http_response):
        new_spots = []
        # Iterate through source data
        for source_spot in http_response.json()["spots"]:
            # Convert to our spot format
            spot = Spot(source=self.name,
                        source_id=source_spot["id"],
                        dx_call=source_spot["call_sign"].upper(),
                        # No separate spotter callsign, assume all spots are self-spots
                        de_call=source_spot["call_sign"].upper(),
                        freq=float(strip_extra_decimal_points(source_spot["frequency"])) * 1000000,
                        mode=source_spot["mode"].upper(),
                        comment=source_spot["notes"],
                        sig="Tiles",
                        # Tiles spots can include POTA & SOTA references, but ignore those on the basis that we will get them separately from the POTA/SOTA providers anyway.
                        # Just take the grid reference itself as the single Tiles SIG reference.
                        sig_refs=[SIGRef(id=source_spot["maidenhead_grid"], sig="Tiles",
                                         name=source_spot["maidenhead_grid"])],
                        time=datetime.fromisoformat(source_spot["created_at"].replace("Z", "+00:00")).timestamp(),
                        dx_grid=source_spot["maidenhead_grid"],
                        dx_latitude=source_spot["latitude"],
                        dx_longitude=source_spot["longitude"])

            # Add to our list. Don't worry about de-duping, removing old spots etc. at this point; other code will do
            # that for us.
            new_spots.append(spot)
        return new_spots


# Utility function to keep the first decimal point in a given string but remove any others. Used to parse Tiles'
# strange frequency format where we can sometimes have e.g. "14.123.5".
def strip_extra_decimal_points(s):
    parts = s.split('.', 1)
    if len(parts) == 1:
        return s
    return parts[0] + '.' + parts[1].replace('.', '')
