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

var AppCatalogService = Class.create( {
 	initialize: function() {
		
	},
	
	getServerUrl: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(AppCatalogService.accountServiceIdentifier, {
				method: 'getServerUrl',
				parameters: {},
				onSuccess: successCallback,
				onFailure: errorCallback
			});
	},
	
	getToken: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(AppCatalogService.accountServiceIdentifier, {
			method: 'getAccountToken',
			parameters: {},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getDeviceId: function (successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(AppCatalogService.deviceProfileServiceIdentifier, {
			method: 'getDeviceId',
			parameters: {"subscribe":true},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getCarrier: function (successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(AppCatalogService.preferencesServiceIdentifier, {
			method: 'Get',
			parameters: {"key": "com.palm.properties.DMCARRIER", "subscribe":true},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getUserInstalledApps: function (url, id, crier, tkn, eml, successCallback, errorCallback) {
		 		 
		//var params = "{'AccountTokenInfo':{'token':'" + token +"'," + "'deviceId':'" + deviceId + "'," + "'email':'" + email + "'," + "'carrier':'"+ carrier +"'}}";
		var params = {
			AccountTokenInfo: {
				token: tkn,
				deviceId: id,
				email: eml,
				carrier: crier
			}
		};
		Mojo.Log.info("-------- getUserInstalledApps Params ---------", url, Object.toJSON(params));
		new Ajax.Request(url + "getAppListForUpdates", {
			method:"POST",
			contentType:"application/json",
			postBody: Object.toJSON(params),
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
});

AppCatalogService.downloadManagerServiceIdentifier='palm://com.palm.downloadmanager';
AppCatalogService.appManagerServiceIdentifier='palm://com.palm.applicationManager';
AppCatalogService.accountServiceIdentifier='palm://com.palm.accountservices';
AppCatalogService.deviceProfileServiceIdentifier='palm://com.palm.deviceprofile';
AppCatalogService.preferencesServiceIdentifier='palm://com.palm.preferences/systemProperties';




var AppCatalogMockService = Class.create( {
 	initialize: function() {
		this.ticket = 1;
		this.progress = .1;
	},
	
	getServerUrl: function(successCallback, errorCallback, sceneController) {
		successCallback({serverUrl:"fake server"});
	},
	
	getToken: function(successCallback, errorCallback, sceneController) {
		successCallback({token:"fake token", accountAlias:"fake email"});
	},
	
	getDeviceId: function (successCallback, errorCallback, sceneController) {
		successCallback({deviceId:"fake deviceId"});
	},
	
	getCarrier: function (successCallback, errorCallback, sceneController) {
		successCallback({carrier:"fake carrier"});
	},
	
	getUserInstalledApps: function (url, id, crier, tkn, eml, successCallback, errorCallback) {
		 		 
		//var params = "{'AccountTokenInfo':{'token':'" + token +"'," + "'deviceId':'" + deviceId + "'," + "'email':'" + email + "'," + "'carrier':'"+ carrier +"'}}";
		var params = {AccountTokenInfo: {filter: false,token: tkn,deviceId: id,email: eml,carrier: crier}		};
		Mojo.Log.info("---------------------------- Params ------------------------", url, Object.toJSON(params));
		var fake = {"responseText": "{" +
						"\"OutAppListForUpdates\": " +
							"{\"appList\":[" +
										"{\"appIcon\":\"../images/small.png\",\"appVersion\":1,\"id\":368,\"packageUrl\":\"http:\\/\\/ec2-75-101-159-115.com\\/virtual1\\/apps\\/368\\/com.palm.app.fish.ipk\",\"price\":0,\"publicApplicationId\":\"com.palm.app.fish\",\"title\":\"Fish\"}," +
										"{\"appIcon\":\"../images/small.png\",\"appVersion\":1,\"id\":367,\"packageUrl\":\"http:\\/\\/ec2-75-101-159-115.com\\/virtual1\\/apps\\/367\\/com.palm.app.cat.ipk\",\"price\":1.99,\"publicApplicationId\":\"com.palm.app.Cat\",\"title\":\"Cat\"}," +
										"{\"appIcon\":\"../images/small.png\",\"appVersion\":1,\"id\":366,\"packageUrl\":\"http:\\/\\/ec2-75-101-159-115.com\\/virtual1\\/apps\\/366\\/com.palm.app.dog.ipk\",\"price\":0,\"publicApplicationId\":\"com.palm.app.Dog\",\"title\":\"Dog\"}," +
										"{\"appIcon\":\"../images/small.png\",\"appVersion\":1,\"id\":365,\"packageUrl\":\"http:\\/\\/ec2-75-101-159-115.com\\/virtual1\\/apps\\/365\\/com.palm.app.mouse.ipk\",\"price\":1.77,\"publicApplicationId\":\"com.palm.app.Mouse\",\"title\":\"Mouse\"}" +
										"]" +
							"}" +
							"}"
					};
		successCallback(fake);
	},
	
	installAppCompleted: function(accountToken, url, item, successCallback, errorCallback, sceneController) {
		
	},
		
	installApp: function(item, successCallback, errorCallback, sceneController) {
		item.subscribe = true;
		
		this.intervalFunc = function(){
			var ticket = item.ticket || this.ticket++;			
			var progress = (item.progress) ? (item.progress + .1) : this.progress;
			
			Mojo.Log.info("install item interval ", Object.toJSON(item), ticket, progress);
			if (progress >= 1) {
				window.clearInterval(item.updater);
				var rsp = {"ticket": ticket, "amountReceived": 100, "amountTotal": 100, "completed": true};				
				successCallback(rsp);
			}
			else {				
				var total = 100;
				var received = total * progress;
				var rsp = {"ticket": ticket, "amountReceived": received, "amountTotal": total};
				successCallback(rsp);				
			}
		}
		
		item.updater = window.setInterval(this.intervalFunc.bind(this), 1000);
		return {cancel: function(){}};
	},
		
	
	cancelInstallApp: function(item, successCallback, errorCallback, sceneController) {
		Mojo.Log.info("cancelInstallApp item", Object.toJSON(item));
		window.clearInterval(item.updater);
		delete item.upater;
		if(successCallback)
			successCallback({});
	}
});