# Path: lib/sitkaAPI.py
# def APIGet(url, absolute=False):
#     """
#     GET Wrapper around APIGet
#     :param url:
#     :param absolute:
#     :return:
#     """
#     log = Logger("APIGet")
#     #log.info("Making Call: GET {}".format(url))
#     return APICall(url, absolute, method=requests.get)
# 
# Path: lib/env.py
# def setEnvFromFile(batFilePath, verbose = False):
# 
# Path: lib/loghelper.py
# class Logger():
#     """
#     Think of this class like a light interface
#     """
# 
#     def __init__(self, method=""):
#         self.instance = _LoggerSingleton()
#         self.method = method
# 
#     def setup(self, **kwargs):
#         self.instance.setup(**kwargs)
# 
#     def print_(self, message, **kwargs):
#         self.instance.logprint(message, **kwargs)
# 
#     def debug(self, *args):
#         """
#         This works a little differently. You can basically throw anything you want into it.
#         :param message:
#         :return:
#         """
#         msgarr =  []
#         for arg in args:
#             if type(arg) is str:
#                 msgarr.append(arg)
#             else:
#                 msgarr.append(pformat(arg))
#         finalmessage = '\n'.join(msgarr).replace('\n', '\n              ')
#         self.instance.logprint(finalmessage, self.method, "debug")
# 
#     def destroy(self):
#         self.instance = None
#         self.method = None
# 
#     def info(self, message):
#         self.instance.logprint(message, self.method, "info")
# 
#     def error(self, message, exception=None):
#         self.instance.logprint(message, self.method, "error", exception)
# 
#     def warning(self, message, exception=None):
#         self.instance.logprint(message, self.method, "warning", exception)


import os
import sys, traceback
import argparse
import sqlite3
import json
from lib.sitkaAPI import APIGet
from lib import env
from lib.loghelper import Logger
from datetime import datetime

        try:
            visit_data = APIGet('visits/{}'.format(visitID))

            # Write visit information to json file
            with open(os.path.join(visit_path, 'visit_info.json'), 'w') as json_file:
                json.dump(visit_data, json_file)

            # Loop over the two lists of folders per visit: field folders and visit folders
            for api_key, local_folder in {'fieldFolders': 'Field Folders', 'folders': 'Visit Folders'}.items():

                if api_key in visit_data and isinstance(visit_data[api_key], list):
                    for folder_name in visit_data[api_key]:
                        field_folder_path = os.path.join(visit_path, local_folder, folder_name['name'])
                        field_folder_data = APIGet(folder_name['url'], True)

                        if isinstance(field_folder_data, dict) and 'files' in field_folder_data:
                            [download_file(file_dict, field_folder_path) for file_dict in field_folder_data['files']]

            # Get all the miscellaneous files for the visit
            [download_file(file_dict, os.path.join(visit_path, 'Files')) for file_dict in visit_data['files']]

        except Exception as e:
            log.error('Error for visit {}: {}'.format(visitID, e))

    log.info('Process Complete')


def download_file(file_dict, folder):


# Path: lib/sitkaAPI.py
# def APIGet(url, absolute=False):
#     """
#     GET Wrapper around APIGet
#     :param url:
#     :param absolute:
#     :return:
#     """
#     log = Logger("APIGet")
#     #log.info("Making Call: GET {}".format(url))
#     return APICall(url, absolute, method=requests.get)
# 
# Path: lib/env.py
# def setEnvFromFile(batFilePath, verbose = False):
# 
# Path: lib/loghelper.py
# class Logger():
#     """
#     Think of this class like a light interface
#     """
# 
#     def __init__(self, method=""):
#         self.instance = _LoggerSingleton()
#         self.method = method
# 
#     def setup(self, **kwargs):
#         self.instance.setup(**kwargs)
# 
#     def print_(self, message, **kwargs):
#         self.instance.logprint(message, **kwargs)
# 
#     def debug(self, *args):
#         """
#         This works a little differently. You can basically throw anything you want into it.
#         :param message:
#         :return:
#         """
#         msgarr =  []
#         for arg in args:
#             if type(arg) is str:
#                 msgarr.append(arg)
#             else:
#                 msgarr.append(pformat(arg))
#         finalmessage = '\n'.join(msgarr).replace('\n', '\n              ')
#         self.instance.logprint(finalmessage, self.method, "debug")
# 
#     def destroy(self):
#         self.instance = None
#         self.method = None
# 
#     def info(self, message):
#         self.instance.logprint(message, self.method, "info")
# 
#     def error(self, message, exception=None):
#         self.instance.logprint(message, self.method, "error", exception)
# 
#     def warning(self, message, exception=None):
#         self.instance.logprint(message, self.method, "warning", exception)


# Path: scripts/download_champ_data.py
import os
import sys, traceback
import argparse
import sqlite3
import json
from lib.sitkaAPI import APIGet
from lib import env
from lib.loghelper import Logger
from datetime import datetime


        try:
            visit_data = APIGet('visits/{}'.format(visitID))

            # Write visit information to json file
            with open(os.path.join(visit_path, 'visit_info.json'), 'w') as json_file:
                json.dump(visit_data, json_file)

            # Loop over the two lists of folders per visit: field folders and visit folders
            for api_key, local_folder in {'fieldFolders': 'Field Folders', 'folders': 'Visit Folders'}.items():

                if api_key in visit_data and isinstance(visit_data[api_key], list):
                    for folder_name in visit_data[api_key]:
                        field_folder_path = os.path.join(visit_path, local_folder, folder_name['name'])
                        field_folder_data = APIGet(folder_name['url'], True)

                        if isinstance(field_folder_data, dict) and 'files' in field_folder_data:
                            [download_file(file_dict, field_folder_path) for file_dict in field_folder_data['files']]

            # Get all the miscellaneous files for the visit
            [download_file(file_dict, os.path.join(visit_path, 'Files')) for file_dict in visit_data['files']]

        except Exception as e:
            log.error('Error for visit {}: {}'.format(visitID, e))

    log.info('Process Complete')


def download_file(file_dict, folder):


    log = Logger('Download')