import json
import datetime
from park_api.util import convert_date
from park_api.geodata import GeoData

# This loads the geodata for this city if <city>.geojson exists in the same directory as this file.
# No need to remove this if there's no geodata (yet), everything will still work.
geodata = GeoData(__file__)

# This function is called by the scraper and given the data of the page specified as source in geojson above.
# It's supposed to return a dictionary containing everything the current spec expects. Tests will fail if it doesn't ;)
def parse_html(html):
    data = json.loads(html)
    lots = {
            "lots":[],
            "last_updated":None
            }
    id_lots = {}
    for l in geodata.lots:
        aux = json.loads(geodata.lots[l].aux)
        id_lots[aux["identifier"]] = {"lot":geodata.lots[l],
                                      "open":aux["open"]}
    timestamps = []
    for feature in data["features"]:
        try:
            if id_lots[feature["attributes"]["IDENTIFIER"]]["open"]:
                state = "open"
            else:
                if feature["attributes"]["KAPAZITAET"] == -1:
                    state = "nodata"
                else:
                    state = "unknown"
            lot = id_lots[feature["attributes"]["IDENTIFIER"]]["lot"]
            lots["lots"].append({
                "coords":lot.coords,
                "name":lot.name,
                "total":lot.total,
                "free":feature["attributes"]["KAPAZITAET"],
                "state":state,
                "id":lot.id,
                "lot_type":lot.type,
                "address":lot.address,
                "forecast":False,
                "region":""
            })
            timestamps.append(convert_date(feature["attributes"]["TIMESTAMP"], "%Y-%m-%d %H:%M:%S"))
        except KeyError:
            pass
    timestamps.sort()
    timestamps.reverse()
    lots["last_updated"] = timestamps[0]
    return lots


@@ -1425,16 +1425,20 @@
 %22total%22:
+int(
 lot.tota
@@ -1438,16 +1438,17 @@
 ot.total
+)
 ,%0A      
@@ -1464,16 +1464,20 @@
  %22free%22:
+int(
 feature%5B
@@ -1503,16 +1503,17 @@
 ZITAET%22%5D
+)
 ,%0A      
@@ -1839,16 +1839,17 @@
 ept 
+(
 KeyError
 :%0A  
@@ -1844,16 +1844,29 @@
 KeyError
+, ValueError)
 :%0A      

fix total and free type