import json


def make_base_config(config_file, kwargs, par):
    """
    Make a config file for `fps_single.py`.
    Args:
        config_file (str): path to general config file
        kwargs (dict): extra dictionary items to put in it
        par (bool): whether we're going to be using
            this to make fingerprints in parallel.
    Returns:
        base_path (str): path to the new, base config
            file, which is missing certain keys if we're
            running this in parallel.
    """

    # get the contents of the config file
    with open(config_file, "r") as f_open:
        config = json.load(f_open)

    # update it with any new values
    for key, val in kwargs.items():
        # if you put the config file in the dictionary,
        # but the config file is the original name and
        # not the new name, it messes things up
        if key not in config and key != "config_file":
            config[key] = val

    # if running in parallel, get rid of these keys because
    # we'll want to specify them individually for each parallel
    # process

    if par:
        par_keys = ["dset_folder", "feat_save_folder", "config_file",
                    "slurm_parallel"]
        for key in par_keys:
            if key in config:
                config.pop(key)

    # make a new path to this "base" config and save
    base_path = config_file.replace(".json", "_base.json")
    with open(base_path, "w") as f_open:
        json.dump(config, f_open, indent=4, sort_keys=True)
    return base_path