from pathlib import Path
from typing import List
from typing import Dict


def sort_videos(project_path: Path) -> List[Path]:
    """Sorts each videos in a project.

    Videos are sorted by scene and then by videos.

    The scenes are sorted using their `id`. The minimal
    `id` value is `1`, and there is no maximum.

    Once the scenes are sorted, each video in each scene
    is sorted, starting with the scene with id `1`. Each
    video is appended to a list of paths.

    Args:
        project_path (Path): The path towards the project
            from which the videos will be found and sorted.

    Returns:
        List[Path]: A sorted list of paths towards the video recordings.
    """

    # Sorting scenes
    scene_amount: int = 0

    scene_dict: Dict[int, Path] = {}

    for directory in project_path.iterdir():
        if "scene_" in directory.name:
            scene_id: int = int(directory.stem.split("_")[1])
            scene_dict[scene_id] = directory

    all_scenes: List[Path] = [item[1] for item in sorted(scene_dict.items())]

    # Sorting per scene.
    all_videos: List[Path] = []

    for scene in all_scenes:

        videos_path: Path = scene / Path("videos")

        videos_dict: Dict[int, Path] = {}

        for video in videos_path.iterdir():
            if "commands_" in video.name:
                video_id: int = int(video.stem.split("_")[1])
                videos_dict[video_id] = video

        all_videos = all_videos + [item[1] for item in sorted(videos_dict.items())]

    return all_videos