/*
 *  Copyright 2013 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * This factory creates Ametys.message.MessageTarget for a content.
 * 
 * See #createTargets to know more.
 * @private
 */
Ext.define("Ametys.plugins.cms.content.ContentMessageTargetFactory",
	{
		extend: "Ametys.message.factory.DefaultMessageTargetFactory",

		statics: {
			/**
			 * Create the target from a {@link Ametys.cms.content.Content}
			 * @param {String} targetId The id of target
			 * @param {Ametys.cms.content.Content} content The content
			 * @param [parameters] The additional target's parameters. Can be null or empty.
			 * @returns the content target
			 */
			createTarget: function (targetId, content, parameters)
			{
				return Ext.create("Ametys.message.MessageTarget", {
					id: targetId,
					parameters: Ext.merge(content.getProperties(parameters || {}), {content: content})
				})
			}
		},
		
		/**
		 * Create the targets for a message
		 * @param {Object} parameters The parameters needed by the factory to create the message. Can not be null. Handled elements are
		 * @param {String[]} parameters.ids The content's identifiers. Must be present if contents is empty
		 * @param {Ametys.cms.content.Content[]} parameters.contents The contents themselves. Must be present if ids is empty
		 * @param {Function} callback The callback function called when the targets are created. Parameters are
		 * @param {Ametys.message.MessageTarget[]} callback.targets The targets created. Cannot be null.
         * @param {String} targetId The id of target specified. Useful for gerneric MessageTargetFactories.
		 */
		createTargets: function(parameters, callback, targetId)
		{
			var targets = [];
			
			if (parameters.ids)
			{
				Ametys.cms.content.ContentDAO.getContents (parameters.ids, Ext.bind(this._createTargets, this, [callback, parameters], true));
			}
			else if (parameters.contents)
			{
				this._createTargets (parameters.contents, callback, parameters);
			}
		},

		/**
		 * Create the content targets
		 * @param {Ametys.cms.content.Content[]} contents The contents
		 * @param {Function} callback The callback function called when the targets are created. Parameters are
		 * @param {Ametys.message.MessageTarget[]} callback.targets The targets created. Cannot be null.
		 * @param {Object} parameters The initial parameters of the #createTargets method
		 * @private
		 */
		_createTargets: function (contents, callback, parameters)
		{
			delete parameters['ids'];
			delete parameters['contents'];
			
			var targets = [];
			
			for (var i=0; i < contents.length; i++)
			{
				targets.push(Ametys.plugins.cms.content.ContentMessageTargetFactory.createTarget(this.getId(), contents[i], parameters));
			}
				
			callback(targets);
		}
	}
);

Ext.define("Ametys.message.ContentMessageTarget",
	{
		override: "Ametys.message.MessageTarget",
		
		statics: 
		{
			/**
			 * @member Ametys.message.MessageTarget
			 * @readonly
			 * @property {String} CONTENT The target type is a content. See Ametys.plugins.cms.content.ContentMessageTargetFactory parameters to know more of the associated parameters. 
			 */
			CONTENT: "content",
			
			/**
			 * @member Ametys.message.MessageTarget
			 * @readonly
			 * @property {String} REFERENCE_TABLE_CONTENT The target type is a entry of a reference table. See Ametys.plugins.cms.content.ContentMessageTargetFactory parameters to know more of the associated parameters. 
			 */
			REFERENCE_TABLE_CONTENT: "reference-table-content",
			
			/**
			 * @member Ametys.message.MessageTarget
			 * @readonly
			 * @property {String} ARCHIVED_CONTENT The target type is a archived content. See Ametys.plugins.cms.content.ContentMessageTargetFactory parameters to know more of the associated parameters. 
			 */
			ARCHIVED_CONTENT: "archived-content",
			
			/**
			 * @member Ametys.message.MessageTarget
			 * @readonly
			 * @property {String} COMMENT The target type is a content's comment. The expected parameters are:
             * @property {String} COMMENT.id The comment identifier
             * @property {String} COMMENT.content the content identifier
             * @property {Number} COMMENT.reportsCount The number of reports on this comment
			 */
			COMMENT: "comment"
		}
	}
);