import sys

import librosa
import numpy as np

from mirdata.datasets import tonality_classicaldb
from tests.test_utils import run_track_tests


def test_track():
    default_trackid = "0"
    data_home = "tests/resources/mir_datasets/tonality_classicaldb"
    dataset = tonality_classicaldb.Dataset(data_home)
    track = dataset.track(default_trackid)

    expected_attributes = {
        "audio_path": "tests/resources/mir_datasets/tonality_classicaldb/audio/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.wav",
        "key_path": "tests/resources/mir_datasets/tonality_classicaldb/keys/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.txt",
        "spectrum_path": "tests/resources/mir_datasets/tonality_classicaldb/spectrums/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json",
        "hpcp_path": "tests/resources/mir_datasets/tonality_classicaldb/HPCPs/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json",
        "musicbrainz_path": "tests/resources/mir_datasets/tonality_classicaldb/musicbrainz_metadata/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json",
        "title": "01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D",
        "track_id": "0",
    }

    expected_property_types = {
        "key": str,
        "spectrum": np.ndarray,
        "hpcp": np.ndarray,
        "musicbrainz_metadata": dict,
        "audio": tuple,
    }
    run_track_tests(track, expected_attributes, expected_property_types)

    audio, sr = track.audio
    assert sr == 44100, "sample rate {} is not 44100".format(sr)
    assert audio.shape == (88200,), "audio shape {} was not (88200,)".format(
        audio.shape
    )


def test_to_jams():
    data_home = "tests/resources/mir_datasets/tonality_classicaldb"
    dataset = tonality_classicaldb.Dataset(data_home)
    track = dataset.track("0")
    jam = track.to_jams()
    assert jam["sandbox"]["key"] == "D major", "key does not match expected"
    assert (
        jam["file_metadata"]["title"]
        == "01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D"
    ), "title does not match expected"

    assert "spectrum" in jam["sandbox"]
    assert "hpcp" in jam["sandbox"]
    assert "musicbrainz_metatada" in jam["sandbox"]


def test_load_key():
    key_path = "tests/resources/mir_datasets/tonality_classicaldb/keys/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.txt"
    key_data = tonality_classicaldb.load_key(key_path)

    assert type(key_data) == str

    assert key_data == "D major"

    assert tonality_classicaldb.load_key(None) is None


def test_load_spectrum():
    spectrum_path = "tests/resources/mir_datasets/tonality_classicaldb/spectrums/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json"
    audio_path = "tests/resources/mir_datasets/tonality_classicaldb/audio/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.wav"
    spectrum_data = tonality_classicaldb.load_spectrum(spectrum_path)

    assert type(spectrum_data) == np.ndarray

    y, sr = librosa.load(audio_path)
    spectrum = librosa.cqt(y, sr=sr, window="blackmanharris", hop_length=4096)

    # only first 2 seconds
    spectrum_data = spectrum_data[:, : spectrum.shape[1]]

    assert spectrum.shape[0] == spectrum_data.shape[0]
    assert spectrum.shape[1] == spectrum_data.shape[1]
    assert tonality_classicaldb.load_spectrum(None) is None


np.set_printoptions(threshold=sys.maxsize)


def test_load_hpcp():
    hpcp_path = "tests/resources/mir_datasets/tonality_classicaldb/HPCPs/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json"
    hpcp_data = tonality_classicaldb.load_hpcp(hpcp_path)

    assert type(hpcp_data) == np.ndarray
    assert hpcp_data.shape[0] == 2865
    assert hpcp_data.shape[1] == 12
    assert isinstance(hpcp_data[0][0], float) is True

    assert tonality_classicaldb.load_hpcp(None) is None


musicbrainz_metadata_annotated = {
    "chromaprint": "AQAEHZG4R0mMPzmO44ds4sKU0AW0E-YR4kR7uPgRhp_wHknv4uiVLWg4priUWPjh88gv_EJD7ugZHLmC4xduobeCaI9yHMqPa8jxHP4Q-sY_oDl6HLfRRngu9Hxw_B80X4MbEfGJ4jjy7JCjou2DKfJyHPuhRTtCIP9R6C_StUev4KKO4ydyaN-DhsgX6vjRPOifoD3MJ0boDcdDHA_wH79w_KhOwpNRHK_wozwaZsvxbYc75biJ88iFD13kHE2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QzBBjBiABIKISIAQIQQolUSAEGgDHAIOCYEUIZB5ATRAihjDCCIGJRIcAIJIgwAggBlCAIaUYAIYKSkpgSwgQsGEPOGGAUYAYZoEgQxhEkFCFBAiEIpYJIAgAEbIgkgQFCGgMCIQooQMBlbBCRpXWeAkCYsM4IhS0FRgBiBA",
    "similar_mbids": [
        {
            "artist": "Antonio Vivaldi",
            "mbid": "6d965a63-6cdb-4e49-beae-2e1247622652",
            "score": 0.96163,
            "title": "Allegro: Gloria in excelsis Deo",
        },
        {
            "artist": "The John Alldis Choir; John Constable; Olga Hegedus; Rodney Slatford; English Chamber Orchestra; Vittorio Negri",
            "mbid": "d2a477fd-0061-4db2-b39c-26c87eead92f",
            "score": 0.96163,
            "title": 'Gloria in D, RV 589: I. Allegro "Gloria in excelsis Deo"',
        },
        {
            "artist": "Antonio Vivaldi",
            "mbid": "dc863e40-7daa-4dd0-a44c-e8f33ed07268",
            "score": 0.96163,
            "title": "Gloria RV 589, I Gloria",
        },
        {
            "artist": "The John Alldis Choir; John Constable; Olga Hegedus; Rodney Slatford; English Chamber Orchestra; Vittorio Negri",
            "mbid": "d2a477fd-0061-4db2-b39c-26c87eead92f",
            "score": 0.954246,
            "title": 'Gloria in D, RV 589: I. Allegro "Gloria in excelsis Deo"',
        },
    ],
}


def test_load_musicbrainz_metadata():
    musicbrainz_metadata_path = "tests/resources/mir_datasets/tonality_classicaldb/musicbrainz_metadata/01-Allegro__Gloria_in_excelsis_Deo_in_D_Major - D.json"
    musicbrainz_metadata_data = tonality_classicaldb.load_musicbrainz(
        musicbrainz_metadata_path
    )
    print(musicbrainz_metadata_data)
    assert type(musicbrainz_metadata_data) == dict
    assert musicbrainz_metadata_data == musicbrainz_metadata_annotated

    assert tonality_classicaldb.load_musicbrainz(None) is None
