/*
 *  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 class controls a ribbon button enabled if at least one page on the current selection fills the following conditions:<br/>
 * - page is not a blank page<br/>
 * - page is modifiable<br/>
 * - user has convenient right on page
 * @private
 */
Ext.define('Ametys.plugins.web.page.controller.DeletePageController', {
	extend: 'Ametys.web.controller.WebButtonController',
	
	/**
	 * @property {String[]} [_pageIds=[]] List of identifiers of pages concerned by the action of the controller
	 * @private
	 */
	
	constructor: function(config)
	{
		this.callParent(arguments);
		
		this._pageIds = [];
	},

	updateState: function()
	{
		this._getStatus(this.getMatchingTargets());
	},
	
	/**
	 * Get the matching targets that match set blank page conditions
	 * @return {Ametys.message.MessageTarget[]} targets filling all conditions.
	 */
	getAllRightPageTargets: function ()
	{
		var matchingTargets = this.getMatchingTargets();
		var me = this;
		
		return Ext.Array.filter (matchingTargets, function (target) {
			return Ext.Array.contains(me._pageIds, target.getParameters().id)
		});
	},
	
	/**
	 * Get the lock state of given targets
	 * @param targets The page targets
	 * @private
	 */
	_getStatus: function (targets)
	{
		this.disable();
		
		var pageIds = [];
		for (var i=0; i < targets.length; i++)
		{
			pageIds.push(targets[i].getParameters().id);
		}

		this.serverCall ('getStatus', [pageIds], this._getStatusCb, { errorMessage: true, refreshing: true });
	},
	
	/**
	 * @private
	 * Callback function called after retrieving the status of page targets
	 * @param params The JSON result 
	 */
	_getStatusCb: function (params)
	{
		this._pageIds = [];
		
		var allRightPages = params['allright-pages'];
		if (allRightPages.length > 0)
		{
			for (var i=0; i < allRightPages.length; i++)
			{
				this._pageIds.push(allRightPages[i].id)
			}
			this.enable();
		}
		else
		{
			this.disable();
		}
		
		this._updateTooltipDescription(this.getInitialConfig('description'), params);
	},
	
	/**
	 * @protected
	 * Update the tooltip description according state of the current selection
	 * @param description The initial description. Can be empty.
	 * @param params The JSON result received
	 */
	_updateTooltipDescription: function (description, params)
	{
		description = this._handlingMultiple(description, "noright", params['noright-pages']);
		description = this._handlingMultiple(description, "nomodifiable", params['nomodifiable-pages']);
		
		this.setDescription (description);
	},
	
	/**
	 * @private
	 * Add text to description
	 * @param description The initial description to concatenate. Can be empty.
	 * @param {String} prefix The parameters prefix to used to retrieve the start and end description. The start and end description are retrieved from initial configuration with [prefix]-start-description and [prefix]-end-description
	 * @param {Object[]} pages The concerned pages. If empty, no text will be concatenated
	 */
	_handlingMultiple: function(description, prefix, pages)
	{
		if (pages.length > 0)
		{
			if (description != "")
			{
				description += "<br/><br/>";
			}
			
			description += this.getInitialConfig(prefix + "-start-description");
			for (var i=0; i < pages.length; i++)
			{
				if (i != 0) 
				{
					description += ", ";
				}
				description += pages[i].description;
			}
			description += this.getInitialConfig(prefix + "-end-description");
		}
		
		return description;
	}
});