# Need this to import from parent directory when running outside pycharm
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))

from ac_utils.general import save_to_json, load_from_json
import click
import xml.etree.ElementTree
from urllib import unquote


def find_corresponding_rekordbox_entry(sound_metadata, rekordbox_file):
    collection = rekordbox_file.find('COLLECTION')
    found = False
    for document in collection:
        if str(sound_metadata['id']) in document.attrib['Location'].split('/')[-1]:
            found = document
            break
        if str(sound_metadata['wav_sound_path'].split('/')[-1]) in document.attrib['Location'].split('/')[-1]:
            found = document
            break
        if str(sound_metadata['wav_sound_path'].split('/')[-1]) in unquote(document.attrib['Location'].split('/')[-1]):
            found = document
            break
    return found


@click.command()
@click.argument('dataset_path')
def rekordbox_file_to_analysis_file(dataset_path):
    """
    Read information from rekordbox_rhythm.xml present in dataset_path and convert it into
    analsysis_rhythm_rekordbox.json to be stored in the same folder and compatible with our evaluation
    framework.
    """
    rekordbox_file = xml.etree.ElementTree.parse(os.path.join(dataset_path, 'rekordbox_rhythm.xml')).getroot()
    metadata_file = load_from_json(os.path.join(dataset_path, 'metadata.json'))
    out_file_path = os.path.join(dataset_path, 'analysis_rhythm_rekordbox.json')

    analysis = dict()
    with click.progressbar(metadata_file.keys(), label="Converting...") as metadata_keys:
        for key in metadata_keys:
            entry = find_corresponding_rekordbox_entry(metadata_file[key], rekordbox_file)
            if entry is not False:
                tempo_entry = entry.find('TEMPO')
                if tempo_entry is not None:
                    bpm_raw = float(tempo_entry.attrib['Bpm'])
                else:
                    bpm_raw = 0.0
                analysis[key] = {"RekBox": {
                        "bpm": bpm_raw,
                    }
                }
    save_to_json(out_file_path, analysis, verbose=True)

if __name__ == '__main__':
    rekordbox_file_to_analysis_file()

[((28, 1, 28, 16), 'click.command', 'click.command', ({}, {}), '()', False, 'import click\n'), ((29, 1, 29, 31), 'click.argument', 'click.argument', ({(29, 16, 29, 30): '"""dataset_path"""'}, {}), "('dataset_path')", False, 'import click\n'), ((38, 20, 38, 80), 'os.path.join', 'os.path.join', ({(38, 33, 38, 45): 'dataset_path', (38, 47, 38, 79): '"""analysis_rhythm_rekordbox.json"""'}, {}), "(dataset_path, 'analysis_rhythm_rekordbox.json')", False, 'import os\n'), ((54, 4, 54, 55), 'ac_utils.general.save_to_json', 'save_to_json', (), '', False, 'from ac_utils.general import save_to_json, load_from_json\n'), ((37, 35, 37, 78), 'os.path.join', 'os.path.join', ({(37, 48, 37, 60): 'dataset_path', (37, 62, 37, 77): '"""metadata.json"""'}, {}), "(dataset_path, 'metadata.json')", False, 'import os\n'), ((4, 45, 4, 71), 'os.path.realpath', 'os.path.realpath', ({(4, 62, 4, 70): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((36, 49, 36, 99), 'os.path.join', 'os.path.join', ({(36, 62, 36, 74): 'dataset_path', (36, 76, 36, 98): '"""rekordbox_rhythm.xml"""'}, {}), "(dataset_path, 'rekordbox_rhythm.xml')", False, 'import os\n')]