import np
import nppy
import json
import math

d = np.PyBuildAntzDict()

def npFun1():
    print "npFun1"
    # NodeType : kNPpin , parentID (if none, then 0) 
    id = np.PyNodeNew(5, 0)
    recId = 10
    tableId = 15
    np.PyTagNodeWith(id, recId, tableId, "Hello ANTz-Py")
    np.PyNodeSet(id, d['tag_mode'], 1)

def npFun2():
    print "npFun2"
    np.PyCurlGetURL("https://firemap.sdsc.edu:5443/stations/data/latest?selection=withinRadius&lat=32.88437231&lon=-117.2414495&radius=15&observable=temperature&observable=wind_speed&observable=wind_direction&all=true")
    print("PyCURLgetURL")
    raw = np.PyCurlGetRawResult()
    parsed_json = json.loads(raw)
    #element = parsed_json['features'][0]
    element = parsed_json['features'][0]['properties']['description']['wifire_uid']
    recId = 11
    tableId = 16
    print(element)
    id = np.PyNodeNew(5,0)
    np.PyTagNodeWith(id, recId, tableId, element)
    np.PyNodeSet(id, d['tag_mode'], 1)
    #print(element['wifire_uid'])

def npFun3():
    print("npFun3")
    temperatureNodes = []
    np.PyCurlGetURL("https://firemap.sdsc.edu:5443/stations/data/latest?selection=withinRadius&lat=32.88437231&lon=-117.2414495&radius=15&observable=temperature&observable=wind_speed&observable=wind_direction&all=true")
    print("asdf")
    raw = np.PyCurlGetRawResult()
    parsed_json = json.loads(raw)
    recordID = 10
    tableID = 5
    lowTemp = 150
    highTemp = 0
    descRecId = 100
    temps = {}
    for element in parsed_json['features']:
        id = np.PyNodeNew(5, 0)
        json_properties = element['properties']
        propId = np.PyNodeNew(5, id)
        json_desc = json_properties['description']
        descId = np.PyNodeNew(5, propId)
        np.PyTagNodeWith(descId, descRecId, 15, str(json_desc['name']))
        descRecId = descRecId + 1
        np.PyNodeSet(descId, d['tag_mode'], 1)
        json_geometry = element['geometry']
        longitude = json_geometry['coordinates'][0]
        latitude  = json_geometry['coordinates'][1]
        altitude  = json_geometry['coordinates'][2]
        np.PyNodeSet(id, d['translate'], longitude, latitude, altitude)
        myLocation = np.PyNodeGet(id, d['translate'])
        wind_speed = json_properties['wind_speed']
        print("windspeed:")
        print(wind_speed['value'])
        windN = np.PyNodeNew(5, propId)
        np.PyNodeSet(windN, d['geometry'], 3)
        np.PyNodeSet(windN, d['scale'], wind_speed['value'], 1, 1)
        np.PyNodeSet(windN, d['rotate'], 1, 1, json_properties['wind_direction']['value'])
        temperatureN = np.PyNodeNew(5, id)
        temperatureNodes.append(temperatureN)
        temps[temperatureN] = json_properties['temperature']['value']
        print json_properties['temperature']['value']
        if json_properties['temperature']['value'] < lowTemp :
            lowTemp = json_properties['temperature']['value']
        if json_properties['temperature']['value'] > highTemp :
            highTemp = json_properties['temperature']['value']

    print("lowTemp : ")
    print lowTemp
    print("highTemp : ")
    print highTemp
    recordID = 10
    for x in temperatureNodes:
        r = ((255 * (temps[x]+lowTemp)) / highTemp)
        b = ( 255 * (highTemp - (temps[x]+lowTemp)) ) / highTemp
        g = 0   
        print ""
        print "temperature : ", temps[x]
        print "r : ", r ,",", "g : ", g ,",", "b : ", b
        print ""
        np.PyNodeSet(x, d['color'], r, g, b, 255)
        np.PyTagNodeWith(x, recordID, 15, str(temps[x]))
        recordID = recordID + 1


def npFun4():
    print "npFun4"
    id = np.PyNodeNew(5, 0)
    np.PyNodeSet(id, d['translate'], 30, 40, 0)
    myLocation = np.PyNodeGet(id, d['translate'])
    print myLocation[0]
    print myLocation[1]
    print myLocation[2]

def npFun5():
    print "npFun5"
    for i in range(0, 5):
        id = np.PyNodeNew(5, 0)

def npFun6():
    print "npFun6"
    for i in range(0, 6):
        id = np.PyNodeNew(5, 0)

def npFun7():
    print "npFun7"
    for i in range(0, 7):
        id = np.PyNodeNew(5, 0)

def npFun8():
    print "npFun8"
    for i in range(0, 8):
        id = np.PyNodeNew(5, 0)

def npFun9():
    print "npFun9"
    for i in range(0, 9):
        id = np.PyNodeNew(5, 0)

def npFun0():
    print"npFun0"
    for i in range(0, 10):
        id = np.PyNodeNew(5, 0)

