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


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

    if mode == 'all':
        bdate = date(1979, 1, 1) #date(1960, 1, 1) # date before record starts
        bdate = date(2003, 1, 1) #date(1960, 1, 1) # date before record starts
        edate = date.today()

        os.makedirs('{}/NAPMD/original_files/{}/'.format(dl_root, version), exist_ok=True)
        download_location = dl_root+'/NAPMD/original_files/'+version+'/{}/{}/{}_{}.csv'

    elif mode == 'nrt':
        print("No nrt NAPMD")
        exit()

    else:
        print('time mode inapplicable')


    # create date array, daily; format to YYYYMMDD
    years = pd.date_range(bdate, edate, freq='Y').strftime('%Y').tolist()
    temporal_resolutions = {'daily': 'ap_monitor_data_daily_master', 'hourly': 'ap_monitor_data_master'} # define correct tables
    components = {'pm10': 'PM10', 'pm25': 'PM2.5', 'co': 'CO', 'o3': 'O3', 'nh3': 'NH3', 'no': 'NO', 'no2': 'NO2', 'so2': 'SO2', 'benzene': 'BENZENE', 'methane': 'CH4', 'toluene': 'TOLUENE', 'xylenes': 'XYLENE', 'p-xylen': 'P-XYLENE', 'pb': 'Pb'}
    print(years)

    # set up SQL server connection
    driver='PostgreSQL'
    cnxn = pyodbc.connect('DRIVER={};SERVER=swish5.tern.org.au;DATABASE=postgis_car;UID=raphael_grodofzig;PWD=215_melange_macrotis@'.format(driver))
    cursor = cnxn.cursor() # do I need?
    # table_names = [x[2] for x in cursor.tables(tableType='TABLE')]
    # print(table_names)  # ['customer', 'invoice', ...]

    # download
    for temporal_res in temporal_resolutions:
        for year in years:
            os.makedirs('{}/NAPMD/original_files/{}/{}/{}/'.format(dl_root, version, temporal_res, year), exist_ok=True)

            for component in components:

                #cursor.execute("Select * from air_pollution_monitors.ap_monitor_data_master where year = 2020 and variable = 'no2'")
                data = pd.read_sql("Select * from air_pollution_monitors.{} where year={} and variable='{}'".format(temporal_resolutions[temporal_res], year, component), cnxn) # selection from master table
                if not data.empty:
                    data.to_csv(download_location.format(temporal_res, year, components[component], year), index=False)
                    print('Downloaded {} {} in {}'.format(temporal_res, components[component], year))
                

    cnxn.close()


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

    network = os.path.basename(__file__).split('_downloa')[0]
    identifier = meta_params['identifier']
    today = date.today()
    
    # set up SQL server connection
    driver='PostgreSQL'
    cnxn = pyodbc.connect('DRIVER={};SERVER=swish5.tern.org.au;DATABASE=postgis_car;UID=raphael_grodofzig;PWD=215_melange_macrotis@'.format(driver))
    cursor = cnxn.cursor() # do I need?

    # download
    os.makedirs('{}/NAPMD/metadata/{}/network_provided/'.format(dl_root, version), exist_ok=True)
    download_location = '{}/NAPMD/metadata/{}/network_provided/NAPMD_META_{}.csv'.format(dl_root, version, today.strftime('%Y%m%d'))

    #cursor.execute("Select * from air_pollution_monitors.ap_monitor_data_master where year = 2020 and variable = 'no2'")
    data = pd.read_sql("Select * from air_pollution_monitors.ap_monitor_locations_master", cnxn) # selection from master table
    if not data.empty:
        data.to_csv(download_location, index=False)
        print('Downloaded metadata')
    
    cnxn.close()

    # 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)

