﻿// ----- music helper functions ----- //

// create an array of ids from the specified records
function idHelper_createIdArray(records) {
    var idArray = [];

    // build up array of ids
    if (records) {
        for (var iRecord = 0; iRecord < records.length; iRecord++) {
            if ((records[iRecord].data) && (records[iRecord].data.id)) {
                idArray.push(records[iRecord].data.id);
            }
        }
    }

    return idArray;
}
    
// create an array of filehash ids from the specified rcords
function fileHashHelper_createFileHashIdArray(records) {
    var fileHashIdArray = [];

    for (var iRecord = 0; iRecord < records.length; iRecord++) {
        if ((records[iRecord].data) && (records[iRecord].data.filehashid)) {
            fileHashIdArray.push(records[iRecord].data.filehashid);
        }
    }

    return fileHashIdArray;
}

 // create an array of track ids from the specified rcords
function trackHelper_createTrackIdArray(records) {
    var trackIdArray = [];

    for (var iRecord = 0; iRecord < records.length; iRecord++) {
        if ((records[iRecord].data) && (records[iRecord].data.trackid)) {
            trackIdArray.push(records[iRecord].data.trackid);
        }
    }

    return trackIdArray;
}


// -------------- queue helper functions ------------- //

/*
// set queue status of specified ids (or tracks with specified ids as artist/album, etc)
function ajax_setQueueStatus_multi(idTripleArray, isUpload, priority) {
    
    var url;

    if (isUpload) {
        url = g_setUploadPriorityMultiAsyncUrl;
    }
    else {
        url = g_setDownloadPriorityMultiAsyncUrl;
    }

    var priorityInfoMulti       = { priority: priority, ids: idTripleArray }
    var priorityInfoMultiJson   = $.toJSON(priorityInfoMulti);

    // post ajax request to server
    $.ajax({
        url: url,
        type: g_method_post,
        dataType: g_dataType_json,
        data: priorityInfoMultiJson,
        contentType: g_contentType_json,
        error: jQuery_ajaxRequestFailed
    });
}
*/

// set queue status of specified ids (or tracks with specified ids as artist/album, etc)
function ajax_setQueueStatus(sourceDeviceId, destinationDeviceId, type, idArray, isUpload, priority, showSuppressOption) {

    var url;

    if (isUpload) {
        url = g_setUploadPriorityAsyncUrl;
    }
    else {
        url = g_setDownloadPriorityAsyncUrl;
    }

    // create json data to pass back to server
    // fields must match the mappings in DevicePriorityInfo class
    // type tells the server what the idArray contains (filehash ids / artist ids, etc)
    // and therefore whether it needs to look up filehash ids if the type is anything other than 'track'
    // set priority to 1 (lowest so added on to end of queue)
    var devicePriorityInfo = {
        type: type,
        sourcedeviceid: sourceDeviceId,
        destinationdeviceid: destinationDeviceId,
        priority: priority,
        ids: idArray,
        showsuppressoption: showSuppressOption
    };
        
    var devicePriorityInfoJson = $.toJSON(devicePriorityInfo);

    // post ajax request to server then display the resulting html, be it success or failure
    // (display a different message if the request itself failed)
    $.ajax({
        url: url,
        type: g_method_post,
        data: devicePriorityInfoJson,
        contentType: g_contentType_json,
        dataType: g_dataType_html,
        success: function(data) {
            $.facebox(data);
        },
        error: jQuery_ajaxRequestFailed
    });
}


// -------------- playlist helper functions ------------- //

// add specified ids to playlist (or tracks with specified ids as artist/album, etc)
// call the success callback if the operation was successful
function ajax_addToPlaylist(playlistId, sourceDeviceId, positionQualifierEnum, targetEntryId, type, idArray, successCallback) {

    var url = g_addToPlaylistAsyncUrl;

    // create json data to pass back to server
    // fields must match the mappings in PlaylistInfo class
    // type tells the server what the idArray contains (filehash ids / artist ids, etc)
    // and therefore whether it needs to look up filehash ids if the type is anything other than 'track'
    var playlistInfo = {
        playlistid: playlistId,
        sourcedeviceid: sourceDeviceId,
        type: type,
        positionqualifierenum: positionQualifierEnum,
        targetentryid: targetEntryId,
        ids: idArray
    };
        
    var playlistInfoJson = $.toJSON(playlistInfo);

    // post ajax request to server
    $.ajax({
        url: url,
        type: g_method_post,
        dataType: g_dataType_json,
        data: playlistInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: jQuery_ajaxRequestFailed
    });
}


// reorder specified ids in playlist
// call the success callback if the operation was successful
function ajax_reorderPlaylist(playlistId, positionQualifierEnum, targetEntryId, idArray, successCallback) {

    var url = g_reorderPlaylistAsyncUrl;

    // create json data to pass back to server
    // fields must match the mappings in PlaylistInfo class
    // type tells the server what the idArray contains (filehash ids / artist ids, etc)
    // and therefore whether it needs to look up filehash ids if the type is anything other than 'track'
    var playlistInfo = {
        playlistid: playlistId,
        type: g_extJsGrid_dataTypePlaylistEntry,
        positionqualifierenum: positionQualifierEnum,
        targetentryid: targetEntryId,
        ids: idArray
    };
    
    var playlistInfoJson = $.toJSON(playlistInfo);

    // post ajax request to server
    $.ajax({
        url: url,
        type: g_method_post,
        dataType: g_dataType_json,
        data: playlistInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: jQuery_ajaxRequestFailed
    });
}