/*
 *  Copyright 2013 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 class controls a ribbon gallery for thesaurus.
 * @private
 */
Ext.define('Ametys.plugins.thesaurus.controller.ThesaurusGalleryController', {
	extend: 'Ametys.ribbon.element.ui.ButtonController',
	
    _getMenuPanel: function()
    {
        var me = this;
        var menuPanelWrapper = this.callParent(arguments);
        
        var thesaurii = this.getInitialConfig("thesaurii")
        if (thesaurii.length > 0)
        {
            var gpItems = [];
            
            for (var i = 0; i < thesaurii.length; i++)
            {
                var itemConfig = thesaurii[i];
                itemConfig.pluginName = me.getPluginName();
                
                var buttonElement = Ext.create("Ametys.ui.fluent.ribbon.controls.Button", Ext.apply({
                    text: itemConfig.label,
                    tooltip: {
                        title: itemConfig.label,
                        glyphIcon: itemConfig['icon-glyph'],
                        iconDecorator: itemConfig['icon-decorator'],
                        image: itemConfig['icon-glyph'] ? null : Ametys.CONTEXT_PATH + itemConfig['icon-large'],
                        imageWidth: 48,
                        imageHeight: 48,
                        text: itemConfig.description,
                        help: itemConfig.help,
                        inribbon: false
                    },
                    
                    iconCls: itemConfig['icon-glyph'] + (itemConfig['icon-decorator'] ? ' ' + itemConfig['icon-decorator'] : ''),
                    icon: !itemConfig['icon-glyph'] ? Ametys.CONTEXT_PATH + itemConfig['icon-medium'] : null,
                    scale: 'large',
                    
                    disabled: false,
                    controlId: me.getId(),
                    thesaurusId: itemConfig.thesaurusId,
                    thesaurusLabel: itemConfig.label,

                    handler: Ext.bind(me._onItemPress, me),
                    enableToggle: false,
                    pressed: false
                }, me.getInitialConfig('items-config') || {}));
                
                gpItems.push(buttonElement);
            }
            
            var menuPanelCfg = Ext.applyIf({
                    title: "{{i18n PLUGINS_THESAURUS_GALLERY_GROUP_LABEL}}",
                    items: gpItems
                }, me._getMenuSubPanelConfig());
                
            var menuPanel = Ext.create("Ametys.ui.fluent.ribbon.controls.gallery.MenuPanel", menuPanelCfg);
                
            if (menuPanelWrapper)
            {
                
                menuPanelWrapper.add(menuPanel);
            }
            else
            {
                return this._createGalleryWrapper([menuPanel]);
            }
        }
        
        return menuPanelWrapper;
    },
    
	/**
	 * This function is called when an item of the gallery is pressed
	 * @param {Ametys.ui.fluent.ribbon.controls.Button} button The pressed button
	 * @param {Boolean} state The button state.
	 * @private
	 */
	_onItemPress: function (button, state)
	{
		var actionFn = button.action;
		if (actionFn)
		{
			var role = button['opentool-id'];
			
			var tool = Ametys.tool.ToolsManager.getTool(role + "$" + button.thesaurusId);
			if (tool != null)
			{
				if (tool.isActivated())
				{
					tool.close();
				}
				else
				{
					tool.focus();
				}
			}
			else
			{
				Ametys.tool.ToolsManager.openTool(role, {'id': button.thesaurusId, 'label': button.thesaurusLabel});
			}
		}
	}
});