#!/usr/bin/env python
#
# Data Quality Checker also known as DQC
#
# Read the README.md for more detailed information
#
#       Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
#       Earth Sciences Department - Computational Earth Sciences
#
#     Authors:
#       Joan Sala Calero (joan.sala@bsc.es)

import os, sys
from pathlib import Path

from dqc_wrapper.utils.utils_db import *
from dqc_downloader.utils_download import *
from dqc_wrapper.utils.utils_files import *

"""
ECFLOW workflow source

[ ] Task 0 - Init/Create database, insert c3s datasets
[ ] Task 1 - Sync cds-c3s-forms
[*] Task 2 - Download GRIB files + update database
[ ] Task 3 - DQC checks + update database
[ ] Task 4 - PDF generation
[ ] Task 5 - Upload PDFs to drupal
[ ] Task 6 - Generate overview HTMLs
[ ] Task 7 - Delete and free up space
"""
def main(args):

    # Check parameters
    if len(args) != 2:
        print('Usage: python download_workflow.py <config_file> <cred_file>')
        return -1
    else:
        try:
            # General configuration
            with open(args[0]) as json_file:
                conf = json.load(json_file)
                dqc_conf_dir = conf['dqc_conf_dir']
                dqc_db_file = conf['dqc_db_file']
                dqc_down_root = conf['outdir']
                logdir = conf['logdir']
                formsdir = Path(conf['formsdir'])
            with open(args[1]) as cred_file:
                cred = json.load(cred_file)
        except Exception as e:
            print('ERROR: while reading the config file provided, {}'.format(e))
            exit(-1)

    # Create connection to SQLITE file
    try:
        conn = create_connection(dqc_db_file)
    except:
        print('ERROR: Could not connect to database')
        exit(-1)

    # LOOP 1: All datasets registered in the database
    for d in select_datasets(conn):
        dataset_id, dataset_group = d

        # Read configuration and check mandatory fields in the general section
        conf_path = os.path.join(dqc_conf_dir, dataset_id + '.ini')
        conf = CheckerConfig(conf_path)
        conf.read_config(execute=False)

        # Get constraints from c3s-cds-forms
        constraints = get_constraints(conf.cds_dataset, formsdir, "constraints")

        # Select remaining downloads
        for dwn in select_required_downloads(conn, d[0]):

            # Perform download
            outfile = os.path.join(dqc_down_root, '{}/{}/{}/{}'.format(dataset_group, conf.cds_dataset, conf.cds_variable, dwn[0]+'.grib'))
            info = generate_download_obj(constraints, conf, dwn[0])
            call_download(cred, outfile, info, conf.cds_dataset, logdir)

            # If file exists update db (download went well)
            if os.path.exists(outfile):
                update_downperformed(conn, 1, dataset_id, dwn[0])
                update_filedatetimes(conn, dataset_id, [outfile])

if __name__ == "__main__":
    args = sys.argv[1:]
    sys.exit(main(args))