#!python
import logging

import sdap_ingest_manager.sdap_ingest_manager.util
from sdap_ingest_manager.sdap_ingest_manager.util import full_path
from sdap_ingest_manager import sdap_ingest_manager

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


config = sdap_ingest_manager.read_local_configuration()


def collection_row_callback(row):

    sdap_ingest_manager.collection_row_callback(row,
                                                full_path(config.get("OPTIONS", "collection_config_template")),
                                                full_path(config.get("LOCAL_PATHS", "granule_file_list_path")),
                                                full_path(config.get("LOCAL_PATHS", "collection_config_path")),
                                                full_path(config.get("LOCAL_PATHS", "history_path")),
                                                deconstruct_nfs=True,
                                                job_deployment_template=full_path(config.get("INGEST",
                                                                                             "job_deployment_template")),
                                                connection_settings=full_path(config.get("INGEST",
                                                                                         "connection_config")),
                                                profiles=config.get("INGEST", "connection_profile").split(','),
                                                namespace=config.get("INGEST", "kubernetes_namespace"),
                                                dry_run=config.getboolean("OPTIONS", "dry_run"))


if config.has_option("COLLECTIONS_YAML_CONFIG", "yaml_file"):
    sdap_ingest_manager.read_yaml_collection_config(full_path(config.get('COLLECTIONS_YAML_CONFIG', 'yaml_file')),
                                                    collection_row_callback)
elif config.has_option("COLLECTIONS_GOOGLE_SPREADSHEET", "spreadsheet_id"):
    sdap_ingest_manager.google_spreadsheet_collection_config.read_google_spreadsheet(config.get('COLLECTIONS_GOOGLE_SPREADSHEET', 'scope'),
                                                                                     config.get("COLLECTIONS_GOOGLE_SPREADSHEET", "spreadsheet_id"),
                                                                                     config.get('COLLECTIONS_GOOGLE_SPREADSHEET', 'sheet_name'),
                                                                                     config.get("COLLECTIONS_GOOGLE_SPREADSHEET", "cell_range"),
                                                                                     collection_row_callback)
else:
    logger.error("no collection configuration found, nothing done")