/*
 *  Copyright 2017 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.
 */
 
 /**
  * Ribbon button for opening, if needed, the HierarchicalReferenceTablesTool, besides the ReferenceTableEditionTool.
  */
Ext.define('Ametys.plugins.cms.content.controller.OpenReferenceTableToolsButtonController', {
    extend: 'Ametys.ribbon.element.ui.button.OpenToolButtonController',
    
    statics: {
        /**
         * @protected
         * This action do open or close both tools for handling hierarchical reference tables (ReferenceTableEditionTool and HierarchicalReferenceTablesTool)
         * @param {Ametys.ribbon.element.ui.button.OpenToolButtonController} button This controller.
         */
        _act: function (button)
        {
            Ametys.ribbon.element.ui.button.OpenToolButtonController._act(button); // will open ReferenceTableEditionTool
            
            // Now, will open or close HierarchicalSimpleContentsTool according to the status of ReferenceTableEditionTool
            var editionToolId = button.getInitialConfig("opentool-id"),
                editionToolParameters = button.getInitialConfig("opentool-params") || {},
                openTreeToolId = button.getInitialConfig("open-tree-tool-id");
                openTreeToolParameters = button.getInitialConfig("open-tree-tool-params") || {};
            if (openTreeToolId != null && openTreeToolParameters != null)
            {
                var editionTool = this._getTool(editionToolId, editionToolParameters), 
                    openTreeTool = this._getTool(openTreeToolId, openTreeToolParameters);
                
                if (editionTool != null && openTreeTool == null)
                {
                    // HierarchicalSimpleContentsTool should open
                    Ametys.tool.ToolsManager.openTool(openTreeToolId, openTreeToolParameters);
                }
                else if (editionTool == null && openTreeTool != null)
                {
                    // HierarchicalSimpleContentsTool should close
                    openTreeTool.close();
                }
            }
        },
        
        /**
         * @protected
         * Gets the tool with the given id and parameters
         * @param {String} toolId The id of the tool
         * @param {Object} toolParameters The tool parameters
         * @return {Ametys.tool.Tool} The tool, or null if not opened
         */
        _getTool: function(toolId, toolParameters)
        {
            var tool = null;
            if (Ametys.tool.ToolsManager.getFactory(toolId) instanceof Ametys.tool.factory.UniqueToolFactory)
            {
                tool = Ametys.tool.ToolsManager.getTool(toolId);
            }
            else if (Ametys.tool.ToolsManager.getFactory(toolId) instanceof Ametys.tool.factory.BasicToolFactory)
            {
                tool = Ametys.tool.ToolsManager.getTool(toolId + "$" + toolParameters.id)
            }
            
            return tool;
        }
    }
});