import os
from typing import Tuple

import torchaudio
from torch import Tensor
from torch.utils.data import Dataset
from torchaudio.datasets.utils import (
    download_url,
    extract_archive,
    walk_files,
)

URL = "train-clean-100"
FOLDER_IN_ARCHIVE = "LibriTTS"
_CHECKSUMS = {
    "http://www.openslr.org/60/dev-clean.tar.gz": "0c3076c1e5245bb3f0af7d82087ee207",
    "http://www.openslr.org/60/dev-other.tar.gz": "815555d8d75995782ac3ccd7f047213d",
    "http://www.openslr.org/60/test-clean.tar.gz": "7bed3bdb047c4c197f1ad3bc412db59f",
    "http://www.openslr.org/60/test-other.tar.gz": "ae3258249472a13b5abef2a816f733e4",
    "http://www.openslr.org/60/train-clean-100.tar.gz": "4a8c202b78fe1bc0c47916a98f3a2ea8",
    "http://www.openslr.org/60/train-clean-360.tar.gz": "a84ef10ddade5fd25df69596a2767b2d",
    "http://www.openslr.org/60/train-other-500.tar.gz": "7b181dd5ace343a5f38427999684aa6f",
}


def load_libritts_item(
    fileid: str,
    path: str,
    ext_audio: str,
    ext_original_txt: str,
    ext_normalized_txt: str,
) -> Tuple[Tensor, int, str, str, int, int, str]:
    speaker_id, chapter_id, segment_id, utterance_id = fileid.split("_")
    utterance_id = fileid

    normalized_text = utterance_id + ext_normalized_txt
    normalized_text = os.path.join(path, speaker_id, chapter_id, normalized_text)

    original_text = utterance_id + ext_original_txt
    original_text = os.path.join(path, speaker_id, chapter_id, original_text)

    file_audio = utterance_id + ext_audio
    file_audio = os.path.join(path, speaker_id, chapter_id, file_audio)

    # Load audio
    waveform, sample_rate = torchaudio.load(file_audio)

    # Load original text
    with open(original_text) as ft:
        original_text = ft.readline()

    # Load normalized text
    with open(normalized_text, "r") as ft:
        normalized_text = ft.readline()

    return (
        waveform,
        sample_rate,
        original_text,
        normalized_text,
        int(speaker_id),
        int(chapter_id),
        utterance_id,
    )


class LIBRITTS(Dataset):
    """Create a Dataset for LibriTTS.

    Args:
        root (str): Path to the directory where the dataset is found or downloaded.
        url (str, optional): The URL to download the dataset from,
            or the type of the dataset to dowload.
            Allowed type values are ``"dev-clean"``, ``"dev-other"``, ``"test-clean"``,
            ``"test-other"``, ``"train-clean-100"``, ``"train-clean-360"`` and
            ``"train-other-500"``. (default: ``"train-clean-100"``)
        folder_in_archive (str, optional):
            The top-level directory of the dataset. (default: ``"LibriTTS"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
    """

    _ext_original_txt = ".original.txt"
    _ext_normalized_txt = ".normalized.txt"
    _ext_audio = ".wav"

    def __init__(
        self,
        root: str,
        url: str = URL,
        folder_in_archive: str = FOLDER_IN_ARCHIVE,
        download: bool = False,
    ) -> None:

        if url in [
            "dev-clean",
            "dev-other",
            "test-clean",
            "test-other",
            "train-clean-100",
            "train-clean-360",
            "train-other-500",
        ]:

            ext_archive = ".tar.gz"
            base_url = "http://www.openslr.org/resources/60/"

            url = os.path.join(base_url, url + ext_archive)

        basename = os.path.basename(url)
        archive = os.path.join(root, basename)

        basename = basename.split(".")[0]
        folder_in_archive = os.path.join(folder_in_archive, basename)

        self._path = os.path.join(root, folder_in_archive)

        if download:
            if not os.path.isdir(self._path):
                if not os.path.isfile(archive):
                    checksum = _CHECKSUMS.get(url, None)
                    download_url(url, root, hash_value=checksum)
                extract_archive(archive)

        walker = walk_files(
            self._path, suffix=self._ext_audio, prefix=False, remove_suffix=True
        )
        self._walker = list(walker)

    def __getitem__(self, n: int) -> Tuple[Tensor, int, str, str, int, int, str]:
        """Load the n-th sample from the dataset.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            tuple: ``(waveform, sample_rate, original_text, normalized_text, speaker_id,
            chapter_id, utterance_id)``
        """
        fileid = self._walker[n]
        return load_libritts_item(
            fileid,
            self._path,
            self._ext_audio,
            self._ext_original_txt,
            self._ext_normalized_txt,
        )

    def __len__(self) -> int:
        return len(self._walker)

