/**
 * This code was taken from the framework controller_scene.js
 * This should be used for service request management when a 
 * scene is not available (headless parts of apps).
 
Copyright 2009 Palm, Inc.  All rights reserved.

*/

var ServiceRequestHelper = Class.create({
	initialize: function() {
		this.activeServiceRequests = [];
		this.activeSubscribedServiceRequests = []; //manage the subscribed version separately
		this._serviceRequestErrorHandler = this.serviceRequestError.bind(this);
	},
	
	_completeRequest: function(r, data) {
		var i = -1;
	  	var subscribed = data.subscriberId; //if subscribed was true, this needs to be moved
		if (this.activeServiceRequests){
			i = this.activeServiceRequests.indexOf(r);
			if (i !== -1) {
				this.activeServiceRequests.splice(i, 1);
				if (data.subscriberId) {
					Mojo.Log.error("ERROR! We removed a subscribed request onComplete.");
				}
			} else {
				if (!data.subscriberId) {
					Mojo.Log.warn("WARNING: scene controller was asked to remove a request object wasn't in the list.");
				}
			}
		} 
	},

	onComplete: function(appOnComplete, data, activeReq) {
		if (!data.failed) { //marked this as failed, so leave it in the list to be removed later
			this._completeRequest(activeReq, data);
  
			if (appOnComplete) {
		    	appOnComplete(data);
	    	}
		}
	},
	
	removeRequest: function(r) {
		var i = -1;
		if (this.activeServiceRequests){
		    i = this.activeServiceRequests.indexOf(r);
			if (i !== -1) {
				this.activeServiceRequests.splice(i, 1);
			}
		}
		
		if (i === -1 && this.activeSubscribedServiceRequests) { //try the subscribed list
			i = this.activeSubscribedServiceRequests.indexOf(r);
			if (i !== -1) {
				this.activeSubscribedServiceRequests.splice(i, 1);
			}
		}
		
		if (i === -1) {
			Mojo.Log.warn("WARNING: scene controller was asked to remove a request object wasn't in either the active requests or subscribed requests list.");
		}
	},
	
	
	cancelServiceRequest: function(request) {	
		this.removeRequest(request);
		request.originalCancel();
	},
	
	serviceRequest: function(url, options, resubscribe) {
		var serviceRequestOptions;
		
		if (!this.activeServiceRequests) {
			Mojo.Log.error(this.activeServiceRequests, "ActiveServiceRequests does not exist for this scene. Cleanup was called before this request was made.");
			return undefined;
		}
		
		serviceRequestOptions = {
			onFailure: this._serviceRequestErrorHandler,
			onComplete: this.onComplete.bind(this, options.onComplete)
		};
		Object.extend(serviceRequestOptions, options || { });
		var request = new Mojo.Service.Request(url, serviceRequestOptions, resubscribe);
		request.originalCancel = request.cancel;
		request.cancel = this.cancelServiceRequest.bind(this, request);
		if (options.parameters && options.parameters.subscribe) {
			this.activeSubscribedServiceRequests.push(request);
		} else {
			this.activeServiceRequests.push(request);
		}
		return request;
	},
	
	serviceRequestError: function(response) {
		Mojo.Log.error("Error: service request: %s", response.errorText);
		//Mojo.Controller.errorDialog(response.errorText, this.window);
		response.failed = true; //mark this as a failed request so that we leave it alone
	}
	
});