import pickle


def stop_idx_in_route(stop_times_file, FOLDER):
    """
    This function saves a dictionary to provide easy access to index of a stop in a route.
    Args:
        stop_times_file (pandas.dataframe): stop_times.txt file in GTFS.
        FOLDER (str): path to network folder.
    Returns:
        idx_by_route_stop_dict (dict): Keys: (route id, stop id), value: stop index. Format {(route id, stop id): stop index in route}.
    """
    pandas_group = stop_times_file.groupby(["route_id","stop_id"])
    idx_by_route_stop = {route_stop_pair:details.stop_sequence.iloc[0] for route_stop_pair, details in pandas_group}

    with open(f'./dict_builder/{FOLDER}/idx_by_route_stop.pkl', 'wb') as pickle_file:
        pickle.dump(idx_by_route_stop, pickle_file)
    print("idx_by_route_stop done")
    return idx_by_route_stop