# use API
import requests
import pandas as pd

import os
import sys
import logging
import urllib
import datetime as dt
import json
import csv
from pathlib import Path
from datetime import date
from datetime import timedelta
import zipfile


class eea_ereporting_api(object):
    """defines and configures API to query the eea database"""

    def __init__(self, ):
        self.logger = logging.getLogger(__file__)
        self.url_api = "https://eeadmz1-downloads-api-appservice.azurewebsites.net/"
        self.headers = {'content-type': 'application/json', 'accept': 'application/json'}

        self.parquet_files = 'ParquetFile'
        self.get_countries = 'Country'
        self.get_cities = 'City'
        self.components = 'Property'

        return

    def get_parquet_files(self, max_time_per_dl, n_max_tries, ccode, city, pcode, dataset):
        """build a query to return data in parquet files"""
        query = urllib.parse.urljoin(self.url_api, self.parquet_files)
        request_body = {'countries': [ccode], 'cities': [city], 'properties': [pcode], 'datasets':[dataset], 'source': 'Api'}
        n_tries = 0
        errcode = 999

        while (n_tries < n_max_tries) and (errcode != 200):
            response = requests.post(url=query, json=request_body, timeout=max_time_per_dl)

            if response.status_code == 200:
                return response.content
                errcode = response.status_code
            else:
                # try again
                print('Response error {}, attempt {}'.format(response.status_code, n_tries))
                errcode = response.status_code
                n_tries += 1

        if n_tries == n_max_tries:
            print('Failed downloading {}_{}_{}_{} {} times in {} seconds, error code {}'.format(ccode, city, pcode, dataset, n_tries, max_time_per_dl, errcode))
            return None
        

    def get_countrycodes_in_list(self, ):
        """build a query to return country codes"""
        query = urllib.parse.urljoin(self.url_api, self.get_countries)
        response = requests.get(url=query, data='')
        if response.status_code == 200:
            return response.text
        else:
            return print('Response error {}'.format(response.status_code))

    def get_components(self, ):
        """build a query to return components"""
        query = urllib.parse.urljoin(self.url_api, self.components)
        response = requests.get(url=query, data='')
        if response.status_code == 200:
            return response.text
        else:
            return print('Response error {}'.format(response.status_code))
        max_time_per_dl, n_max_tries

    def get_cities_in_list(self, ccode):
        """build a query to return cities"""
        query = urllib.parse.urljoin(self.url_api, self.get_cities)
        request_body = [ccode]
        response = requests.post(url=query, json=request_body)
        if response.status_code == 200:
            return response.content
        else:
            return print('Response error {}'.format(response.status_code))


def download_data(mode, version, n_max_tries, max_time_per_dl):

    if mode == 'all':
        bdate = date(2021, 1, 1)
        edate = date(2022, 12, 31)
        dataset = 2 # this means E1a: data from 2013 until 2022
        

    elif mode == 'nrt':
        version = mode
        bdate = date.today() - timedelta(days = 1)
        edate = date.today() 
        dataset = 1 # this means E2a: data from 2023-01-01 until present

        download_location = '/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/nrt/'

    else:
        print('time mode inapplicable')

    countries = json.loads(eea_ereporting_api().get_countrycodes_in_list())
    components = json.loads(eea_ereporting_api().get_components())

    for country in countries:
        ccode = country['countryCode']

        cities = json.loads(eea_ereporting_api().get_cities_in_list(ccode))
        for city in cities:
            cityname = city['cityName']

            for component in components:
                pcode = component['notation'].replace(' ', '')

                data = eea_ereporting_api().get_parquet_files(max_time_per_dl, n_max_tries, ccode, cityname, pcode, dataset)
                if data == None:
                    continue # move to next component

                os.makedirs('/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/{}/{}/'.format(version, pcode), exist_ok=True)
                download_location = '/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/{}/{}/'.format(version, pcode)
                filename = '{}_{}_{}.zip'.format(ccode, cityname, pcode)

                with open(download_location+filename, 'wb') as outfile:
                    outfile.write(data)
                print("Downloaded {}".format(filename))

                with zipfile.ZipFile(download_location+filename, 'r') as zip_ref:
                    zip_ref.extractall(download_location)
                os.remove(download_location+filename)



    #df = pd.read_parquet('/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/E2a/SP_28006004_10_49.parquet')
    #df.to_csv('/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/E2a/SP_28006004_10_49.csv')
