#!/usr/bin/env python3
# system modules
import argparse
import textwrap
import logging
import os
import json
import sys

# internal modules
import patatmo
from patatmo.utils import *
from patatmo.cli import *
from patatmo.api.client import NetatmoClient
from patatmo.api.authentication import Authentication

# external modules

# Options
parser = cli_argumentparser( 
    description = textwrap.dedent("""
        Issue a Getpublicdata request to the Netatmo server and write the
        results to file.
        """)
    )

# Getpublicdata arguments
parser.add_argument("--lat_ne", required = True,
    help = "North-east latitude in degrees [-85;85]")
parser.add_argument("--lon_ne", required = True,
    help = "North-east longitude [-180;180]")
parser.add_argument("--lat_sw", required = True,
    help = "South-west latitude [-85;85]")
parser.add_argument("--lon_sw", required = True,
    help = "South-west longitude [-180;180]")
parser.add_argument("--required_data", help = "Some kind of data filter...", 
    default = None)
parser.add_argument("--filter", help = "exclude stations with unusual data? " 
    "Defaults to False.", default = False)
parser.add_argument("-r","--raw", help = "Return the raw API output.", 
    action="store_true", default = False)

# parse the arguments
args = parser.parse_args()

# set up logging
logging.basicConfig(level = logging.DEBUG if args.verbose else logging.WARNING)
# get a client
client = get_client(args)
# get an output file
output_file = get_output_file(args)

# issue the Getpublicdata request
logging.info("Issuing Getpublicdata request...")
response = client.Getpublicdata( 
    region = {key:float(getattr(args,key)) \
        for key in ["lat_ne","lon_ne","lat_sw","lon_sw"]},
    required_data = args.required_data,
    filter = args.filter,
    )
logging.info("Getpublicdata request completed!")

if args.raw:
    logging.info("Using raw API response as output")
    output_string = json.dumps(response.response,sort_keys=True,indent=4)
else:
    # convert response
    logging.info("Converting Getpublicdata response...")
    dataframe = response.dataframe()
    logging.info("Getpublicdata response converted!")

    output_string = dataframe.to_csv()

# output
logging.info("Writing to output file '{}'...".format(output_file.name))
output_file.write(output_string)
logging.info("Written to output file!")
