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

/**
 * Implementation of a Ametys.tool.factory.BasicToolFactory for page tool.
 * If the tool is open with the parameter "path" instead of id of page, the pre-treatment will get the id of the path
 * 
 * A {@link Ametys.plugins.web.page.tool.PageTool} can be open by its id
 * 
 */
Ext.define("Ametys.plugins.web.page.tool.PageToolFactory", 
	{
		extend: "Ametys.tool.factory.BasicToolFactory",
			
		/**
		 * Open the page tool
		 * @param {Object} toolParams the parameters to open tool:
		 * @param {String} toolParams.id the id of the page. Required if toolParams.path is empty
		 * @param {String} toolParams.path the path in sitemap of the page. Required if toolParams.id is empty.
		 * @param {String} [toolParams.lang] the language of the page. Used only if path parameter is not empty. If null, the current language will be determined from current context.
		 * @param {String} [toolParams.siteName] the site of the page. Used only if path parameter is not empty. If null, the current site will be determined from current context.
		 * @return {Ametys.tool.Tool} the page tool.
		 */
		openTool: function(toolParams)
		{
            var args = arguments;
            
            if (!Ametys.tool.ToolsManager.isInitialized())
            {
                // On startup do not open additionnal tools for node/redirect pages
                toolParams.followNavigation = false;
            }
            
			if (!toolParams.id && toolParams.path)
			{
				var siteName = toolParams.siteName || Ametys.getAppParameters().siteName;
				var lang = toolParams.lang || Ametys.cms.language.LanguageDAO.getCurrentLanguage();
				
				var response = Ametys.data.ServerComm.send({
			        plugin: 'web',
				    url: 'page/convert-path-to-id',
				    parameters: {path: toolParams.path, lang: lang, siteName: siteName}, 
				    priority: Ametys.data.ServerComm.PRIORITY_SYNCHRONOUS, 
				    responseType: 'text'
				});
				
				var result = Ext.JSON.decode(Ext.dom.Query.selectValue("", response));
				toolParams.id = result.id;
				
				args = [toolParams];
			}
            
            var currentTool = Ametys.tool.ToolsManager.isInitialized() ? Ametys.tool.ToolsManager.getFocusedTool() : null;
            var currentToolIsAPageTool = currentTool instanceof Ametys.plugins.web.page.tool.PageTool;
            
            if (currentToolIsAPageTool && currentTool._ctrlPressedOnLastLink === false && toolParams.followNavigation)
            {
                 var toolId = this.getId() + "$" + args[0].id
                 
                 if (!Ametys.tool.ToolsManager.getTool(toolId)) // The new tool was already opened? do not close the current one... just switch
                 {
                    currentTool.close();
                 }
            }
            
    	    return this.callParent(args);
		}
	}
);