import requests
import time
import pandas as pd
from datetime import date
from datetime import timedelta
import os.path
import json
import metadata_parsing_functions


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

    base_url = 'https://quotsoft.net/air/data'

    if mode == 'all':
        bdate = date(2014, 5, 1) # date before record starts
        edate = date.today()

        os.makedirs('{}/CNEMC/original_files/{}/sites/'.format(dl_root, version), exist_ok=True)
        os.makedirs('{}/CNEMC/original_files/{}/cities/'.format(dl_root, version), exist_ok=True)

        download_location = dl_root+'/CNEMC/original_files/'+version+'/{}/{}.csv'

    elif mode == 'nrt':
        bdate = start # if code is run after 2 am, data from previous day will be available
        edate = stop
        os.makedirs('{}/CNEMC/original_files/nrt/sites/'.format(dl_root), exist_ok=True)
        os.makedirs('{}/CNEMC/original_files/nrt/cities/'.format(dl_root), exist_ok=True)

        download_location = dl_root+'/CNEMC/original_files/nrt/{}/{}.csv'

    else:
        print('time mode inapplicable')


    # create date array, daily; format to YYYYMMDD
    Headers = {'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'}
    dates = pd.date_range(bdate, edate, freq='d').strftime('%Y%m%d').tolist()
    print(dates)

    # download
    for day in dates:

        url = '{}/china_sites_{}.csv'.format(base_url, day)
        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.format('sites', day), 'wb') as file:
                    file.write(r.content)
                #urllib.request.urlretrieve(url, download_location.format('sites', day))
                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))


        url = '{}/china_cities_{}.csv'.format(base_url, day)
        n_tries = 0
        errcode = 999

        while (n_tries < n_max_tries) and (errcode != 200):
            r = requests.get(url, timeout=max_time_per_dl)
            if r.status_code == 200:
                with open(download_location.format('cities', day), 'wb') as file:
                    file.write(r.content)
                #urllib.request.urlretrieve(url, download_location.format('cities', day))
                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))

        time.sleep(1)

        """
        # reorganize files per component
        components = ['AQI', 'PM2.5', 'PM2.5_24h', 'PM10', 'PM10_24h','SO2', 'SO2_24h', 'NO2', 'NO2_24h','O3', 'O3_24h', 'O3_8h', 'O3_8h_24h', 'CO', 'CO_24h']

        if mode == "nrt":
            for component in components:
                # sites
                og_file = pd.read_csv(download_location.format('sites', day))
                comp = og_file[og_file["type"] == component]

                output_path = download_location.format('sites', component+"_{}".format(dates[0][0:4]))
                comp.to_csv(output_path, mode='a', header=not os.path.exists(output_path), index=False)

                # check if there is data doubled
                new_file = pd.read_csv(output_path).drop_duplicates()
                new_file.to_csv(output_path, index=False)


                # cities
                og_file = pd.read_csv(download_location.format('cities', day))
                comp = og_file[og_file["type"] == component]

                output_path = download_location.format('cities', component+"_{}".format(dates[0][0:4]))
                comp.to_csv(output_path, mode='a', header=not os.path.exists(output_path), index=False)

                # check if there is data doubled
                new_file = pd.read_csv(output_path).drop_duplicates()
                new_file.to_csv(output_path, index=False)


            # delete original files
            os.remove(download_location.format('sites', day))
            os.remove(download_location.format('cities', day))"""



def download_metadata(n_max_tries, max_time_per_dl, version, dl_root, meta_basefile, meta_history, meta_params):
    # download not possible due to login issue. Use metadata from GHOST 1.5 to parse jsons
    
    # JSON =====================================================================
    network = os.path.basename(__file__).split('_downloa')[0]
    present_meta_file_id = '2022'

    metadata_parsing_functions.json_parser(dl_root, network, version, meta_basefile, meta_history, meta_params, present_meta_file_id)