/*
 *  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 page of redirection<br/>
 * - page is modifiable<br/>
 * - user has convenient right on page
 * @private
 */
Ext.define('Ametys.plugins.web.page.controller.AddContentController', {
	extend: 'Ametys.web.controller.WebButtonController',
	
	updateState: function()
	{
		this._getStatus(this.getMatchingTargets());
	},
	
	/**
	 * Get the authorized content types for creation for selected page and zone.
	 * @param targets The page targets
	 * @private
	 */
	_getStatus: function (targets)
	{
		this.disable();
		
		var target = targets[0];
		var pageId = targets[0].getParameters().id;
		var zoneName = targets[0].getParameters()['zone-name'];
		var contentLanguage = targets[0].getParameters().lang;
		
		this.serverCall ('getAvailableContentTypes', [pageId, zoneName], this._getAvailableContentTypesCb, { errorMessage: true, refreshing: true, arguments: {contentLanguage: contentLanguage} });
	},
	
	/**
	 * @private
	 * Callback function called after retrieving the available content types
	 * @param {String[]} contentTypes The authorized content types for creation
	 * @param {Object} args The callback arguments
	 */
	_getAvailableContentTypesCb: function (contentTypes, args)
	{
		var atLeastOneContentType = false;
        
        this._getGalleries().each(function (gallery) {
            var atLeastOneAvailable = false;
            
            gallery.items.each (function (item) {
                var elmt = Ametys.ribbon.RibbonManager.getUI(item.controlId);
                
                var available = Ext.Array.contains(contentTypes, elmt.contentTypes);
                item.setVisible(available);
                
                atLeastOneAvailable = atLeastOneAvailable || available
                atLeastOneContentType = available || atLeastOneContentType;
            });
            
            atLeastOneAvailable ? gallery.show() : gallery.hide();
        });

        if (atLeastOneContentType)
		{
			this.enable();
			this.setAdditionalDescription('');
		}
		else
		{
			this.disable();
			this.setAdditionalDescription(this.getInitialConfig("no-contenttype-description"));
		}
	},
    
    areSameTargets: function (target1, target2)
    {
        var match = this.callParent(arguments);
        return match && target1.getParameters()['zone-name'] == target2.getParameters()['zone-name'];
    }
});