/*
 *  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 representing the alerts of a content
 * @private
 */
Ext.define('Ametys.plugins.cms.content.controller.AlertsController', {
	extend: 'Ametys.plugins.cms.content.controller.SmartContentController',
	
	constructor: function(config)
	{
		this.callParent(arguments);
		
		Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this._onModified, this);
	},

	/**
	 * Listener when the content has been modified
	 * Will update the state of the buttons effectively upon the current selection.
	 * @param {Ametys.message.Message} message The modified message.
	 * @protected
	 */
	_onModified: function (message)
	{
        if (this.updateTargetsInCurrentSelectionTargets(message))
        {
            this.refresh();
        }
	},
    
    _getStatus: function(targets)
    {
        var contentIds = [];
        for (var i=0; i < targets.length; i++)
        {
            contentIds.push(targets[i].getParameters().id);
        }
        
        this.serverCall('getStatus', [contentIds], this._getStatusCb, {arguments: targets, cancelCode: this.self.getName() + "$" + this.getId(), refreshing: true});
    },
    
	/**
	 * @private
	 * Callback function called after retrieving the 'alert' state of content targets
	 * @param params The JSON result 
	 */
	_getStatusCb: function (params, targets)
	{
        this.callParent(arguments);
        
        // Toggle element if at least one content has an active alert
        var contentsWithAlert = params['alert-enabled-contents'] || [];
		this.toggle(contentsWithAlert.length > 0);
	},
    
    /**
     * @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, 'allright', params['allright-contents']);
        description = this._handlingMultiple (description, 'noread', params['noread-contents']);
        description = this._handlingMultiple(description, "locked", params['locked-contents']);
        description = this._handlingMultiple(description, "noright", params['noright-contents']);
        description = this._handlingMultiple(description, "nomodifiable", params['unmodifiable-contents']);
        description = this._handlingMultiple(description, "alerts-enabled", params['alert-enabled-contents']);
        description = this._handlingMultiple(description, "alerts-disabled", params['alert-disabled-contents']);
        
        this.setDescription (description);
    }
	
});