#!/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 json
import configparser
from dqc_wrapper.utils.utils_pdf import *
from dqc_wrapper.utils.utils_files import *


'''
Read a configparser object
'''


def read_conf_file(res_file):
    config = configparser.RawConfigParser()
    config.read(res_file)
    return config


'''
Get latest log file
'''


def get_log_file(log_folder, res_file):
    # Get latest available log file
    res = None
    log_files = sorted(
        glob.glob(os.path.join(log_folder, 'LOG_{}*.txt'.format(os.path.basename(res_file)))),
        reverse=True,
    )
    if len(log_files) > 0:
        res = log_files[0]
    return res

'''
INI replaces a whole section in the ini result file located in a given dir to PDF
Intended to patch the check refered to temporal completeness that for hourly and daily data
is not possible to do it for the moment
'''


def iniUpdateSection(ini_result_file, ini_section_name, output_dir):
    # Configuration object
    cf = configparser.RawConfigParser()
    cf.read(ini_result_file)
    out_file = ini_result_file

    for s in cf.sections():
        # Remove log (unnecessary for CMS)
        if ini_section_name == s:
            # Basic assumption, time granularity is defined
            if cf['temporal_consistency'] is not None:
                time_granularity = cf['temporal_consistency']['time_granularity'].strip()
                print("INFO: time granularity {}".format(time_granularity))
                if time_granularity  == 'hour' or time_granularity == 'day':
                    print("INFO: Updating section {}".format(ini_section_name))
                    cf.set(ini_section_name,"res", "Not available yet")
                    if 'msg' in cf[ini_section_name]:
                        cf[ini_section_name].pop('msg')
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)
                    out_file = save_result(cf, output_dir, os.path.basename(ini_result_file))
            else:
                print("WARNING: time granularity not defined in file {} - SKIPPING".format(ini_result_file))


    return out_file

'''
INI format to PDF
'''


def ini2pdf(res_file, log_file):
    # Zip
    zipfname = res_file.replace('.ini', '.zip')
    completeness_done = False
    files2zip = []



    # Res file
    config = read_conf_file(res_file)

    # Save PDF report (result + log)
    # TODO: zip file is generated corrupted!!!!!!!!
    if os.path.exists(log_file):
        for s in config.sections():
            if not ('result' in s):
                # Spatial and temporal completeness go together for C3S
                if 'completeness' in s:
                    if not (completeness_done):
                        files2zip.append(save_pdf_report('completeness', res_file, log_file,
                                                         suffix='spatial_temporal_completeness'))
                        completeness_done = True
                pdf_file = save_pdf_report(s, res_file, log_file)
                files2zip.append(pdf_file)
                if 'valid_ranges' in pdf_file:
                     # Copy original plot, since these cannot be regenerate
                     html_file = "{}/{}".format(res_folder, os.path.basename(pdf_file).replace('.pdf', '.html'))
                     os.system('cp {} {}'.format(html_file, output_folder_regenerated))
                     if os.path.exists(pdf_file.replace('.pdf', '.html')):
                       files2zip.append(pdf_file.replace('.pdf', '.html'))
    else:
        print('ERROR: Missing log file {}'.format(log_file))
    zip_results(zipfname, files2zip)
    return pdf_file


'''
INI format to PDF
'''


def ini2pdf_justheader(res_file):
    # Res file
    config = read_conf_file(res_file)

    # Save PDF report (result + log)
    for s in config.sections():
        if not ('result' in s):
            pdf_file = save_pdf_report(s, res_file, None)

    return pdf_file


'''
Script in order to convert res and log files to separate PDFs to be imported at the CDS / CMS
'''
# Folder with results
# res_folder = 'path to where are the ini/pdf generated by the checker'
# log_folder = 'path to log folder of the generated QAR'
# just_header = 'Indicates to only generate the header without the full log'

# load script settings
this_dir = os.path.dirname(__file__)
conf_file = os.path.join(this_dir, "../conf/scripts_settings_conf.json")

# Load settings for script
with open(conf_file) as json_file:
    conf = json.load(json_file)
    res_folder = conf['dqc_res_dir']
    log_folder = conf['dqc_log_dir']
    output_folder_regenerated = conf['dqc_ini2pdf_ini_output_path']
    just_header = conf['dqc_ini2pdf_just_header']
    update_section_ini = conf['dqc_ini2pdf_update_section_ini']

print('------------------------')
print(res_folder)

print('------------------------')

nr_ini_processed = 0
nr_pdf_not_processed = 0
nr_pdf_processed = 0

for res_file in glob.glob(os.path.join(res_folder, '*.ini')):
    print('------------------------')
    print('INI: ' + res_file)
    print('------------------------')
    pdf_file=None
    file_to_process = res_file
    #check if patching is needed
    if update_section_ini == 'True':
        file_to_process = iniUpdateSection(res_file, "temporal_completeness_result", output_folder_regenerated )
        nr_ini_processed += 1

    if just_header == 'True':
        pdf_file = ini2pdf_justheader(file_to_process)
        nr_pdf_processed += 1
    else:
        log_filename = get_log_file(log_folder, file_to_process)
        if log_filename is not None:
            log_file = os.path.join(log_folder, log_filename )
            print('LOG: ' + log_file)
            pdf_file = ini2pdf(file_to_process, log_file)
            nr_pdf_processed += 1
        else:
            print('LOG not found for ' + file_to_process)
            nr_pdf_not_processed += 1

    if pdf_file is not None:
        print('PDF regenerated: ' + str(pdf_file))

if update_section_ini == 'True':
    print('------------------------')
    print('# INIs modified: ' + str(nr_ini_processed))
    print('------------------------')

print('------------------------')
print('# PDFs regenerated from ini result file: ' + str(nr_pdf_processed))
print('------------------------')

print('------------------------')
print('# PDFs skipped: ' + str(nr_pdf_not_processed ))
print('------------------------')

