import requests
import time
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 = 'https://thredds.nilu.no/thredds/fileServer/ebas_doi/{}/{}/{}/{}.nc'

    if mode == 'all':
        bdate = date(2013, 12, 1)
        edate = date.today() 

        os.makedirs('{}/EBAS/original_files/{}/'.format(dl_root,version), exist_ok=True)
        download_location = dl_root+'/EBAS/original_files/'+version+'/{}.nc'

    elif mode == 'nrt':
        bdate = start
        edate = stop
        download_location = dl_root+'/EBAS/original_files/nrt/{}.nc'

    else:
        print('time mode inapplicable')


    # import DOIs of datasets
    dois = pd.read_csv('EBAS_doilist.txt', header=None, names=["DOI"])

    # download
    for index, doi in dois.iterrows():
        doi_string = doi.loc["DOI"][-9:]
        url = base_url.format(doi_string[0:2], doi_string[2:4], doi_string[5:7], doi_string)
        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(doi_string), 'wb') as file:
                    file.write(r.content)
                #urllib.request.urlretrieve(url, download_location.format(doi_string))
                print('Downloaded DOI {}'.format(doi_string))
                errcode = r.status_code
            elif r.status_code == 404:
                print("No data found, error 404")
                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 ** 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)