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

/**
 * @private
 * This UI helper provides a toolbar to select a site and or a sitemap as a search context.
 */
Ext.define('Ametys.web.helper.ContextToolbar', {
    
	singleton: true,
    
	/**
	 * @property {String} [SITE_CONTEXT_CURRENT="current"] Context of site for using current site only.
	 */
	SITE_CONTEXT_CURRENT: "current",
	/**
	 * @property {String} [SITE_CONTEXT_ALL="all"] Context of site for choosing site in a combo box of available sites.
	 */
	SITE_CONTEXT_ALL: "all",
	/**
	 * @property {String} [SITEMAP_CONTEXT_ALL="all"] Context of sitemap for choosing language in a combo box of available languages.
	 */
	SITEMAP_CONTEXT_ALL: "all",
	
    /**
     * Builds and return a {@link Ext.toolbar.Toolbar} allowing to choose the site / sitemap context to use, according to the given configuration
     * @param {Object} config the toolbar's configuration
     * @param {String} [config.siteContext] the optional site context for the toolbar. If not given, the sites combo box will not be drawn.
     * @param {Function} [config.onSelectSiteFn] the function called whenever a site is selected from the sites combo box. Can be null.
     * @param {String} [config.sitemapContext] the optional sitemap context for the toolbar. If not given, the sitemaps combo box will not be drawn.
     * @param {Function} [config.onSelectSitemapFn] the function called whenever a sitemap is selected from the sitemaps combo box. Can be null.
     * @return {Ext.toolbar.Toolbar} the toolbar
     */
	getToolbar: function(config)
    {
		var items = [];
		if (config.siteContext == Ametys.web.helper.ContextToolbar.SITE_CONTEXT_ALL)
		{
			// Add site combobox
			var sitesCombo = Ext.create('Ext.form.field.ComboBox', Ext.apply(this._getSitesComboConfig(), {
				flex: .6, 
				style: { marginRight: '6px' }
			}));
			
			if (config.onSelectSiteFn)
			{
				sitesCombo.on('select', config.onSelectSiteFn);
			}
			
			items.push(sitesCombo);
		}
		
		if (config.sitemapContext == Ametys.web.helper.ContextToolbar.SITEMAP_CONTEXT_ALL)
		{
			// Add sitemap combobox
			var sitemapsCombo = Ext.create('Ext.form.field.ComboBox', Ext.apply(this._getSitemapsComboConfig(), {
				flex: .4
			}));
			
			sitemapsCombo.getStore().on('beforeload', function(store, operation) {
				var siteName = sitesCombo ? sitesCombo.getValue() : config.defaultSiteName;
				siteName = siteName || Ametys.getAppParameter('siteName');
				
				operation.setParams(Ext.apply(operation.getParams() || {}, {
					siteName: siteName
				}));
			});
			
			if (config.onSelectSitemapFn)
			{
				sitemapsCombo.on('select', config.onSelectSitemapFn);
			}
			
			items.push(sitemapsCombo);
		}
		
		return Ext.create('Ext.toolbar.Toolbar', {
			dock: config.dock || 'top',
			xtype: 'toolbar',
			layout: {
		        type: 'hbox',
		        align: 'stretch'
		    },
			
		    border: false,
			defaults : {
				cls: 'ametys',
				labelWidth: 55,
				labelSeparator: ''
			},
			
			items: items
		});
    },
    
    /**
	 * @private
	 * Get configuration for sites combo box
	 * @return {Object} the sites combo box configuration object
	 */
	_getSitesComboConfig: function ()
	{
		var store = Ext.create('Ext.data.Store', {
			model: 'Ametys.web.site.SitesTree.Site',
			
	        proxy: {
	        	type: 'ametys',
				plugin: 'web',
				url: 'repository/sites.json',
	        	reader: {
	        		type: 'json',
					rootProperty: 'sites'
	        	}
	        }
		});
		
		return {
			xtype: 'combobox',
			itemId: 'sites-combo',
			
			forceSelection: true,
	    	triggerAction: 'all',
	    	queryMode: 'local',
	    	editable: true,
	    	name: 'sitename',
	    	
	    	fieldLabel: "{{i18n PLUGINS_WEB_TOOL_SITEMAP_SITEMAPTOOL_SITE}}",
	    	labelWidth: 40,
	    	
	    	store: store,
	        valueField: 'name',
	        displayField: 'text',
	        iconClsField: 'type',
	        
	        tpl: Ext.create('Ext.XTemplate',
	        		 '<ul class="x-list-plain">',
	        		 	'<tpl for=".">',
	        		 		'<li role="option" class="x-boundlist-item site-item {type} level-{depth}">{title} <em>({name})</em></li>',
	        		 	'</tpl>',
	        		 '</ul>')
		}
	},
	
	/**
	 * @private
	 * Get the languages combo box
	 * @return {Object} the sitemaps combo box configuration object
	 */
	_getSitemapsComboConfig: function ()
	{
		var store = Ext.create('Ext.data.Store', {
			model: 'Ametys.web.sitemap.SitemapTree.Sitemap',
			
	        proxy: {
	        	type: 'ametys',
				plugin: 'web',
				url: 'repository/languages.xml',
	        	reader: {
	        		type: 'xml',
	        		rootProperty: 'languages',
					record: 'language'
	        	}
	        },
	        
	        sorters: [{property: 'label', direction: "ASC" }],
		});
		
		return {
			xtype: 'combobox',
			itemId: 'sitemaps-combo',
			
	    	forceSelection: true,
	    	editable: false,
	    	triggerAction: 'all',
	    	queryMode: 'local',
	    	 
	    	fieldLabel: "{{i18n PLUGINS_WEB_TOOL_SITEMAP_SITEMAPTOOL_LANGUAGE}}",
	    	
	    	store: store,
	    	
	    	name: 'sitemap',
	        valueField: 'name',
	        displayField: 'fulllabel',
	        
	        tpl: Ext.create('Ext.XTemplate',
	        		 '<ul class="x-list-plain">',
	        		 	'<tpl for=".">',
	        		 		'<li role="option" class="x-boundlist-item lang-item">{label} <em>({name})</em></li>',
	        		 	'</tpl>',
	        		 '</ul>')
		};
	}
});