import requests
from datetime import date
import os.path
import time
import json
import csv



def download_data(mode, version, n_max_tries, max_time_per_dl, start, stop, dl_root):

    baseurl = 'https://nadp.slh.wisc.edu/networks/national-trends-network/'

    if mode == 'all':
        os.makedirs('{}/US_NADP_AIRMoN/original_files/{}/'.format(dl_root, version), exist_ok=True)
        download_location = '{}/US_NADP_AIRMoN/original_files/{}/'.format(dl_root,version)

    elif mode == 'nrt':
        os.makedirs('{}/US_NADP_AIRMoN/original_files/nrt/'.format(dl_root), exist_ok=True)
        download_location = '{}/US_NADP_AIRMoN/original_files/nrt/'.format(dl_root)

    else:
        print('time mode inapplicable')

    Headers = {'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'}
    url = 'https://nadp.slh.wisc.edu/wp-content/uploads/2021/09/AIRMoN-ALL.csv'
    n_tries = 0
    errcode = 999

    while (n_tries < n_max_tries) and (errcode != 200):
        r = requests.get(url, timeout=max_time_per_dl, headers=Headers)
        if r.status_code == 200:
            with open(download_location+'AIRMoN-ALL.csv', 'wb') as file:
                file.write(r.content)
            print('Downloaded {}'.format(url))
            errcode = r.status_code

        elif r.status_code == 404:
            print('No {}'.format(url))
            errcode = 200 #skip
        else:
            # try again
            print('Response error {}, attempt {}'.format(r.status_code, n_tries))
            errcode = r.status_code
            n_tries += 1
            time.sleep(n_tries ** 2) # wait a lil more every time 

    if n_tries == n_max_tries:
        print('Failed downloading {} {} times in {} seconds, error code {}'.format(url, n_tries, max_time_per_dl, errcode))



def download_metadata(n_max_tries, max_time_per_dl, version, dl_root):

    os.makedirs('{}/US_NADP_AIRMoN/metadata/{}/network_provided/'.format(dl_root,version), exist_ok=True)
    os.makedirs('{}/US_NADP_AIRMoN/metadata/{}/processed/'.format(dl_root,version), exist_ok=True)

    download_location = '{}/US_NADP_AIRMoN/metadata/{}/network_provided/'.format(dl_root, version)
    today = date.today()

    Headers = {'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'}
    url = 'https://nadp.slh.wisc.edu/wp-content/uploads/2021/09/AIRMoNsites.csv'
    n_tries = 0
    errcode = 999

    while (n_tries < n_max_tries) and (errcode != 200):
        r = requests.get(url, timeout=max_time_per_dl, headers=Headers)
        if r.status_code == 200:
            with open(download_location+'US_NADP_AIRMoN_META_{}.csv'.format(today.strftime('%Y%m%d')), 'wb') as file:
                file.write(r.content)
            print('Downloaded {}'.format(url))
            errcode = r.status_code

        elif r.status_code == 404:
            print('No {}'.format(url))
            errcode = 200 #skip
        else:
            # try again
            print('Response error {}, attempt {}'.format(r.status_code, n_tries))
            errcode = r.status_code
            n_tries += 1
            time.sleep(n_tries ** 2) # wait a lil more every time 

    if n_tries == n_max_tries:
        print('Failed downloading {} {} times in {} seconds, error code {}'.format(url, n_tries, max_time_per_dl, errcode))


    # create json from original metadata file =====================================================================================
    json_metadata = {}
    with open('{}/US_NADP_AIRMoN/metadata/{}/network_provided/US_NADP_AIRMoN_META.csv'.format(dl_root,version), 'r') as file:
        csv_filedata = csv.DictReader(file)

        for row in csv_filedata:  
            key = row['siteid']
            update_date = today.strftime('%Y-%m-%d')
            for parameter in row:
                row[parameter] = {'values': [row[parameter]], 'update_time': [update_date]} # create inner dictionary for every parameter
            json_metadata[key] = row

    with open('{}/US_NADP_AIRMoN/metadata/{}/processed/US_NADP_AIRMoN_META.json'.format(dl_root,version), 'w', encoding='utf-8') as f:
        f.write(json.dumps(json_metadata, indent=4))

    
    # create json in desired shape from current metadata file
    json_metadata_now = {}  
    with open(download_location+'US_NADP_AIRMoN_META_{}.csv'.format(today.strftime('%Y%m%d')), encoding='utf-8') as file:
        csv_filedata = csv.DictReader(file)

        for row in csv_filedata:  
            key = row['siteid']
            update_date = today.strftime('%Y-%m-%d')
            for parameter in row:
                row[parameter] = {'values': [row[parameter]], 'update_time': [update_date]} # create inner dictionary for every parameter
            json_metadata_now[key] = row

    
    # read standardised file to compare!
    with open('{}/US_NADP_AIRMoN/metadata/{}/processed/US_NADP_AIRMoN_META.json'.format(dl_root,version), 'r', encoding='utf-8') as f:
        json_metadata = json.loads(f.read())

        for station in json_metadata: # loop through all the old stations
            if station in json_metadata_now.keys(): # if station is in current meta data, go on
                for parameter in json_metadata[station]:
                    if json_metadata[station][parameter]['values'][-1] != json_metadata_now[station][parameter]['values'][0]: # compare last entry in standardised file to value in new file
                        # if different value, append the standardised metadeta file
                        print("old {} --- new {}".format(json_metadata[station][parameter]['values'][-1], json_metadata_now[station][parameter]['values'][0]))
                        json_metadata[station][parameter]['values'].append(json_metadata_now[station][parameter]['values'][0])
                        json_metadata[station][parameter]['update_time'].append(json_metadata_now[station][parameter]['update_time'][0])
                    else:
                        pass
            else:
                print('Station {} was abolished'.format(station))

        for station in json_metadata_now: # loop through all the new stations
            if station in json_metadata.keys(): # if station is in old meta data
                pass # comparison was done before
            else: # new station appeared!
                print('New station {}'.format(station))
                json_metadata.update({station: json_metadata_now[station]})


    # safe
    with open('{}/US_NADP_AIRMoN/metadata/{}/processed/US_NADP_AIRMoN_META.json'.format(dl_root,version), 'w', encoding='utf-8') as f:
        f.write(json.dumps(json_metadata, indent=4))

