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

/**
 * Singleton class defining the open content action. 
 * @private
 */
Ext.define('Ametys.plugins.cms.content.actions.OpenContentAction', {
	singleton: true,
	
	/**
	 * This function opens the content registered by the controller
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	view: function(controller)
	{
		var target = controller.getMatchingTargets()[0];
		
		if (target != null)
		{
			var contentId = target.getParameters().id;
			var contentTool = Ametys.tool.ToolsManager.getTool('uitool-content$' + contentId);
			
			if (contentTool == null)
			{
				Ametys.tool.ToolsManager.openTool('uitool-content', {id: contentId});
			}
		}
	},
	
	/**
	 * This function open the content registered by the controller to edit it
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	edit: function (controller)
	{
		var target = controller.getMatchingTargets()[0];
		
		if (target != null)
		{
			var contentId = target.getParameters().id;
			var contentWidth = target.getParameters().contentWidth || 0;
            
            var viewName = controller.getInitialConfig("view-name") || 'default-edition';
			
			Ametys.tool.ToolsManager.openTool('uitool-content', {id: contentId, mode: 'edit', "edit-workflow-action": controller.getWorkflowActionId(), "view-name": viewName, "content-width": contentWidth});
		}
	},
	
	/**
	 * This function reload the focused content tool
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	reload: function (controller)
	{
		var target = controller.getMatchingTargets()[0];
		if (target != null)
		{
			var contentId = target.getParameters().id;
			var contentTool = Ametys.tool.ToolsManager.getTool('uitool-content$' + contentId);

			if (contentTool != null)
			{
				contentTool.refresh(false);
			}
		}
	},
    
    /**
     * This function opens the content to see its raw data
     * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
     */
    viewData: function(controller)
    {
        var target = controller.getMatchingTargets()[0];
        if (target != null)
        {
            var contentId = target.getParameters().id;
            var contentVersion = target.getParameters().version; // old-content tool target only
            var toolId = contentId + (contentVersion ? "$" + contentVersion : '');
            var contentTool = Ametys.tool.ToolsManager.getTool('uitool-content-data$' + toolId);
      
            if (contentTool == null)
            {
                var toolParams = {id: toolId, contentId: contentId};
                if (contentVersion)
                {
                    toolParams['contentVersion'] = contentVersion;
                    toolParams['versionName'] = target.getParameters().versionName;
                }
                Ametys.tool.ToolsManager.openTool('uitool-content-data', toolParams);
            }
        }
    }
});