import csv
import logging
from time import sleep

from pyhamtools.locator import latlong_to_locator

from data.sig_ref import SIGRef
from providers.sigrefdata.file_download_sig_ref_data_provider import FileDownloadSIGRefDataProvider


class WCA(FileDownloadSIGRefDataProvider):
    """SIG ref data provider for World Castles Award"""

    POLL_INTERVAL_DAYS = 30
    SIG = "WCA"
    DATA_URL = "https://polo.ham2k.com/data/activities/wca/all-castles.csv"

    def __init__(self, provider_config):
        super().__init__(self.SIG, provider_config, self.DATA_URL, self.POLL_INTERVAL_DAYS)

    def _http_response_to_data(self, http_response):
        new_data = []
        for row in csv.DictReader(http_response.content.decode("utf-8-sig").splitlines()):
            ref_id = row["REF"]

            coords_str = row["COORDINATES"]
            latitude = None
            longitude = None
            grid = None
            try:
                if coords_str:
                    split = coords_str.split(", ")
                    latitude = float(split[0])
                    longitude = float(split[1])
                    grid = latlong_to_locator(latitude, longitude)
            except ValueError:
                logging.debug(f"Encountered dodgy formatting in WCA CSV, skipping location data for %s", ref_id)

            new_data.append(SIGRef(sig=self.SIG, id=ref_id, name=row["CLEAN NAME"] if "CLEAN NAME" in row else None,
                                      ref_type="Castle",
                                      url="https://www.cqgma.org/zinfo.php?ref=" + ref_id,
                                      latitude=latitude,
                                      longitude=longitude,
                                      grid=grid))

            # Bail out if a stop has been requested, i.e. the program is shutting down - no need to parse the rest of
            # the data in this case
            if self._stop_event.is_set():
                break

            # Very short pause. This will extend the time to handle sig refs by a few seconds but will ensure some time
            # is available for other threads e.g. the web server.
            sleep(0.001)

        return new_data
