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


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

    base_url_aod_sda = 'https://aeronet.gsfc.nasa.gov/cgi-bin/print_web_data_v3?year={}&month={}&day={}&year2={}&month2={}&day2={}&{}{}=1&AVG=10'
    base_url_alm = 'https://aeronet.gsfc.nasa.gov/cgi-bin/print_web_data_inv_v3?year={}&month={}&day={}&year2={}&month2={}&day2={}&product=ALL&AVG=10&{}{}=1'
    levels = {'20': '2.0'}
    data_types = {'ALM': "almucantar"} #{'AOD': 'directsun', 'SDA': 'oneill', 'ALM': "almucantar"}

    if mode == 'all':
        bdate = date(2015, 10, 1) # start at 1993-1-1
        edate = date.today()

    elif mode == 'nrt':
        version = mode
        bdate = start
        edate = stop

    else:
        print('time mode inapplicable')


    # create date array, daily; format YYYYMMDD
    dates = pd.date_range(bdate, edate, freq='d')

    # download
    for level in levels:

        for data_type in data_types:
            os.makedirs('{}/AERONET_v3_lev{}/original_files/{}/{}/'.format(dl_root, levels[level], version, data_types[data_type]), exist_ok=True)
            download_location = '{}/AERONET_v3_lev{}/original_files/{}/{}/'.format(dl_root, levels[level], version, data_types[data_type])

            for day in dates:

                if data_type == 'ALM':
                    base_url = base_url_alm
                else:
                    base_url = base_url_aod_sda
                
                url = base_url.format(day.year, day.month, day.day, (day+timedelta(days=1)).year, (day+timedelta(days=1)).month, (day+timedelta(days=1)).day, data_type, level)
                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:
                        urllib.request.urlretrieve(url, download_location+day.strftime('%Y%m%d')+'.html')
                        print('Downloaded {}'.format(url))
                        errcode = r.status_code

                    elif r.status_code == 404:
                        print('No {}'.format(url))
                        errcode = 200

                    else:
                        # try again
                        print('Response error {}, attempt {}'.format(r.status_code, n_tries))
                        errcode = r.status_code
                        n_tries += 1
                        time.sleep(n_tries * 3) # 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))