/*
 *  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.
 */

/**
 * Singleton class to delete contents
 * @private
 */
Ext.define('Ametys.plugins.cms.content.actions.DeleteContentAction', {
	singleton: true,
	
	/**
	 * Action function to be called by the controller.
	 * Will delete the contents registered by the controller.
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	act: function (controller)
	{
		var contentIds = controller.getContentIds();
		if (contentIds.length > 0)
		{
			var contentTitles = this._getContentTitles(controller, contentIds);
			
			Ametys.Msg.confirm("{{i18n CONTENT_DELETE_LABEL}}", 
					"{{i18n CONTENT_DELETE_CONFIRM}}" + contentTitles.join(', ') + ' ?', 
					Ext.bind(this._deleteConfirm, this, [controller, contentIds], 1),
					this
			);
		}
	},
	
	/**
	 * Action function to be called by the controller.
	 * Will force to delete the contents registered by the controller even if they are referenced.
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	forceAct: function (controller)
	{
		var contentIds = controller.getContentIds();
		if (contentIds.length > 0)
		{
			var contentTitles = this._getContentTitles(controller, contentIds);
			
			Ametys.Msg.confirm("{{i18n CONTENT_DELETE_LABEL}}",
					Ext.String.format("{{i18n CONTENT_FORCE_DELETE_CONFIRM}}", contentTitles.join(', ')),
					Ext.bind(this._forceDeleteConfirm, this, [controller, contentIds], 1),
					this
			);
		}
	},
	
	/**
	 * Get the content titles from the matching targets
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling the #act function
	 * @param {String[]} contentIds The content identifiers
	 */
	_getContentTitles: function (controller, contentIds)
	{
		var contentTitles = [];

		var contentTargets = controller.getMatchingTargets();
		Ext.Array.forEach (contentTargets, function (contentTarget) {
			if (Ext.Array.contains (contentIds, contentTarget.getParameters().id))
			{
				contentTitles.push (contentTarget.getParameters().title);
			}
		});
		
		return contentTitles;
	},
	
	/**
	 * Callback function invoked after the #act confirm box is closed
	 * @param {String} answer Id of the button that was clicked
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling the #act function
	 * @param {String[]} contentIds The content identifiers
	 * @private
	 */
	_deleteConfirm: function (answer, controller, contentIds)
	{
		if (answer == 'yes')
		{
			Ametys.cms.content.ContentDAO.trashContents (contentIds, controller.getMatchingTargets());
		}
	},
	
	/**
	 * Callback function invoked after the #forceAct confirm box is closed
	 * @param {String} answer Id of the button that was clicked
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling the #act function
	 * @param {String[]} contentIds The content identifiers
	 * @private
	 */
	_forceDeleteConfirm: function (answer, controller, contentIds)
	{
		if (answer == 'yes')
		{
			Ametys.cms.content.ContentDAO.forceTrashContents (contentIds, controller.getMatchingTargets());
		}
	}
	
});