import os


def write_yaml(data=None):
    if data:
        r_dict = {
            "DIRECTORY": data.get("DIRECTORY"),
            "BASELINE DIRECTORY": data.get("BASELINE DIRECTORY"),
            "GRADIENT": data.get("GRADIENTS", [10]),
            "ISOTHERMS": data.get("ISOTHERMS", []),
            "WAVE LENGTHS": data.get("WAVE LENGTHS", [1, 2, 3]),
            "RMS": data.get("RMS", False),
            "TECHNIQUES": data.get("TECHNIQUES"),
        }
    else:
        r_dict = {
            "DIRECTORY": os.getcwd(),
            "PROJECT": "VERY NICE",
            "TOPICS": ["WOW WHAT A TOPIC", "VERY GOOD CHOICE", "NICE", "WELL DONE",],
            "GRADIENTS": [10],
            "ISOTHERMS": [190, 220, 350],
            "TECHNIQUES": {
                "STA": True,
                "IR": True,
                "GC": False,
                "MS": True,
                "TG": False,
            },
        }
    return r_dict


def write_post_yaml(data):
    new_data = write_yaml(data)
    root = data.get("DIRECTORY", os.getcwd())
    project_name = data.get("PROJECT", "<NAME MISSING>")
    project = os.path.join(root, f"Project {project_name}")
    new_data["DIRECTORY"] = project
    write_path = os.path.join(project, "project parameters.yaml")
    parameters = os.path.join(project, "Parameters")
    baseline = os.path.join(parameters, "Baseline")
    new_data["BASELINE DIRECTORY"] = baseline
    return new_data, write_path


def decompress(data_loaded):
    root = data_loaded.get("DIRECTORY", os.getcwd())
    project_name = data_loaded.get("PROJECT", "<NAME MISSING>")
    project = os.path.join(root, f"Project {project_name}")
    isotherms = [iso for iso in data_loaded.get("ISOTHERMS") if iso]
    gradients = [grad for grad in data_loaded.get("GRADIENTS") if grad]
    methods = [
        method
        for method in data_loaded.get("TECHNIQUES")
        if data_loaded.get("TECHNIQUES")[method]
    ]
    topics = [f"Topic {topic}" for topic in data_loaded.get("TOPICS") if topic]
    parameters = os.path.join(project, "Parameters")
    baseline = os.path.join(parameters, "Baseline")
    tg_methods = os.path.join(parameters, "Method")
    return (
        project,
        topics,
        methods,
        isotherms,
        gradients,
        parameters,
        baseline,
        tg_methods,
    )


def isograder(isograd, temperature, methods):
    for temp in temperature:
        iso_path = os.path.join(isograd, str(temp))
        try:
            os.mkdir(iso_path)
        except OSError:
            pass
        try:
            for method in methods:
                iso_method_path = os.path.join(iso_path, method)
                os.mkdir(iso_method_path)
        except OSError:
            continue
