# 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


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'

        return

    def get_parquet_files(self, code, ccode, scode, pcode, start, end):
        """build a query to return data in parquet files"""
        query = urllib.parse.urljoin(self.url_api, self.parquet_files)
        params = {'email': 'raphael.grodofzig@bsc.es', 'key': 'bluewolf34', 'param': pcode, 'bdate': start, 'edate': end, 'state': str(code), 'county': str(ccode), 'site': str(scode)}
        response = requests.get(url=query, data='', params=params)
        if response.status_code == 200:
            return response.text
        else:
            return print('Response error {}'.format(response.status_code))
        

    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_cities_in_list(self, ccode):
        """build a query to return cities"""
        query = urllib.parse.urljoin(self.url_api, self.get_cities)
        print(ccode)
        request_body = {'countries': ['ES']}
        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))
        

    """apiUrl = "https://eeadmz1-downloads-api-appservice.azurewebsites.net/"
    endpoint = "ParquetFile"
    downloadPath = "/esarchive/obs/ghost/EEA_AQ_eReporting/original_files/"
    fileName = "download_data.zip"

    # Request body
    request_body = {
    "countries":["ES"],
    "cities":["Madrid"],
    "properties":["1","7","8"],
    "datasets":[1],
    "source":"Api"
    }

    
    # A get request to the API
    downloadFile = requests.post(apiUrl+endpoint,
    json=request_body).content
    # Store in local path

    output = open(downloadPath+fileName, 'wb')
    output.write(downloadFile)"""


def scraper(mode, version):

    if mode == 'all':
        bdate = date(2021, 1, 1)
        edate = date(2022, 12, 31)

        os.makedirs('/esarchive/obs/ghost/US_EPA_AQS/original_files/{}/E1a/'.format(version), exist_ok=True)

    elif mode == 'nrt':
        version = mode
        bdate = date.today() - timedelta(days = 1)
        edate = date.today() 
        download_location = '/esarchive/obs/ghost/US_EPA_AQS/original_files/nrt/..'
    else:
        print('time mode inapplicable')

    
    apiUrl = "https://eeadmz1-downloads-api-appservice.azurewebsites.net/"
    endpoint = "ParquetFile"
    # Request body
    request_body = {"countries":["ES"],"cities":["Madrid"],"properties":["1","7","8"],"datasets":[1],"source":"Api"}
    downloadFile = requests.post(apiUrl+endpoint,json=request_body).text
    print(downloadFile)


    countries = json.loads(eea_ereporting_api().get_countrycodes_in_list())
    for country in countries:
        ccode = country['countryCode']
        print(ccode)

        cities = json.loads(eea_ereporting_api().get_cities_in_list(ccode))
        print(cities)

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

