from selenium import webdriver
from datetime import date
import os.path
import time
import json
import csv
import metadata_parsing_functions

from chromedriver_py import binary_path
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC



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

    baseurl = 'https://nadp.slh.wisc.edu/networks/mercury-deposition-network/'

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

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

    else:
        print('time mode inapplicable')


    # set up driver
    options = Options()
    prefs = {'download.default_directory' : download_location}
    options.add_experimental_option('prefs', prefs)
    options.add_argument("--no-sandbox")
    options.add_argument("--headless")

    svc = webdriver.ChromeService(executable_path=binary_path)
    driver = webdriver.Chrome(service=svc, options=options)
    n_tries = 0
    errcode = 999

    while (n_tries < n_max_tries) and (errcode != 200):
        try:
            # open url
            driver.get(baseurl)
            WebDriverWait(driver, max_time_per_dl).until(EC.presence_of_all_elements_located((By.ID, 'data-type'))) # wait till loaded

            dropdown_element = driver.find_element(By.ID, 'data-type')
            select = Select(dropdown_element)
            options = [opt.get_attribute("text") for opt in select.options]
            select.select_by_visible_text("Weekly")
            time.sleep(max_time_per_dl)

            dropdown_element = driver.find_element(By.ID, 'sites-list')
            select = Select(dropdown_element)
            options = [opt.get_attribute("text") for opt in select.options]
            select.select_by_visible_text("All Sites")
            time.sleep(max_time_per_dl)

            invalid_box = driver.find_element(By.ID, 'invalid')
            invalid_box.click()
            time.sleep(max_time_per_dl)

            # download
            driver.find_element(By.ID, 'generate-button-text').click()

            # wait until download finished
            while not os.path.exists("{}MDN-ALL-W-i.csv".format(download_location)):
                time.sleep(1)

                if os.path.isfile("{}MDN-ALL-W-i.csv".format(download_location)):
                    print('MDN-ALL-W-i.csv download successful')
                    errcode = 200
                    continue
        
        except Exception as e:
            print(e)
            n_tries = n_tries+1
            print("Number of tries: {}".format(n_tries))
            continue
        
    driver.close()

    if n_tries == n_max_tries:
        print('Failed downloading US_NADP_MDN data {} times in {} seconds'.format(n_tries, max_time_per_dl))

    os.rename("{}MDN-ALL-W-i.csv".format(download_location), "{}/US_NADP_MDN/original_files/{}/MDN-ALL-W-i.csv".format(dl_root,version))




def download_metadata(n_max_tries, max_time_per_dl, version, dl_root, meta_basefile, meta_history, meta_params):

    baseurl = 'https://nadp.slh.wisc.edu/networks/mercury-deposition-network/'
    os.makedirs('{}/US_NADP_MDN/metadata/{}/network_provided/'.format(dl_root,version), exist_ok=True)
    os.makedirs('{}/US_NADP_MDN/metadata/{}/processed/'.format(dl_root,version), exist_ok=True)

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

    # set up driver
    options = Options()
    prefs = {'download.default_directory' : download_location}
    options.add_experimental_option('prefs', prefs)
    options.add_argument("--no-sandbox")
    options.add_argument("--headless")

    n_tries = 0
    errcode = 999
    svc = webdriver.ChromeService(executable_path=binary_path)
    driver = webdriver.Chrome(service=svc, options=options)
    driver.maximize_window()

    while (n_tries < n_max_tries) and (errcode != 200):
        try:
            # open url
            driver.get(baseurl)

            #WebDriverWait(driver, max_time_per_dl).until(EC.element_to_be_clickable((By.ID, 'invalid'))) # wait till loaded
            time.sleep(max_time_per_dl)
            invalid_box = driver.find_element(By.ID, 'download-show-inactive')
            driver.execute_script("arguments[0].click()", invalid_box)
            
            # download
            #WebDriverWait(driver, max_time_per_dl).until(EC.presence_of_all_elements_located((By.ID, 'generate-button-text'))) # wait till loaded
            time.sleep(max_time_per_dl)
            bttn = driver.find_element(By.ID, 'network-data-submit')
            driver.execute_script("arguments[0].click()", bttn)


            # wait until download finished
            while not os.path.exists(download_location+'mdn.csv'):
                time.sleep(1)

                if os.path.isfile(download_location+'mdn.csv'):
                    print('MDN metadata download successful')
                    errcode = 200
                    continue
        
        except Exception as e:
            print(e)
            max_time_per_dl = max_time_per_dl*2 # set waiting time to double
            n_tries = n_tries+1
            continue

    if n_tries == n_max_tries:
        print('Failed downloading MDN metadata {} times in {} seconds'.format(n_tries, max_time_per_dl))

    driver.close()

    os.rename(download_location+'mdn.csv', download_location+'US_NADP_MDN_META_{}.csv'.format(today.strftime('%Y%m%d'))) 

   # JSON =====================================================================
    network = os.path.basename(__file__).split('_downloa')[0]
    present_meta_file_id = today.strftime('%Y%m%d')

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