/*
 *  Copyright 2019 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 tool is a tool for viewing or editing a content
 * @private
 */
Ext.define('Ametys.plugins.web.content.tool.ContentTool', {
    extend: "Ametys.plugins.cms.content.tool.ContentTool",
    
    createPanel: function ()
    {
    	var contentPanel = this.callParent(arguments);
    	
    	var oosCt = this._createOutOfSitePanel();
    	var sharedCt = this._createSharedContentPanel();
    	
    	contentPanel.addDocked ([oosCt, sharedCt]);
    	
    	return contentPanel;
    },
    
    setParams: function(params)
    {
        params['close-after-edition'] = params && params.mode == 'edit' && (params['close-after-edition'] != null ? params['close-after-edition'] : this.getParams()['close-after-edition'] !== false); 
        this.callParent(arguments);
    },
    
    /**
	 * Provides the out of site component to display when the content is out of site.
	 * @return {Ext.container.Container} The out of site panel
	 * @private
	 */
    _createOutOfSitePanel: function ()
    {
        return {
            dock: 'top',
            xtype: 'button',
            hidden: true,
            itemId: 'out-of-site',
            ui: 'tool-hintmessage',
            text: "{{i18n PLUGINS_WEB_TOOL_CONTENT_CONTENTTOOL_EXTERNAL}}",
            handler: this._openOOSContent,
            scope: this
        };
    },
    
    /**
	 * Provides the component to display when the content is shared.
	 * @return {Ext.container.Container} The componant
	 * @private
	 */
    _createSharedContentPanel: function ()
    {
        return {
            dock: 'top',
            xtype: 'button',
            hidden: true,
            itemId: 'shared-content',
            ui: 'tool-hintmessage',
            text: "{{i18n PLUGINS_WEB_TOOL_CONTENT_CONTENTTOOL_SHARED}}",
            handler: this._openInitialContent,
            scope: this
        };
    },
    
    /**
     * @private
     * Callback of #_setParams for the Ametys.web.content.ContentDAO#getContent call
     * @param {Ametys.web.content.Content} content The content retrieved
     */
    _getContentCb: function (content)
    {
        if (!this._destroyed)
        {
            this.callParent(arguments);
            
            this.getContentPanel().down("#out-of-site").setVisible (content.getSiteName() != null && Ametys.getAppParameter('siteName') != content.getSiteName());
            this.getContentPanel().down("#shared-content").setVisible (content.getIsShared());
        }
    },
    
    _createContentTarget: function (content)
    {
        if (content && content.getSiteName() != null && Ametys.getAppParameter('siteName') != content.getSiteName())
        {
            // Shared or out-of-site content : send empty selection message to see content in read-only mode
            Ext.create("Ametys.message.Message", {
                type: Ametys.message.Message.SELECTION_CHANGED,
                targets: []
            });
        }
        else
        {
            this.callParent(arguments);
        }
    },

    
    /**
     * @private 
     * Open a "out-of-site" content in a new window
     */
    _openOOSContent: function ()
    {
        var params = "uitool=uitool-content,id:%27" + this._contentId + "%27";
        if (this._workspaceName)
        {
            params += ",%27jcr-workspace-name%27:%27" + this._workspaceName + "%27";   
        }
        if (this._contentMessageType != Ametys.message.MessageTarget.CONTENT)
        {
            params += ",%27content-message-type%27:%27" + this._contentMessageType + "%27";
        }

    	window.open("../" + this._content.getSiteName() + "/index.html?" + params);
    },
    
    /**
     * @private 
     * Open the initial content of a shared content
     */
    _openInitialContent: function ()
    {
    	Ametys.tool.ToolsManager.openTool ('uitool-content', {id: this._content.getInitialContentId()});
    }
    
});