/*
 *  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 to handle reference tables
 * @private
 */
Ext.define('Ametys.plugins.cms.content.actions.ReferenceTableActions', {
	singleton: true,
	
	/**
	 * This actions creates a entry of a reference tables and adds it in the reference table content edition tool that has focus.
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	add: function (controller)
	{
		var tool = Ametys.tool.ToolsManager.getFocusedTool();
		
		var params = {
			contentType: tool.contentTypeId,
			contentTitle: tool.contentType.getDefaultTitle() || "{{i18n PLUGINS_CMS_REFERENCE_TABLE_DEFAULT_TITLE}}",
			contentLanguage: tool.contentLanguage,
			initWorkflowActionId: tool.workflowInitActionId,
			workflowName: tool.workflowName
		};
			
		Ametys.cms.content.ContentDAO.createContent(
			params,
			null,
			this,
			false,
			Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT,
            null,
            null,
            true // auto creation
		);
	},
    
    /**
     * 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, 'content-message-type': Ametys.message.MessageTarget.REFERENCE_TABLE_CONTENT});
            }
        }
    },
    
    /**
     * 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 viewName = controller.getInitialConfig("view-name") || 'default-edition';
            
            Ametys.tool.ToolsManager.openTool('uitool-content', {id: contentId, mode: 'edit', "view-name": viewName, "edit-workflow-action": controller.getWorkflowActionId()});
        }
    },
    
    /**
     * This action opens the tool for referencing contents
     * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
     */
    openReferencingContents: function(controller) 
    {
        var target = controller.getMatchingTargets()[0];
        if (target != null)
        {
            Ametys.tool.ToolsManager.openTool('uitool-referencetable-referencing-contents', {
                'id': target.getParameters().id, 
                'title': target.getParameters().title,
                'contenttypes': target.getParameters().types
            });  
        }
    },
	
	
});