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


	//------------------------------------------------------------------------------------------
	// basic information
	//------------------------------------------------------------------------------------------
		
	MSGI.channel.getInfo = function(slug, callback) {
		MSGI.service.send("channel_getInfo", { 'slug': slug }, function(response) {
			if (response.error) throw(response.error.message || response.error.toString());
			callback(response.result && response.result.id);
		});
	};
	
	MSGI.channel.getCurrent = (function() {
		var slug = MSGI.getChannelSlugFromURL();
		var cookieName = 'channel[' + slug +']'; // support edge case of user visiting multiple channels in one session
		
		function readCookie() { return $.cookie(cookieName); }
		function saveCookie(value) { return $.cookie(cookieName, value); }

		return function(callback) {
			var id = readCookie();
			if (id) return callback(id);
						
			MSGI.channel.getInfo(slug, function(id) {
				saveCookie(id);
				callback(id);
			});
		};
	})();

	/**
	 * Get channel details
	 * @param id {number} channel_id
	 * @param callback {function} callback function is provided with result object
	 * @throws error.message.
	 */
	MSGI.channel.getDetails = function(id, callback) {
		MSGI.service.send('channel_getDetails', { 'id': id.toString() }, function(response) {
			if (response.error) throw(response.error.message);
			callback(response.result);
		});
	}
	
	/**
	 * Searches channels
	 * @param params {object} channel_search parameters for service API
	 * @param callback {function} callback function is provided with result object {data:[],total:100}
	 * @throws error.message.
	 */
	MSGI.channel.search = (function() {
		return function(params, callback) {
			MSGI.service.send('channel_search', params, function(channelResponse){
				if (!channelResponse.error) return callback(channelResponse.result);
				throw(channelResponse.error.message);
			});
		};
	})();
	


	//------------------------------------------------------------------------------------------
	// create / update / delete
	//------------------------------------------------------------------------------------------
	
	/**
	 * Creates channel.
	 * @param params {object} Channel create object {name:'Kicks',slug:'kick'}.
	 * @param callback {function} callback function is provided with result object {success:[],failed:null}
	 * @throws error.message.
	 */
	MSGI.channel.create = (function() {
		return function(params, callback) {
			MSGI.service.send('channel_create', params, function(channelResponse){
				callback(channelResponse);
			});
		};
	})();
		
	/**
	 * Updates channel.
	 * @param params {object} Channel update object {name:'Kicks',slug:'kick'}.
	 * @param callback {function} callback function is provided with result object {success:[],failed:null}
	 * @throws error.message.
	 */
	MSGI.channel.update = (function() {
		return function(params, callback) {
			MSGI.service.send('channel_update', params, function(channelResponse){
				callback(channelResponse);
			});
		};
	})();
	
	/**
	 * Deletes channels.
	 * @param ids {array} Array of ids to be deleted.
	 * @param callback {function} callback function is provided with result object {success:[],failed:null}
	 * @throws error.message.
	 */
	MSGI.channel.remove = function(ids, callback) {
		MSGI.service.send('channel_delete', ids, function(channelResponse){
			if (!channelResponse.error) return callback(channelResponse.result);
			throw(channelResponse.error.message);
		});
	}
	
	MSGI.channel.removeWithPrompt = function(ids, callback) {
		if (ids.length && confirm('Are you sure you want to delete ' + (ids.length == 1 ?  'this item' : (ids.length + ' items')) +'?')) {
			MSGI.channel.remove(ids, callback);
		}
	}


		
	//------------------------------------------------------------------------------------------
	// Categories & Locations
	//------------------------------------------------------------------------------------------

	MSGI.channel.getCategories = (function() {
		function getCategories(id, callback) {
			MSGI.service.send('channel_getCategories', { 'channel_id': id.toString() }, function(response) {
				if (response.error) throw(response.error.message);
				callback(response.result);
			});
		}

		return function(id, callback) {
			// MSGI.channel.getCategories(id, callback)
			// gets categories for a given channel
			if (arguments.length == 2) return getCategories(id, callback);
			
			// MSGI.channel.getCategories(callback)
			// gets categories for current channel
			callback = arguments[0];
			MSGI.channel.getCurrent(function(current) {
				getCategories(current, callback);
			});
		};
	})();

	// create an alternate version of this function with caching enabled
	MSGI.channel.getCategories.fromCache = enableCaching(MSGI.channel.getCategories);
		

	MSGI.channel.getViewAllCategories = (function() {
		function getCategories(id, callback) {
			MSGI.service.send('channel_getViewAllCategories', { 'channel_id': id.toString() }, function(response) {
				if (response.error) throw(response.error.message);
				callback(response.result);
			});
		}

		return function(id, callback) {
			// MSGI.channel.getViewAllCategories(id, callback)
			// gets categories for a given channel
			if (arguments.length == 2) return getCategories(id, callback);
			
			// MSGI.channel.getViewAllCategories(callback)
			// gets categories for current channel
			callback = arguments[0];
			MSGI.channel.getCurrent(function(current) {
				getCategories(current, callback);
			});
		};
	})();

	// create an alternate version of this function with caching enabled
	MSGI.channel.getViewAllCategories.fromCache = enableCaching(MSGI.channel.getViewAllCategories);

	MSGI.channel.getLocations = (function() {
		function fetch(id, callback) {
			MSGI.service.send('channel_getLocations', { 'channel_id': id.toString() }, function(response) {
				if (response.error) throw(response.error.message);
				callback(response.result);
			});
		}

		return function(id, callback) {
			// getLocations(id, callback)
			if (callback) return fetch(id, callback);
			
			// getLocations(callback) -> gets locations for current channel
			callback = id;
			MSGI.channel.getCurrent(function(currentChannel) {
				fetch(currentChannel, callback);
			});
		};
	})();
	
	

	//------------------------------------------------------------------------------------------
	// Sweepstakes
	//------------------------------------------------------------------------------------------
	
	MSGI.channel.getSweepstakes = (function() {
		function fetch(id, callback) {
			MSGI.service.send('channel_getSweepstakes', { 'channel_id': id.toString() }, function(response) {
				if (response.error) throw(response.error.message);
				callback(response.result);
			});
		}

		return function(id, callback) {
			// getSweepstakes(id, callback)
			if (callback) return fetch(id, callback);
			
			// getSweepstakes(callback) -> gets locations for current channel
			callback = id;
			MSGI.channel.getCurrent(function(currentChannel) {
				fetch(currentChannel, callback);
			});
		};
	})();
	
	MSGI.channel.getActiveSweepstakes = (function() {
		function fetch(id, callback) {
			MSGI.service.send('channel_getActiveSweepstakes', { 'channel_id': id.toString() }, function(response) {
				if (response.error) throw(response.error.message);
				callback(response.result);
			});
		}

		return function(id, callback) {
			// getActiveSweepstakes(id, callback)
			if (callback) return fetch(id, callback);
			
			// getActiveSweepstakes(callback) -> gets locations for current channel
			callback = id;
			MSGI.channel.getCurrent(function(currentChannel) {
				fetch(currentChannel, callback);
			});
		};
	})();

	MSGI.channel.updateSweepstakes = function(params, callback) {
		MSGI.service.send('channel_updateSweepstakes', params, function(response) {
			if (response.error) throw(response.error.message);
			callback(response.result);
		});
	}
	

	
	//------------------------------------------------------------------------------------------
	// private functions
	//------------------------------------------------------------------------------------------

	function enableCaching(fetch) {
		var cached;
		return function(callback) {
			if (cached) return callback(cached);
			fetch(function(response) {
				cached = response;
				callback(response);
			});
		};
	}

})();


