import json
import os

def split_file(rootpath, cv_idx):
    if os.path.isdir(rootpath):
        path_splitfile = os.path.join(rootpath, f'cv_{cv_idx}.yaml')
        assert os.path.exists(path_splitfile)
    else:
        path_splitfile = rootpath
    return path_splitfile



def load_json(specs_filename, cv_idx=None, sample_size: int=None):
    if not os.path.isfile(specs_filename):
        raise Exception(
            'The experiment directory does not include specifications file "specs.json"'
        )

    # load specs
    specs = json.load(open(specs_filename))
    if sample_size is not None:
        specs["SampleSize"] = sample_size
        specs["ExperimentName"] = os.path.join(specs["ExperimentName"], "SampleSize_" + str(sample_size))
    if cv_idx is not None:
        specs["Split"] = split_file(specs["Split"], cv_idx)
        specs["ExperimentName"] = os.path.join(specs["ExperimentName"], "fold_" + str(cv_idx))

    return specs



# def load_json_varying_sample_size(specs_filename, cv_idx: int=None, sample_size: int=None):
#     if not os.path.isfile(specs_filename):
#         raise Exception(
#             'The experiment directory does not include specifications file "specs.json"'
#         )
#
#     # load specs
#     specs = json.load(open(specs_filename))
#     if sample_size is not None:
#         specs["SampleSize"] = sample_size
#         specs["ExperimentName"] = os.path.join(specs["ExperimentName"], "SampleSize_" + str(sample_size))
#     if cv_idx is not None:
#         specs["Split"] = split_file(specs["Split"], cv_idx)
#         specs["ExperimentName"] = os.path.join(specs["ExperimentName"], "fold_" + str(cv_idx))
#     return specs
