from typing import OrderedDict
import json


def load_recording(frame_file_tuples, session_id=None, camera=None, width=None, height=None, verbose=False):
    """
    Load all frames of a single recording and return the dict representation of the recording.
    :param frame_file_tuples: List of (frame index, filename) tuples. Files are loaded in frame order.
    :param session_id: The identifier of this multi-camera recording session.
    :param camera: The identifier of this specific recording (i.e. camera angle) within the session.
    :param width: An integer representing the pixel width of the video file with which the OpenPose data was computed.
    :param height: An integer representing the pixel height of the video file with which the OpenPose data was computed.
    :param verbose: If true, print script progress information.
    :return:
    """
    if verbose:
        print('Loading {} frames for recording "{}" of session "{}"'.format(len(frame_file_tuples), camera, session_id))

    # Set up recording dictionary
    recording_dict = OrderedDict()
    if session_id is not None:
        recording_dict['id'] = str(session_id)
    if camera is not None:
        recording_dict['camera'] = str(camera)
    if width is not None:
        recording_dict['width'] = int(width)
    if height is not None:
        recording_dict['height'] = int(height)

    # Load frame data
    recording_dict['frames'] = frame2data = OrderedDict()
    for frame, filename in sorted(frame_file_tuples):
        with open(filename) as f:
            frame2data[frame] = json.load(f, object_pairs_hook=OrderedDict)

    return recording_dict