#!/usr/bin/env python3
# Copyright 2023 - Barcelona Supercomputing Center
# Authors: Amirpasha Mozaffari
# MIT License

import yaml
from yaml.loader import SafeLoader
import os 
import argparse


def argument_parse_namelist_amip():
    parser = argparse.ArgumentParser()
    parser.add_argument("--file_name",default="value0", type=str, help='name of the YAML file that will be created')
    parser.add_argument("--leg_length_sec",default="value1", type=str, help='TBD')
    parser.add_argument("--cpl_freq_amip_sec",default="value2", type=str, help='TBD')
    parser.add_argument("--leg_start_date_yyyy",default="value3", type=str, help='TBD')
    parser.add_argument("--leg_start_date_mm",default="value4", type=str, help='TBD')
    parser.add_argument("--leg_start_date_dd",default="value5", type=str, help='TBD')
    parser.add_argument("--ifs_cmip_fixyear",default="value6", type=str, help='TBD')
    parser.add_argument("--flist_sst",default="value7", type=str, help='TBD')
    parser.add_argument("--flist_sic",default="value8", type=str, help='TBD')
    parser.add_argument("--LDebug",default="value9", type=str, help='TBD')
    parser.add_argument("--LInterpolate",default="value10", type=str, help='TBD')
    input_argument = vars(parser.parse_args()) #convertargparseNameSpace to dict
    print(type(input_argument))
    return input_argument 


def read_yaml_file(file_name):
    current_path = os.getcwd()
    yamel_file_path = os.path.join(current_path,file_name)     
    if os.path.isfile(yamel_file_path) == False: 
        print("YAML file does not exist. Aborted")
        raise NameError("YAML file does not exist.")
        
    #Open the file and load the file 
    with open(yamel_file_path) as file:
        read_in_data = yaml.load(file, Loader=SafeLoader)
        print(read_in_data)
        file.close()
    return read_in_data


def write_namelist_amip_yaml(input_arguments):
    '''
    write down a yaml file, by using the input arguments that passed to it. 

    '''
    
    file_name = input_arguments.get("file_name")
    leg_length_sec = input_arguments.get("leg_length_sec")
    cpl_freq_amip_sec = input_arguments.get("cpl_freq_amip_sec")
    leg_start_date_yyyy = input_arguments.get("leg_start_date_yyyy")
    leg_start_date_mm = input_arguments.get("leg_start_date_mm")
    leg_start_date_dd = input_arguments.get("leg_start_date_dd")
    ifs_cmip_fixyear = input_arguments.get("ifs_cmip_fixyear")
    flist_sst= input_arguments.get("flist_sst")
    flist_sic= input_arguments.get("flist_sic")
    LDebug = input_arguments.get("LDebug")
    LInterpolate = input_arguments.get("LInterpolate") 

    # yaml file path 
    current_path = os.getcwd()
    yamel_file_path = os.path.join(current_path,file_name)


    # remove if the YAML file exist 
    if os.path.isfile(yamel_file_path) == True:
        os.remove(yamel_file_path)
        print("file exists, older file is deleted.")

    yaml_write_doc = [{'RunLengthSec' : [leg_length_sec]},
                      {'TimeStepSec' : [cpl_freq_amip_sec]},
                      {'StartYear' : [leg_start_date_yyyy]},
                      {'StartMonth' : [leg_start_date_mm]},
                      {'StartDay' : [leg_start_date_dd]},
                      {'FixYear' : [ifs_cmip_fixyear]},
                      {'FileListSST' : [flist_sst]},
                      {'FileListSIC' : [flist_sic]},
                      {'LDebug' : [LDebug]},
                      {'LInterpolate' : [LInterpolate]}]

    with open(yamel_file_path, 'w') as file:
        file.write('# This is an input namelist file for DataCoupler tool\n')
        documents = yaml.dump(yaml_write_doc, file, width=50, indent=1)
        file.close
    
    return file_name 

if __name__ == '__main__':

    input_arguments = argument_parse_namelist_amip()
    file_name = write_namelist_amip_yaml(input_arguments)
    read_yaml_file(file_name)
    
    print("Run finished")
 
