import v
from v import *

def zu_load_departures(station, operator, country="ch"):
    
    data = requests.get("https://search.ch/timetable/api/stationboard.json?stop=" + station).text
    x = json.loads(data)
    
    x = x["connections"]
    
    
    search_list = {}
    results = []
    for departures in x:
        try: 
            _time = departures["time"]
            line = departures["line"]
            __type = departures["type"]
            name = departures["terminal"]["name"]
        except: pass
        
        
        try:
            _time = _time[:19]
            
            
            if __type == "m": __type = "metro"
            elif __type == "regional": __type = "train"
            elif __type == "re": __type = "train"
            elif __type == "metro": __type = "metro"
            elif __type == "express": __type = "train"
            elif __type == "lyn": __type = "train"
            elif __type == "reg": __type = "train"
            elif __type == "ic": __type = "train"
            elif __type == "s-tog": __type = "train"
            elif __type == "tog": __type = "train"
            elif __type == "exb": __type = "bus"
            elif __type == "nb": __type = "bus"
            elif __type == "tb": __type = "bus"
            elif __type == "f": __type = "ship"
            elif __type == "let": __type = "tram"
            elif __type == "hv-bus": __type = "bus"
            elif "bus" in __type: __type = "bus"
            elif "ic" in __type: __type = "train"

            if "train" in __type: __type = "train"

            
            #print(__type)

            new_record = {"destination" : name.replace("via ", ""),
                        "direction_code" : "0",
                        "expected" : _time[:19].replace(" ", "T"),
                        "line" : {"id": line.replace("-","."),
                        "transport_mode":__type.upper()},
                        "deviations": [],
                        }
            results.append(new_record)
            if len(results) > 50: break
            
        except Exception as e: 
            print("Error: ", e)
            continue


    if len(results):
        print(results)
        v.operators[country][operator][station]["departures"] = results
        return results
        print("Success")
    
    all_types = "&useMetro=0&useBus=0&useTog=0"
    #cycle_list =  {"Metro=0":"Metro=1", "Tog=0":"Tog=1", "Bus=0":"Bus=1"}
    cycle_list =  {"Metro=0":"Metro=1"}
    results = []
    
    if len(results):
        v.operators[country][operator][station]["departures"] = results
        return results




    


def ch_zu_lookup(station, operator, country="ch"):

    print(station)
    station = station.replace(" ", "%20")
    
    #try: xml_data  = fetch_data("xmlopen.rejseplanen.dk", 80, "/bin/rest.exe/location?input=" + station)
    try: data = requests.get("https://search.ch/timetable/api/station.json?stop=" + station).text
    except Exception as e: print("Error. ", e)
    
    data = json.loads(data)
    search_list = {}
    print(data)
    try:
        
        name = data["name"]
        siteid = data["id"]
        print(name, siteid)
        v.sites[country][operator][name] = siteid
        search_list[name] = siteid
    except Exception as e:
        print("Error: ", e)
    
    print(search_list)
    return search_list

#ch_zu_lookup("el")
#zu_load_departures("8506014")
