/*
 *  Copyright 2011 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 tool allows to search in the content archives.
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tool.SearchArchivesTool', {
    extend: 'Ametys.plugins.cms.search.ContentSearchTool',
    
    contentSearchTabCompatible: false,
    
    constructor: function(config)
    {
    	config.allowAdditionalExtensions = false;
    	config.allowEdition = false;
    	config.allowExportResults = false;
    	
        this.callParent([config]);
        
        Ametys.message.MessageBus.on(Ametys.message.Message.ARCHIVED, this._onContentArchived, this);
        Ametys.message.MessageBus.on(Ametys.message.Message.UNARCHIVED, this._onContentUnarchived, this);
    },
    
    _getResultGridCfg: function(store)
	{
		return { 
			store: this.store,
			
			region: 'center',
			
			stateful: true,
			stateId: this.self.getName() + "$grid",

            allowEdition: false,
			
			columns: [],
			
			messageTarget: this.getMessageTargetRole(),
			
			selModel : {
				mode: 'MULTI'
			},
            
            plugins: [this._getRowRenderPluginCfg()],
			
			viewConfig: {
				loadingText: "{{i18n plugin.cms:UITOOL_SEARCH_WAITING_MESSAGE}}"
			},
			
			listeners: {
				'itemdblclick': {fn: function (view, record, item, index, e) { this.openContent(record); }, scope: this}
			}
		};
	},
    
	getMessageTargetRole: function()
	{
		return Ametys.message.MessageTarget.ARCHIVED_CONTENT;
	},
    
	/**
	 * @protected
	 * Opens the double-clicked archived content. 
	 * @param {Ext.data.Model} record The record to open
	 */
	openContent: function (record)
    {
        var params = {
            'jcr-workspace-name': 'archives',
            'ignore-workflow': 'true',
            'content-message-type': Ametys.message.MessageTarget.ARCHIVED_CONTENT
        };
        
        Ametys.cms.content.EditContentsGrid.openContent (record, { toolParams: params});
    },
    
    /**
     * Listens when contents have been archived.
     * @param {Ametys.message.Message} message The unarchive message.
     * @protected
     */
    _onContentArchived: function(message)
    {
        var targets = message.getTargets(Ametys.message.MessageTarget.CONTENT);
        
        if (targets.length > 0)
        {
            this.showOutOfDate();
        }
    },
    
    /**
     * Listens when contents have been unarchived.
     * Will remove the records if present.
     * @param {Ametys.message.Message} message The unarchive message.
     * @protected
     */
    _onContentUnarchived: function(message)
    {
        var targets = message.getTargets(Ametys.message.MessageTarget.CONTENT);
        
        if (targets.length > 0)
        {
            var toRemove = [];
            for (var i=0; i < targets.length; i++)
            {
                var record = this.store.getById(targets[i].getParameters().id);
                if (record != null)
                {
                    toRemove.push(record);
                }
            }
            
            this.store.remove(toRemove);
        }
    }
    
});