﻿// ----- 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;
}

// determine if one or more tracks (represented by the specified record) can be streamed
function trackHelper_isOneOrMoreTracksStreamable(records) {

    var isStreamable = false;

    // check each record in turn
    for (var record = 0; ((record < records.length) && !isStreamable); record++) {
        if (records[record].data.streamablefileavailable) {
            isStreamable = true;
        }
    }

    return isStreamable;
}


// -------------- client device panel helper functions ------------- //

// asynchronously load the devices panel body to the content returned by the ajax request
// (the ViewUserControl rendered by the MusicController)
function ajax_loadDevicesPanelContent() {

    $.ajax({
        url: g_devicesPanelUserControlAsyncUrl, // note: global url
        type: g_method_get,
        dataType: g_dataType_html,
        success: function(data) {
            $("#music_left").html(data);
        }
        // don't worry about error handling here as is non-fatal
    });
}


// -------------- 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_redirectToLogin
    });
}
*/

// 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_redirectToLogin
    });
}


// -------------- 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_redirectToLogin
    });
}


// update the specified playlist
// call the success callback if the operation was successful
function ajax_updatePlaylist(playlistId, start, end, deleteIdArray, updateIdArray, successCallback) {

    var url = g_updatePlaylistAsyncUrl;

    // create json data to pass back to server
    // fields must match the mappings in PlaylistUpdateInfo class
    var playlistUpdateInfo = {
        playlistid: playlistId,
        sourcedeviceid: g_cloud_deviceId,
        start: start,
        end: end,
        deleteids: deleteIdArray,
        updateids: updateIdArray
    };

    var playlistUpdateInfoJson = $.toJSON(playlistUpdateInfo);

    // post ajax request to server
    $.ajax({
        url: url,
        type: g_method_post,
        dataType: g_dataType_json,
        data: playlistUpdateInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: jQuery_ajaxRequestFailed_redirectToLogin
    });
}