#!/usr/bin/env python
#
# grib_checker.py checks Checks whatever you want to check in your life
#
# AUTHOR: BSC-CNS I. CERNICHARO
#

import sys
import json

import msg_content

def check_metada_keys(msg):
    keys = msg_content.return_metadata(msg)

    # Metadata
    with open("structure_files/grib_keys.json","r") as j_doc:
        grib_meta = json.load(j_doc)
    j_doc.close()

    if "GRIBEditionNumber" in keys:
        ge = msg["GRIBEditionNumber"]
        grib_meta_man = grib_meta["gribEdition"][str(ge)]["mandatory"]
        #grib_meta_opt = grib_meta["gribEdition"][str(ge)]["optional"]

        counter = 0
        no_available = list()
        for item in grib_meta_man:
            if item not in keys:
                counter += 1
                no_available.append(item)
        return ["OK"] if counter == 0 else ["ERROR",no_available]

    else: return ["ERROR","GRIBEditionNumber"]

def check_grid_keys(msg):
    keys = msg_content.return_metadata(msg)
    
    # Grid Information Metadata
    with open("structure_files/grid_types_geography.json","r") as j_doc:
        grid_data = json.load(j_doc)
    j_doc.close()

    if "gridType" in keys:
        gt = msg["gridType"]

        if gt in grid_data.keys():
            msg_grid_keys = grid_data[gt]

            counter = 0
            no_available = list()
            for item in msg_grid_keys:
                if item not in keys:
                    counter += 1
                    no_available.append(item)
            return ["OK"] if counter == 0 else ["ERROR",no_available]

    else: return ["ERROR",["gridType"]]