import yaml

from deserialize_json import DeserializeJson
from serialize_json import SerializeJson
from convert_json_to_yaml import ConvertJsonToYaml

tempconffile = open('Assets/basic_config.yaml', encoding="utf8")
confdata = yaml.load(tempconffile, Loader=yaml.FullLoader)
tempconffile.close()

def deserialize():
    global newDeserializator
    newDeserializator = DeserializeJson(confdata['paths']['source_folder']+confdata['paths']['json_source_file'])
    newDeserializator.somestats()

def serialize(source):
    global newDeserializator
    if source == "object":
        SerializeJson.run(newDeserializator, confdata['paths']['source_folder']+confdata['paths']['json_destination_file'])
    elif source == "file":
        newDeserializator = DeserializeJson(confdata['paths']['source_folder']+confdata['paths']['json_source_file'])
        SerializeJson.run(newDeserializator, confdata['paths']['source_folder']+confdata['paths']['json_destination_file'])
    else:
        print("invalid source")

def convert(source):
    if source == "object":
        ConvertJsonToYaml.run(newDeserializator, confdata['paths']['source_folder']+confdata['paths']['yaml_destination_file'])
    elif source == "file":
        ConvertJsonToYaml.run(confdata['paths']['source_folder']+confdata['paths']['json_source_file'], confdata['paths']['source_folder']+confdata['paths']['yaml_destination_file'])
    else:
        print("invalid source")

ops = {
    'deserialize': deserialize,
    'serialize': serialize,
    'convert': convert
}

for operation in confdata['launch']['operations']:
    if operation in ops:
        if operation == "serialize" or operation == "convert":
            ops[operation](confdata['launch']['source'])
        else:
            ops[operation]()