(function() {
	MSGI.namespace('MSGI.campaign');


	//------------------------------------------------------------------------------------------
	// admin tool functions
	//------------------------------------------------------------------------------------------

	MSGI.campaign.stats = function(callback) {
		MSGI.service.send('campaign_getStats', function(response) {
			if (response.error) throw(response.error.message || response.error);
			callback(response.result);
		});
	};
	
	MSGI.campaign.search = function(params, callback) {
		MSGI.service.send('campaign_search', params, function(response){
			if (response.error) throw(response.error.message || response.error);
			callback(response.result);
		});
	};

	MSGI.campaign.create = function(campaign, callback) {
		MSGI.service.send('campaign_create', campaign, callback);
	};

	MSGI.campaign.update = function(campaign, callback) {
		MSGI.service.send('campaign_update', campaign, callback);
	};

	MSGI.campaign.getDetailsByID = function(id, callback) {
		MSGI.service.send('campaign_getDetails', { 'id': id.toString(), 'is_admin': 'true' }, function(response) {
			if (response.error) throw(response.error.message || response.error);
			callback(response.result);
		});
	}


	//------------------------------------------------------------------------------------------
	// public site functions
	//------------------------------------------------------------------------------------------

	/**
	 * Track an action made by a user while participating in a referral campaign.
	 * @param params {object} params to be tracked - see API docs for more info (http://is.gd/dbLc7)
	 */
	MSGI.campaign.track = function(params, callback) {
		callback = callback || function() {};
		MSGI.service.send('campaign_track', params, function(response) {
			if (response.error) throw('Error returned by campaign_track service: ' + response.error);
			callback();
		});
	};
	
	MSGI.campaign.getDetailsBySlug = function(slug, callback) {
		MSGI.service.send('campaign_getDetails', { 'slug': slug }, function(response) {
			var campaignIsOver = (response.error == -2);
			callback(response.result, campaignIsOver);
		});
	}

	/**
	 * Get a referral code that is unique to this campaign and this subscriber
	 * @param id {integer} campaign ID (subscriber's ID is inferred from login cookie)
	 * @param callback {function} callback function
	 */
	MSGI.campaign.getReferralCode = function(id, callback) {
		MSGI.service.send('campaign_getReferralCode', { 'id': id.toString() }, function(response) {
			var campaignIsOver = (response.error == -2);
			callback(response.result, campaignIsOver);
		});
	}
	
	/**
	 * Send referral emails on behalf of the currently logged-in subscriber
	 * @param referral_id {integer} ID referencing a unique combination of a campaign and a subscriber
	 * @param recipients {string} comma-separated list of email addresses
	 * @param body {{string} body of the email message to be sent
	 * @param callback {function} callback function
	 */
	MSGI.campaign.sendReferralEmails = function(referral_id, recipients, message, callback) {
		var params = {
			'ref_id': referral_id.toString(), 
			'referral_emails': recipients,
			'message': message
		};
 
		MSGI.service.send('campaign_sendReferralEmails', params, function(response) {
			// this service returns a result (*not* an error) of -3 to mean that 1 or more delivery errors occurred
			var deliveryErrors = (response.result == -3);
			var error, undeliverables;
			
			if (deliveryErrors) {
				// in this flow, the 'error' field is set to a comma-delimited list of undeliverable emails
				undeliverables = response.error;
				error = true;
			} else if (response.result == 'success') {
				error = false;
			} else {
				error = true;
			}

			callback(error, undeliverables);
		});
	}
	
	/**
	 * Get info about a campaign and referring subscriber
	 * @param id {integer} referral ID, representing the unique combination of a subscriber and a campaign
	 * @param callback {function} callback function
	 */
	MSGI.campaign.getReferrerInfo = function(id, callback) {
		MSGI.service.send('campaign_getReferrerInfo', { 'ref_id': id.toString() }, function(response) {
			var campaignIsOver = (response.error == -2);
			callback(response.result, campaignIsOver);
		});
	}

	/**
	 * Get info about the each of the campaigns the subscriber is associated with
	 * @param callback {function} callback function
	 */
	MSGI.campaign.getSubscriberStats = function(callback) {
		MSGI.service.send('campaign_getSubscriberStats', function(response) {
			if (response.error) throw(response.error.message || response.error);
			callback(response.result && response.result.data);
		});
	}

})();


