import requests
from bs4 import BeautifulSoup


def fetch_total_and_wind_production(session=None):
    """Returns an array that contains [time, total production, wind production] like below
        ['00:00', 689.0, 18.0],
        ['00:15', 680.0, 17.0],
        ['00:30', 673.0, 12.0]
    """
    r = session or requests.session()
    url = 'http://www.dsm.org.cy/en/daily-system-generation-on-the-transmission-system-mw/total-daily-system-generation-mw'
    response = r.get(url)
    html = response.text    # we have an html page from the url above that contains the raw data for the generation table
    soup = BeautifulSoup(html, 'html.parser')

    # first we ensure the column headers are what we expect, otherwise exception
    header = soup.find_all('tr')[0].find_all('th')
    if (header[0].string != 'Time' or
            header[1].string != 'Generation Forecast' or
            header[2].string != 'Actual Generation (MW)' or
            header[3].string != 'Total Available Generation Capacity' or
            header[4].string != 'Wind Farms Generation'):
        raise Exception('Mapping for Cyprus has changed')

    # now we can parse the table to find all the lines that contain actual and wind values
    rows = soup.find_all('tr')[1:]
    res = []
    for row in rows:
        cols = row.find_all('td')
        try:
            time = cols[0].string
            actual = float(cols[2].string)
            wind = float(cols[4].string)
            res.append([time, actual, wind])
        except (TypeError, ValueError):
            pass    # this ensure we read only the columns we have actual and wind production

    return res