from datetime import datetime, timedelta

import pytz
from bs4 import BeautifulSoup
from alertproviders.http_alert_provider import HTTPAlertProvider
from core.sig_utils import get_icon_for_sig
from data.alert import Alert
from data.sig_ref import SIGRef


# Alert provider for Beaches on the Air
class BOTA(HTTPAlertProvider):
    POLL_INTERVAL_SEC = 3600
    ALERTS_URL = "https://www.beachesontheair.com/"

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

    def http_response_to_alerts(self, http_response):
        new_alerts = []
        # Find the table of upcoming alerts
        bs = BeautifulSoup(http_response.content.decode(), features="lxml")
        tbody = bs.body.find('div', attrs={'class': 'view-activations-public'}).find('table', attrs={'class': 'views-table'}).find('tbody')
        for row in tbody.find_all('tr'):
            cells = row.find_all('td')
            first_cell_text = str(cells[0].find('a').contents[0]).strip()
            ref_name = first_cell_text.split(" by ")[0]
            dx_call = str(cells[1].find('a').contents[0]).strip().upper()

            # Get the date, dealing with the fact we get no year so have to figure out if it's last year or next year
            date_text = str(cells[2].find('span').contents[0]).strip()
            date_time = datetime.strptime(date_text,"%d %b - %H:%M UTC").replace(tzinfo=pytz.UTC)
            date_time = date_time.replace(year=datetime.now(pytz.UTC).year)
            # If this was more than a day ago, activation is actually next year
            if date_time < datetime.now(pytz.UTC) - timedelta(days=1):
                date_time = date_time.replace(year=datetime.now(pytz.UTC).year + 1)

            # Convert to our alert format
            alert = Alert(source=self.name,
                          dx_calls=[dx_call],
                          sig_refs=[SIGRef(id=ref_name, sig="BOTA")],
                          start_time=date_time.timestamp(),
                          is_dxpedition=False)

            new_alerts.append(alert)
        return new_alerts
