import json


def parse_fg_slice_dict_single_class(json_path, out_path, removed_fg_idx="1"):
    """
    Reads the foreground (positive) class slice dictionary and creates a new
    dictionary with `removed_fg_idx` removed.
    Args:
        json_path (str): json should be 'slice_indices.json' generated by
            `io.Preprocessor` (sub dicts contain keys for list of slice indices
            for each foreground class)
        out_path (str): path to the json to save the new dictionary with
            the `removed_fg_idx` key removed
        removed_fg_idx (str): string key to remove ('1' or '2' in this case)
    Returns:
        the changed slice_dict
    """
    # reading json
    with open(json_path, "r") as fp:
        slice_dict = json.load(fp)
    cases = list(slice_dict.keys())
    # vv assumes same for all cases (which is true)
    sub_dict_keys = list(slice_dict[cases[0]])
    print(f"{len(cases)} Cases; Case sub-dict keys: {sub_dict_keys}")
    # removing all sub dicts with the `removed_fg_idx` key
    print(f"Removing idx: {removed_fg_idx}")
    for case in cases:
        case_dict = slice_dict[case]
        case_dict.pop(removed_fg_idx)
    sub_dict_keys = list(slice_dict[cases[0]])
    print(f"New case sub-dict keys: {sub_dict_keys}")
    # saving dict
    with open(out_path, "w") as fp:
        json.dump(slice_dict, fp)
    print(f"Saved at {out_path}.")
    return slice_dict