/*
 *  Copyright 2015 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 does allow the user to perform searches on resources.
 * @private
 */
Ext.define('Ametys.cms.attach.AttachmentsSearchTool', {
    extend: 'Ametys.plugins.explorer.applications.resources.ExplorerSearchTool',
    
    /**
     * @private
     * @property _holderId The id of root element holding the attachments
     */
    
    /**
     * @private
     * @property _holderTitle The title of root element holding the attachments
     */
    
    /**
	 * @private
	 * @property {Ext.Template} _rootHintTpl The template for hint text when the current folder is the root attachments
	 */
    
    setParams: function (params)
	{
		this._holderId = params.holderId;
		this._holderTitle = params.holderTitle;
		
		this._rootHintTpl = this.getRootHintTemplate();
		
		this.callParent(arguments);
	},
	
	getHintTemplate: function ()
	{
		var text = this.getInitialConfig("attach-hint-description") || "{{i18n PLUGINS_CMS_CONTENT_SEARCH_ATTACHMENTS_HINT_TEXT}}";
		return Ext.create('Ext.Template', 
		   Ext.String.format(text, '<strong>{holderTitle}</strong>', '<strong>{name}</strong>')
		);
	},
	
    /**
     * @private
     * Get the template for the hint message at the root of the tree
     * @return {Ext.Template} A non-null template
     * @return {String} return.holderTitle The label of the attachments holder
     */
	getRootHintTemplate: function ()
	{
		var text = this.getInitialConfig("attach-root-hint-description") || "{{i18n PLUGINS_CMS_CONTENT_SEARCH_ATTACHMENTS_ROOT_HINT_TEXT}}";
		return Ext.create('Ext.Template', 
		   Ext.String.format(text, '<strong>{holderTitle}</strong>')
		);
	},
    	
	getHintText: function (data)
	{
		if (data.id == data.rootId)
		{
			return this._rootHintTpl.applyTemplate({
	            holderTitle: this._holderTitle
	        });
		}
		else
		{
			return this._hintTpl.applyTemplate({
	            name: data.name,
	            holderTitle: this._holderTitle
	        });
		}
		
	}
});