var MockNetworkList = Class.create({ 
	initialize: function() { 
	}
});

MockNetworkList.getList = function(callback) {
	callback(networkListRaw);
}

MockNetworkList.connect = function(profileId, callback, failCallback) {
	var response = {'profile': profileId};
	callback(response);
}

MockNetworkList.subscribe = function(callback, failCallback) {
	Mojo.Log.info("Faking subscription response: Connected to 'palmOneWLAN'")
	var response = {
			"notificationType" : "connected",
			"ssid": 'palmOneWLAN'
	};
	callback(response);
}


var NetworkList = Class.create({ 
	initialize: function() { 
	}
});


NetworkList.identifier = 'palm://com.palm.wifi';

NetworkList.findNetworks = function(controller, callback, failCallback) {
	Mojo.Log.info("NetworkList.findNetworks {}");
	var request = controller.serviceRequest(NetworkList.identifier, {
			method: 'findnetworks', 
			onSuccess: callback,
			onFailure: failCallback
		});
	return request;
}

NetworkList.subscribeFindNetworks = function(controller, callback, failCallback) {
	Mojo.Log.info("NetworkList.findNetworks subscribe controller {sub:true}");
	var request = controller.serviceRequest(NetworkList.identifier, {
            method: 'findnetworks',
            parameters: {'subscribe':true}, 
            onSuccess: callback,
            onFailure: failCallback
        });	
		
	return request;
}

function cloneObject(item) {
	for (i in item) {
		if (typeof item[i] == 'object') {
			this[i] = new cloneObject(item[i]);
		} else {
			this[i] = item[i];
		}
	}	
}

NetworkList.printProfile = function(profile) {
	var safeJson = profile;
	
	if (safeJson) {
		if (safeJson.security && safeJson.security.simpleSecurity && safeJson.security.simpleSecurity.passKey) {
			safeJson = new cloneObject(profile);
			safeJson.security.simpleSecurity.passKey = "*".times(safeJson.security.simpleSecurity.passKey.length);
		} else if (safeJson.security && safeJson.security.enterpriseSecurity && safeJson.security.enterpriseSecurity.password) {
			safeJson = new cloneObject(profile);
			safeJson.security.enterpriseSecurity.password = "*".times(safeJson.security.enterpriseSecurity.password.length);
		}
	}
	
	return Object.toJSON(safeJson);			
}

NetworkList.connect = function(controller, profile, callback, failCallback) {
	Mojo.Log.info("NetworkList.connect " + NetworkList.printProfile(profile));
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'connect',
		parameters: profile,
		onSuccess: callback,
		onFailure: failCallback 
	});
	return request;
}

NetworkList.subscribe = function(controller, state, callback, failCallback) {
	Mojo.Log.info("NetworkList.subscribe " + state);
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'getstatus',
		parameters: {'subscribe':state},
		onSuccess: callback,
		onFailure: failCallback 
	});
	return request;
}

NetworkList.setState = function(controller, state, callback, failCallback) {
	Mojo.Log.info("NetworkList.setState: " + state);
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'setstate',
		parameters: {'state':state},
		onSuccess: callback,
		onFailure: failCallback 
	});
	return request;
}

NetworkList.setWakeOnWlanSupport = function(controller, state, callback, failCallback) {
        Mojo.Log.info("NetworkList.setWakeOnWlanSupport: " + state);
        var request = controller.serviceRequest(NetworkList.identifier, {
                method: 'setWakeOnWlanSupport',
                parameters: {'state':state},
                onSuccess: callback,
                onFailure: failCallback
        });
        return request;
}

NetworkList.getWakeOnWlanSupport = function(controller, callback, failCallback) {
        Mojo.Log.info("NetworkList.getWakeOnWlanSupport {}");
        var request = controller.serviceRequest(NetworkList.identifier, {
                method: 'setWakeOnWlanSupport',
                parameters: {},
                onSuccess: callback,
                onFailure: failCallback
        });
        return request;
}

NetworkList.getStatus = function(controller, callback, failCallback) {
	Mojo.Log.info("NetworkList.getStatus {}");
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'getstatus',
		onSuccess: callback,
		onFailure: failCallback 
	});
	return request;
}

NetworkList.discardProfile = function(controller, profile, callback, failCallback) {
	Mojo.Log.info("NetworkList.discardprofile " + NetworkList.printProfile(profile));
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'deleteprofile',
		parameters: {'profileId':profile.profileId},
		onSuccess: callback,
		onFailure: failCallback 
	});
	
	return request;
	
}

NetworkList.getProfile = function(controller, profile, callback, failCallback) {
	Mojo.Log.info("NetworkList.getprofile " + NetworkList.printProfile(profile));
	var request = controller.serviceRequest(NetworkList.identifier, {
		method: 'getprofile',
		parameters: {'profileId':profile.profileId},
		onSuccess: callback,
		onFailure: failCallback 
	});
	
	return request;
	
}
