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

/**
 * Archive menu.
 * @private
 */
Ext.define('Ametys.plugins.cms.content.controller.ArchiveMenu', {
    extend: 'Ametys.plugins.cms.content.controller.SmartContentController',
    
    constructor: function(config)
    {
        this.callParent(arguments);
        
        this._scheduledIconDecorator = this.getInitialConfig('scheduled-icon-decorator');
        
        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();
		}
	},
    
    _calculateStatus: function(targets)
    {
        var result = this.callParent(arguments);

        result["schedule-archive-contents"] = [];

        var scheduledDates = [];
        Ext.Array.each(targets, function(matchingTarget)
        {
            var parameters = matchingTarget.getParameters();
            if (parameters && parameters.content)
            {
                var content = parameters.content;
                if (content.getScheduledArchivingDate() != null)
                {
                    scheduledDates.push(content.getScheduledArchivingDate());

                    var i18nStr = this.getConfig("scheduled-archiving-content-description");
                    var scheduledArchivingDate = content.getScheduledArchivingDate();
                    var formatedDate = Ext.Date.format(new Date(scheduledArchivingDate), "d F Y")
                    var description = Ext.String.format(i18nStr, content.getTitle(), formatedDate);
                    var contentParam = this._getContentDefaultParameters(content);
                    contentParam["description"] = description;
                    result["schedule-archive-contents"].push(contentParam);
                }
            }
        }, this);
        return result;
    },

    /**
     * Update the tooltip description according to the status of the current selection.
     * @param {String} description The initial description. Can be empty.
     * @param {Object} params The received JSON result.
     * @private
     */
    _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, 'workflowaction', params['invalidworkflowaction-contents']);
        description = this._handlingMultiple(description, 'workflowstep', params['invalidworkflowstep-contents']);
        
        var scheduledContents = params['schedule-archive-contents'] || [];
        description = this._handlingMultiple(description, 'scheduled-archiving', scheduledContents);
        
        this.setDescription(description);
        
        this.setIconDecorator(scheduledContents.length > 0 ? this._scheduledIconDecorator : null);
        this.toggle(scheduledContents.length > 0);
    }
});