from urllib.request import urlopen
from TRG.Programs import GeoDir
from NEWAFireInfo.settings import BASE_DIR, DATABASES
import os
import datetime
import psycopg2

def fetch():

    link = "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Current_WildlandFire_Perimeters/FeatureServer/0/query?where=irwin_POOState%20%3D%20'US-WA'%20AND%20irwin_POOCounty%20%3D%20'PEND%20OREILLE'%20OR%20irwin_POOState%20%3D%20'US-WA'%20AND%20irwin_POOCounty%20%3D%20'SPOKANE'%20OR%20irwin_POOState%20%3D%20'US-WA'%20AND%20irwin_POOCounty%20%3D%20'FERRY'%20OR%20irwin_POOState%20%3D%20'US-WA'%20AND%20irwin_POOCity%20%3D%20'STEVENS'%20OR%20irwin_POOState%20%3D%20'US-WA'%20AND%20irwin_POOCounty%20%3D%20'LINCOLN'&outFields=poly_IncidentName,irwin_POOCounty,irwin_LocalIncidentIdentifier,irwin_IncidentTypeCategory,poly_Acres_AutoCalc,irwin_InitialLatitude,irwin_InitialLongitude,irwin_PercentContained&outSR=4326&f=json"

    f = urlopen(link)
    myfile = f.read()
    s=myfile.decode()

    return s

def parse(s, kmllist):
    dbstring = DATABASES['default']
    con = psycopg2.connect(
        host=dbstring['HOST'],
        port=dbstring['PORT'],
        database=dbstring['NAME'],
        user=dbstring['USER'],
        password=dbstring['PASSWORD'])
    cur = con.cursor()
    cur.execute("DELETE FROM nifcall")

    timestamp = datetime.datetime.now()
    time = timestamp.strftime('%m%d%H')
    if s.find('"attributes":') == -1:
        cur.execute("INSERT INTO nifcall(date, incnum, name, type, acres, location, trs, lat_long, fuels, resources, status, webcomment, innear) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(time,'','','','','','','','','','','',''))
    while s.find('"attributes":') != -1:
        # 2 Name
        s = s[s.find('"attributes"')+12:]
        name = s[s.find('''"poly_IncidentName":''')+21:s.find('",')].upper()
        # 5 Location
        s = s[s.find('''"irwin_POOCounty":'''):]
        loc = s[s.find('''"irwin_POOCounty":''')+19:s.find('",')].upper() + ' COUNTY'
        # 1 INCnum
        s = s[s.find('''"irwin_LocalIncidentIdentifier":'''):]
        incnum = s[s.find('''"irwin_LocalIncidentIdentifier":''')+35:s.find('",')]
        # 3 Type
        s = s[s.find('''"irwin_IncidentTypeCategory":'''):]
        inctype = s[s.find('''"irwin_IncidentTypeCategory":''')+30:s.find('",')]
        # 4 Acres
        s = s[s.find('''"poly_Acres_AutoCalc":'''):]
        acres = str(int(float(s[s.find('''"poly_Acres_AutoCalc":''')+22:s.find(',')])))
        # 7 Lat-Long
        s = s[s.find('''"irwin_InitialLatitude":'''):]
        lat = s[s.find('''"irwin_InitialLatitude":''')+24:s.find(',')]
        s = s[s.find('''"irwin_InitialLongitude":'''):]
        long = s[s.find('''"irwin_InitialLongitude":''')+25:s.find(',')]
        latlong = lat + ',' + long
        # 11 Web Comments
        s = s[s.find('''"irwin_PercentContained":'''):]
        webcom = s[s.find('''"irwin_PercentContained":''')+25:s.find('},')]
        if webcom == 'null':
            webcom = '0 PERCENT CONTAINED'
        else:
            webcom = webcom + ' Percent Contained'

        # 12 Geo Results
        rings = s[s.find('''"rings":[[[''')+11:s.find("]]]")+3]
        innear = GeoDir.poly(rings, kmllist)

        cur.execute("INSERT INTO nifcall(date, incnum, name, type, acres, location, trs, lat_long, fuels, resources, status, webcomment, innear) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(time,incnum,name,inctype,acres,loc,'',latlong,'','',webcom,'',innear))
        con.commit()
    con.close()
    return

def getnewdata():
    file = fetch()
    kmllist = []
    file_path = os.path.join(str(BASE_DIR) + '/TRG/Programs/KMLs')
    dirlist = os.listdir(file_path)
    for i in range(len(dirlist)):
        if dirlist[i].find('.kml') != -1:
            kmllist = kmllist + [dirlist[i]]
    parse(file, kmllist)
    return

def checkage():
    dbstring = DATABASES['default']
    con = psycopg2.connect(
        host=dbstring['HOST'],
        port=dbstring['PORT'],
        database=dbstring['NAME'],
        user=dbstring['USER'],
        password=dbstring['PASSWORD'])
    cur = con.cursor()
    cur.execute("SELECT date FROM nifcall")
    time1 = cur.fetchone()[0]
    timestamp = datetime.datetime.now()
    time2 = timestamp.strftime('%m%d%H')
    if abs(int(time1[:2])-int(time2[:2])) > 1:
        return False
    age = ((((int(time2[:2])*31)+int(time2[2:4]))*24)+int(time2[4:])) - ((((int(time1[:2])*31)+int(time1[2:4]))*24)+int(time1[4:]))
    return age

def filterdata(kmllistreq):
    kmllist = []
    temp = []
    nifc = []
    for i in range(len(kmllistreq)):
        if kmllistreq[i] == '1':
            pass
        else:
            kmllist = kmllist + [kmllistreq[i][kmllistreq[i].find('TA ')+3:kmllistreq[i].find('.kml')]]
    # Filter NIFCALL to NIFC
    dbstring = DATABASES['default']
    con = psycopg2.connect(
        host=dbstring['HOST'],
        port=dbstring['PORT'],
        database=dbstring['NAME'],
        user=dbstring['USER'],
        password=dbstring['PASSWORD'])
    cur = con.cursor()
    cur.execute("SELECT * FROM nifcall WHERE name!=''")
    values = cur.fetchall()

    for i in range(len(values)):
        add = True
        if kmllistreq[0] == '1':
            if len(kmllistreq) != 1:
                for j in range(len(kmllist)):
                    if values[i][12].find(kmllist[j]) != -1:
                        j = len(kmllist) + 1
                        nifc = nifc + [['',values[i][1],values[i][2],values[i][3],values[i][4],values[i][5],values[i][6],values[i][7],values[i][8],values[i][9],values[i][10],values[i][11],values[i][12]]]
                        add = False
            if add == True:
                nifc = nifc + [['',values[i][1],values[i][2],values[i][3],values[i][4],values[i][5],values[i][6],values[i][7],values[i][8],values[i][9],values[i][10],values[i][11],'']]
        else:
            for j in range(len(kmllist)):
                if values[i][12].find(kmllist[j]) != -1:
                    j = len(kmllist) + 1
                    nifc = nifc + [['',values[i][1],values[i][2],values[i][3],values[i][4],values[i][5],values[i][6],values[i][7],values[i][8],values[i][9],values[i][10],values[i][11],values[i][12]]]

    return nifc

def main(kmllistreq, override):
    isvalidcheck = checkage()
    if override == True:
        getnewdata()
    elif isvalidcheck >= 24:
        getnewdata()
    nifc = filterdata(kmllistreq)

    return nifc

#main()
#input()
