from datetime import datetime

import requests

from core.constants import HTTP_HEADERS, SSB_SUB_MODES
from data.sig_ref import SIGRef
from data.spot import Spot
from providers.spot.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"
    SUBMIT_URL = "https://icneuzxitdqtofutxbla.supabase.co/functions/v1/self-spot"
    VALID_MODES = ["SSB", "CW", "FT8", "FT4", "FM", "DMR", "D-STAR", "M17", "AX.25", "JS8Call", "PSK31", "Olivia",
                   "VarAC", "Other"]

    def __init__(self, provider_config):
        super().__init__("Tiles", 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"], latitude=source_spot["latitude"],
                                         longitude=source_spot["longitude"])],
                        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"],
                        dx_location_source="GRID")

            # 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

    def can_submit_spot(self, sig):
        return sig == "Tiles"

    def submit_spot(self, spot, credentials):
        # Tiles on the air currently only supports *self* spots
        if spot.dx_call == spot.de_call:

            # Figure out a valid mode. Borrowed this from PoLo :)
            # https://github.com/ham2k/app-polo/blob/main/src/extensions/activities/sota/SOTAPostSelfSpot.js
            if spot.mode:
                mode = spot.mode
                if mode not in self.VALID_MODES:
                    if mode in SSB_SUB_MODES:
                        mode = "SSB"
                    elif mode == "OLIVIA":
                        mode = "Olivia"
                    elif mode == "JS8":
                        mode = "JS8Call"
                    else:
                        mode = "Other"

                body = {
                    "call_sign": spot.dx_call,
                    "frequency": str(spot.freq / 1000000.0),
                    "mode": mode or "",
                    "grid": spot.dx_grid or "",
                    "comment": spot.comment or "",
                    "lat": spot.dx_latitude or None,
                    "lon": spot.dx_longitude or None,
                    "qrt": spot.qrt or False,
                    "pin": credentials.get("offline_spot_gateway_pin", "")
                }
                headers = {**HTTP_HEADERS, "Content-Type": "application/json"}
                response = requests.post(self.SUBMIT_URL, json=body, headers=headers, timeout=(5, 30))
                if not response.ok:
                    raise RuntimeError(
                        "Tiles on the Air API returned " + str(response.status_code) + ": " + response.text)
            else:
                raise RuntimeError("The Tiles on the Air API requires a mode to be set.")
        else:
            raise RuntimeError(
                "The Tiles on the Air API only supports self-spots, the DX call and spotter call must match.")


# 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('.', '')
