import csv
from datetime import datetime
import os
from NEWAFireInfo.settings import BASE_DIR


def point(loc, point):
    # NEAR THRESHOLD DISTANCE IN MILES
    neardist = 2
    latconst = 0.01448
    longconst = 0.02167

    file_path = os.path.join(str(BASE_DIR) + '/TRG/Programs/KMLs/')

    temp = point.split(',')
    if temp[0] == 'x' and temp[1]== 'x':
        return ''
    x = [float(temp[0]), float(temp[1])]

    with open(file_path + loc) as f:
        s = f.read()

        # ADD code to store highs/lows in kml if not already done, if already done grab highs.lows from file
    if s.find('<highlow>') != -1:
        lltemp = s[s.find('<highlow>')+9:s.find('</highlow>')]
        lltemp = lltemp.split(',')
        highlat = float(lltemp[0])
        lowlat = float(lltemp[1])
        highlong = float(lltemp[2])
        lowlong = float(lltemp[3])
        s=s[s.find('<coordinates>')+13:s.find('</coordinates>')]
        ll = s.split(',0 ')
        for i in range(len(ll)-1):
            swap=ll[i].split(',')
            ll[i]=[swap[1],swap[0]]

    else:
        s=s[s.find('<coordinates>')+13:s.find('</coordinates>')]
        ll = s.split(',0 ')
        
        highlat = float(0)
        lowlat = float(90)
        highlong = float(-180)
        lowlong = float(0)

        for i in range(len(ll)-1):
            swap=ll[i].split(',')
            ll[i]=[swap[1],swap[0]]

            if float(ll[i][0]) > highlat:
                highlat = float(ll[i][0])
            if float(ll[i][0]) < lowlat:
                lowlat = float(ll[i][0])
            if float(ll[i][1]) > highlong:
                highlong = float(ll[i][1])
            if float(ll[i][1]) < lowlong:
                lowlong = float(ll[i][1])

    # If point is near then full geo, if not near skip full Geo
    isnear = False
    count = 0
    if x[0] <= highlat + (latconst * neardist) and x[0] >= lowlat - (latconst * neardist) and x[1] <= highlong + (longconst * neardist) and x[1] >= lowlong - (longconst * neardist):
        isnear = True
        for i in range(len(ll)-2):
            if ll[i][0] > ll[i+1][0]:
                y1 = float(ll[i+1][0])
                x1 = float(ll[i+1][1])
                y2 = float(ll[i][0])
                x2 = float(ll[i][1])            
            else:
                y1 = float(ll[i][0])
                x1 = float(ll[i][1])
                y2 = float(ll[i+1][0])
                x2 = float(ll[i+1][1])
            
            if x[0] >= y1 and x[0] <= y2:
                m = (y2-y1)/(x2-x1)
                b = y1 - (m * x1)
                if x[1] == (x[0] - b) / m:
                    # If on geofence stop, point is IN .kml
                    i = len(ll)+1
                    count = 1
                elif x[1] <= (x[0] - b) / m:
                    count = count + 1

    # NOT NEAR .kml
    else:        
        isin = ''

    # NEAR .kml
    if isnear == True and (count % 2) == 0:
        if loc.find('TA') != -1:
            isin = 'NEAR ' + loc[3:loc.find('.kml')]
        else:
            isin = 'NEAR ' + loc[:loc.find('.kml')]
    
    # IN .kml
    elif isnear == True and (count % 2) != 0:
        if loc.find('TA') != -1:
            isin = 'IN ' + loc[3:loc.find('.kml')]
        else:
            isin = 'NEAR ' + loc[:loc.find('.kml')]

    f.close()
    return isin