[((1270, 1329), 'os.path.join', 'os.path.join', (['path', 'speaker_id', 'chapter_id', 'normalized_text'], {}), '(path, speaker_id, chapter_id, normalized_text)\n', (1282, 1329), False, 'import os\n'), ((1403, 1460), 'os.path.join', 'os.path.join', (['path', 'speaker_id', 'chapter_id', 'original_text'], {}), '(path, speaker_id, chapter_id, original_text)\n', (1415, 1460), False, 'import os\n'), ((1521, 1575), 'os.path.join', 'os.path.join', (['path', 'speaker_id', 'chapter_id', 'file_audio'], {}), '(path, speaker_id, chapter_id, file_audio)\n', (1533, 1575), False, 'import os\n'), ((1622, 1649), 'torchaudio.load', 'torchaudio.load', (['file_audio'], {}), '(file_audio)\n', (1637, 1649), False, 'import torchaudio\n'), ((3507, 3528), 'os.path.basename', 'os.path.basename', (['url'], {}), '(url)\n', (3523, 3528), False, 'import os\n'), ((3547, 3575), 'os.path.join', 'os.path.join', (['root', 'basename'], {}), '(root, basename)\n', (3559, 3575), False, 'import os\n'), ((3647, 3688), 'os.path.join', 'os.path.join', (['folder_in_archive', 'basename'], {}), '(folder_in_archive, basename)\n', (3659, 3688), False, 'import os\n'), ((3711, 3748), 'os.path.join', 'os.path.join', (['root', 'folder_in_archive'], {}), '(root, folder_in_archive)\n', (3723, 3748), False, 'import os\n'), ((4046, 4131), 'torchaudio.datasets.utils.walk_files', 'walk_files', (['self._path'], {'suffix': 'self._ext_audio', 'prefix': '(False)', 'remove_suffix': '(True)'}), '(self._path, suffix=self._ext_audio, prefix=False, remove_suffix=True\n    )\n', (4056, 4131), False, 'from torchaudio.datasets.utils import download_url, extract_archive, walk_files\n'), ((3445, 3486), 'os.path.join', 'os.path.join', (['base_url', '(url + ext_archive)'], {}), '(base_url, url + ext_archive)\n', (3457, 3486), False, 'import os\n'), ((3790, 3815), 'os.path.isdir', 'os.path.isdir', (['self._path'], {}), '(self._path)\n', (3803, 3815), False, 'import os\n'), ((4003, 4027), 'torchaudio.datasets.utils.extract_archive', 'extract_archive', (['archive'], {}), '(archive)\n', (4018, 4027), False, 'from torchaudio.datasets.utils import download_url, extract_archive, walk_files\n'), ((3840, 3863), 'os.path.isfile', 'os.path.isfile', (['archive'], {}), '(archive)\n', (3854, 3863), False, 'import os\n'), ((3942, 3986), 'torchaudio.datasets.utils.download_url', 'download_url', (['url', 'root'], {'hash_value': 'checksum'}), '(url, root, hash_value=checksum)\n', (3954, 3986), False, 'from torchaudio.datasets.utils import download_url, extract_archive, walk_files\n')]