/* Copyright 2009 Palm, Inc.  All rights reserved. */

Photos.Service = function() {
	function commonParams(offset, limit, filter) {
		var parameters = {};
		if (limit >= 0) {
			parameters.limit = limit;
		}
		if (offset >= 0) {
			parameters.offset = offset;
		}
		if (filter) {
			parameters.filter =
				'%' + filter.replace("\\", "\\\\")
						.replace("_", "\\_")
						.replace("%", "\\%") + '%';
		}
		return parameters;
	}

	return {
		getState: function(callback, sceneController) {
			return sceneController.serviceRequest(
				'palm://com.palm.mediadb/', {
					method: 'getstate',
					onSuccess: callback,
					onFailure: callback,
					resubscribe: true,
					parameters: {subscribe: true}
				}, {resubscribe: true});
		},

		deleteFile: function(path, callback, sceneController) {
			return sceneController.serviceRequest(
					'palm://com.palm.mediadb/', {
						method: 'deletefile',
						onSuccess: callback,
						onFailure: callback,
						parameters: {path: path}
					});
		},

		listAlbums: function(offset, limit, filter,
						callback, sceneController) {
			var parameters = commonParams(offset, limit, filter);
			parameters.subscribe = true;
			parameters.limitedRequest = true;
			parameters.albumNameMiscImages = $L('Miscellaneous');
			parameters.albumNameSyncImages =
					$L('Full-resolution Media Sync');
			return sceneController.serviceRequest(
				'palm://com.palm.mediadb/image', {
					method: 'listalbums',
					onSuccess: callback,
					parameters: parameters
			});
		},

		detailPhotoRoll: function(callback, sceneController) {
			var parameters = {};
			parameters.subscribe = false;
			parameters.albumNamePhotoRoll = $L('Photo roll');
			return sceneController.serviceRequest(
				'palm://com.palm.mediadb/image', {
					method: 'detailphotoroll',
					onSuccess: callback,
					parameters: parameters
			});
		},

		listImages: function(offset, limit, filter, albumID, subscribe,
					callback, sceneController) {
			var parameters = commonParams(offset, limit, filter);
			if (subscribe) {
				parameters.subscribe = true;
			}
			if (albumID > 0) {
				parameters.albumID = albumID;
			}
			return sceneController.serviceRequest(
				'palm://com.palm.mediadb/image', {
					method: 'listimages',
					onSuccess: callback,
					parameters: parameters
			});
		}
	};
}();

