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

/**
 * Override of {@link Ametys.plugins.cms.tag.TagsTreePanel} to be able to handle sites
 * @private
 */
Ext.define('Ametys.plugins.web.tag.TagsTreePanel', {
    extend: 'Ametys.plugins.cms.tag.TagsTreePanel',
    
    /**
     * @cfg {String} [plugin="web"] The plugin for the store.
     */
    plugin: "web",
    
    /**
     * @private
     * @property {String} _siteName the name of the site currently selected
     */
    
    /**
     * @cfg {String} defaultSiteName The default site name. Defaults to the current site name
     */
    
    /**
     * @cfg {String} [siteContext=Ametys.web.helper.ContextToolbar.SITE_CONTEXT_CURRENT] The site context. 
     * Valid contexts are: 
     * <ul>
     *      <li>Ametys.web.helper.ContextToolbar.SITE_CONTEXT_CURRENT for current site only</li>
     *      <li>Ametys.web.helper.ContextToolbar.SITE_CONTEXT_ALL for all sites</li>
     * </ul>
     */
    
    constructor: function(config) 
    {
        config.defaultSiteName =  config.defaultSiteName || Ametys.getAppParameter("siteName");
        config.siteContext =  config.siteContext || Ametys.web.helper.ContextToolbar.SITE_CONTEXT_CURRENT;
        
        this.callParent(arguments);
    },
    
    /**
     * Get the selected site for the tags
     * @return {String} the selected site for the tags
     */
    getSiteName: function()
    {
    	return this._siteName || this.defaultSiteName;
    },
    
    /**
     * @protected
     * Get the docked items
     * @param {Object} config The initial tree panel configuration
     * @return {Object[]} The docked items configuration
     */
    _getDockedItems: function(config)
    {
    	var dockedItems = [];
    	if (config.siteContext == Ametys.web.helper.ContextToolbar.SITE_CONTEXT_ALL)
		{
     		var toolbarCfg = {
    			onSelectSiteFn: Ext.bind(this._onSelectSite, this),
				siteContext: config.siteContext
    		};
    		
    		dockedItems.push(Ametys.web.helper.ContextToolbar.getToolbar(toolbarCfg));
		}
    	
    	return Ext.Array.merge(dockedItems, this.callParent(arguments));
    },
    
    /**
     * Initialize the site in which look for the tags
     */
    initialize: function()
    {
    	if (this.siteContext == Ametys.web.helper.ContextToolbar.SITE_CONTEXT_ALL)
    	{
    		// Load available sites
    		this.down("combobox[itemId='sites-combo']").getStore().load({callback: Ext.bind(this._loadSitesCb, this)});
    	}
    },
    
    /**
     * @private
     * Callback function invoked after the sites combo box is loaded, in order to initialize the site context
     */
    _loadSitesCb: function ()
    {
    	this.down("combobox[itemId='sites-combo']").setValue(this.defaultSiteName);
    },
    
    /**
	 * @private
	 * Listener on site selection
	 * @param {Ext.form.field.ComboBox} combo The sites' ComboBox 
	 * @param {Ext.data.Model} record the selected record
	 */
	_onSelectSite: function (combo, record)
	{
		var value = combo.getValue();
		
		if (this._siteName == value)
		{
			// No change
			return;
		}
		
		this._siteName = value;
		
		this.contextualParameters = Ext.apply (this.contextualParameters, 
		{
			siteName: this._siteName
		});
		
		// Refresh tree
		this.getStore().reload();
	}
});