import os
import pickle
import time
import json
import re
import pandas as pd
import random

os.chdir("/home/msalazar97/Documents/WebDevelopment/round2/")

def unpickler(name):
    with open(name, "rb") as f:
        tmp = pickle.load(f)
    return tmp

municipios = unpickler("municipiosList")
numbers = unpickler("numbersList")
latitudes = unpickler("latitudesList")
longitudes = unpickler("longitudesList")

def remove_bad_coord(l1, l2, mun, num):
    #find corrupt indeces
    corrupt = []
    for index, item in enumerate(l1):
        if (item == 0.0):
            corrupt.append(index)
    for index, item in enumerate(l2):
        if (item == 0.0):
            corrupt.append(index)
    l1 = ['{:.7f}'.format(item) for item in l1]
    l2 = ['{:.7f}'.format(item) for item in l2]
    corrupt = set(corrupt)
    corrupt = list(corrupt)
    #remove corresponding indeces in latitudes
    for index, item in enumerate(corrupt):
        del l1[index]
    #remove corresponding indeces in longitudes
    for index, item in enumerate(corrupt):
        del l2[index]
    #remove corresponding indeces in municipios
    for index, item in enumerate(corrupt):
        del mun[index]
    #remove corresponding indeces in numbers
    for index, item in enumerate(corrupt):
        del num[index]
    return mun, num, l1, l2

def trim(mun, num, lat, lon):
    random_numbers = random.sample(range(0, 155), 145)
    for number in random_numbers:
        mun[number] = ''
        num[number] = 0.0
        lat[number] = 0.0
        lon[number] = 0.0
    mun = list(filter(None, mun))
    print(mun)
    num = [item for item in num if item != 0.0]
    lat = [item for item in lat if item != 0.0]
    lon = [item for item in lon if item != 0.0]
    return mun, num, lat, lon

def create_df(string1, string2, list1, list2):
    df = {string1:[],string2:[]}
    df = pd.DataFrame(data = df)
    df[string1] = list1
    df[string2] = list2
    return df

###############################################################
#clean lists
municipios, numbers, latitudes, longitudes = remove_bad_coord(latitudes, longitudes, municipios, numbers)
#trim
municipios, numbers, latitudes, longitudes = trim(municipios, numbers, latitudes, longitudes)

#Unsure if this is the right output
df = create_df("lat", "lng", latitudes, longitudes)
pueblaCoordinates = df.to_json(orient = "records", default_handler=None)
pueblaCoordinates = re.sub(r'"\[.*\]"', '\[.*\]', pueblaCoordinates)
pueblaCoordinates = re.sub(r'\\', '', pueblaCoordinates)

with open("PueblaData.json","w") as f:
    json.dump(pueblaCoordinates, f)