/**
* Wrappers to JSON services that interact with the SalesforceBridge.
*
* All methods are defined as functions off of the global object SalesforceBridge.
*
*
* A number of the methods return the same type of object. The common objects are described here.
*
*
* Product
*	assetId		- id of a Salesforce asset record.
*	productId	- id of a Salesforce product2 record.
*
* Contact
*	contactId	- id of a Salesforce contact record.
*/

var SalesforceBridge = new Object();
SalesforceBridge.serviceRoot = "/superman/SalesforceBridge/";


/**
* Record an event that caused by a user registered with ExpressionEngine.
*
* @param session -- unique EE session id.
* @param property - name of the web property on which the event occurred.
* @param eventType -- type of event
* @param eventSubType
* @param eventData
* @param onComplete(status) called when the function completes.
*/
SalesforceBridge.recordRegisteredEvent = function( session, property, eventType, eventSubtype, eventData, onComplete ) {
	$.getJSON(SalesforceBridge.serviceRoot + "RecordRegisteredEvent",
		{
			'session': session,
			'property': property,
			'type': eventType,
			'subtype': eventSubtype,
			'data': eventData
		},
		onComplete
	);
};

/**
* Record an event that caused by a user that is not logged into Expression Engine.
*
* @param ip-- orignating IP of the user.
* @param webProperty -- name of the certara web site that generated the event.
* @param eventType -- type of event
* @param eventSubType
* @param eventData
* @param onComplete(status) called when the function completes.
*/
SalesforceBridge.recordAnonymousEvent = function( ip, webProperty, eventType, eventSubType, eventData, onComplete) {
	$.getJSON(SalesforceBridge.serviceRoot + "RecordAnonymousEvent",
		{
			'ip': ip,
			'property': webProperty,
			'type': eventType,
			'subtype': eventSubType,
			'data': eventData
		},
		onComplete
	);
};

/**
* Find a list of records that can be found using a single query parameter.
*
* @param serviceEndPoint - JSON searching service.
* @param keyName - name of the query parameter
* @param key - the query parameter.
* @param onSuccess( key, Record[])  -- if records are found call this method.
* @param onFailure( key )  -- if no records are found call this method.
*/
SalesforceBridge.findRecordsByKey = function( serviceEndPoint, keyName, key, onSuccess, onFailure ) {
	var mySuccess = function( data, status, xhr ) {
		
		if( !data || 0==data.length ) {
			if( onFailure ) { onFailure(key); }
			return;
		}
		
		if( onSuccess) { onSuccess(key, data); }

	};

	var params = {};
	params[keyName] = key;
	$.getJSON(serviceEndPoint, params, mySuccess);

};

/**
* Find a single record that can be found using a single query parameter.
*
* @param serviceEndPoint - JSON searching service.
* @param keyName - name of the query parameter
* @param key - the query parameter.
* @param onSuccess( key, Record)  -- if a record is found call this method.
* @param onFailure( key )  -- if no record is found call this method.
*/
SalesforceBridge.findSingleRecordByKey = function( serviceEndPoint, keyName, key, onSuccess, onFailure ) {
	var mySuccess = function( data, status, xhr ) {
		
		if( !data || 0==data.length ) {
			if( onFailure ) { onFailure(key); }
			return;
		}
		
		if( onSuccess) { onSuccess(key, data); }

	};

	var params = {};
	params[keyName] = key;
	$.getJSON(serviceEndPoint, params, mySuccess);

};

/**
* Find a contact record based on an email address.
*
* @param email -- look for a contact that unique maps to this email address.
* @param onSuccess( email, ContactRecord)
* @param onFailure(email)
*/
SalesforceBridge.findContactByEmail = function( email, onSuccess, onFailure ) {

	SalesforceBridge.findSingleRecordByKey(SalesforceBridge.serviceRoot + "FindContact", "email", email, onSuccess, onFailure );

};

/**
* Find a contact record based on an Expression Engine User Id.
*
* @param eeId-- look for a contact that unique maps to this email address.
* @param onSucess( eeUserId, ContactRecord)
* @param onFailure( eeUserId )
*/
SalesforceBridge.findContactByUserId = function( eeUserId, onSuccess, onFailure ) {

	SalesforceBridge.findSingleRecordByKey(SalesforceBridge.serviceRoot + "FindContact", "userId", eeUserId, onSuccess, onFailure );
	
};




/**
* Find contacts associated with a Flex host id.
*
* @param hostId -- look for products owned by this host id.
* @param onSuccess(hostId, records) where records is an array of Contactrecords.
* @param onFailure( hostId ) -- called if no contacts are found.
*/
SalesforceBridge.findContactsByHostId = function( hostId, onSuccess, onFailure ) {
	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindContactsByHostId", 'id', hostId, onSuccess, onFailure );

};

/**
* Find contacts associated with a Sentinel Authorization code.
*
* @param authCode -- look for products owned by this authorization code,
* @param onSuccess(authCode , records) where records is an array of Contactrecords.
* @param onFailure( authCode  ) -- called if no contacts are found.
*/
SalesforceBridge.findContactsByAuthCode = function(authCode , onSuccess, onFailure ) {

	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindContactsByAuthCode", 'code', authCode, onSuccess, onFailure );

};

/**
* Find products that are owned and active for a specific salesforce contact.
*
* @param contactId -- look for products owned by this contact.
* @param onSuccess(contactId, records) where records is an array of Product records.
* @param onFailure( contactId) -- called if no products are found.
*/
SalesforceBridge.findProductsByContact = function( contactId, onSuccess, onFailure ) {

	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindProductsByContact", 'id', contactId, onSuccess, onFailure );

};

/**
* Find products that are owned and active for a Flex hostId.
*
* @param hostId -- look for products using this Flex host id.
* @param onSuccess(contactId, records) where records is an array of Product records.
* @param onFailure( contactId) -- called if no products are found.
*/
SalesforceBridge.findProductsByHostId = function( hostId, onSuccess, onFailure ) {
	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindProductsByHostId", 'id', hostId, onSuccess, onFailure );

};

/**
* Find products that are owned and active for a Sentinal authorization code.
*
* @param authCode-- look for products using this Sentinal auth code.
* @param onSuccess(contactId, records) where records is an array of Product records.
* @param onFailure( contactId) -- called if no products are found.
*/
SalesforceBridge.findProductsByAuthCode = function( authCode, onSuccess, onFailure ) {

	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindProductsByAuthCode", 'code', authCode, onSuccess, onFailure );

};

/**
* Find release families accessible to an EE session id.
*
* @param sessionId -- look for release families accessible by this EE session id
* @param onSuccess(sessionId, records) where records is an array of release family names
* @param onFailure( sessionId ) -- if no release families are accessible to the caller.
*/
SalesforceBridge.findReleaseFamilies = function( sessionId, onSuccess, onFailure ) {
	SalesforceBridge.findRecordsByKey( SalesforceBridge.serviceRoot + "FindReleaseFamilies", 'id', sessionId, onSuccess, onFailure );

};


