/*
 *  Copyright 2020 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 Welcome tool is used to display some help and information about the CMS application
 */
Ext.define('Ametys.plugins.cms.welcome.tool.WelcomeTool',  {
	extend: 'Ametys.tool.Tool',
	
	/**
	 * @private
	 * @property {Ext.ux.IFrame} _iframe The iframe object
	 */
	
    getMBSelectionInteraction: function() 
    {
        return Ametys.tool.Tool.MB_TYPE_NOSELECTION;
    },
    
	createPanel: function()
	{
		this._iframe = Ext.create("Ext.ux.IFrame", {}); 
		this._iframe.on ('load', this._onIframeLoad, this);
		
		return Ext.create('Ext.panel.Panel', {
			border: false,
			layout: 'fit',
			items: [this._iframe]
		});
	},
    
    setParams: function()
    {
    	this.callParent(arguments);
    	this.refresh();
    	
    	// register the welcome tool to the history tool
	    var toolId = this.getFactory().getId();
	    var toolParams = this.getParams();
    	
	    Ametys.navhistory.HistoryDAO.addEntry({
	        id: this.getId(),
	        label: this.getTitle(),
	        description: this.getDescription(),
	        iconSmall: this.getSmallIcon(),
	        iconMedium: this.getMediumIcon(),
	        iconLarge: this.getLargeIcon(),
	        type: Ametys.navhistory.HistoryDAO.TOOL_TYPE,
	        action: Ext.bind(Ametys.tool.ToolsManager.openTool, Ametys.tool.ToolsManager, [toolId, toolParams], false)
	    });
    },
    
    refresh: function()
    {
        if (this.config.canDisplayWelcomeTool)
        {
    		var url = this.getUrl();
        	this.showRefreshing();
        	this._iframe.load(url);
        }
        else
        {
            this.close();
        }
    },
    
    getUrl: function()
    {
        var url = Ametys.getPluginDirectPrefix('cms') + '/welcome_' + Ametys.getAppParameter('user').locale + '.html';
        return url;
    },
    
    /**
     * @private
     * Listener called when the iframe is loaded.
     * Relays the focus to the tool
     * @param {Ext.ux.IFrame} iframe The iframe
     */
    _onIframeLoad: function (iframe)
    {
        if (window.event && window.event.srcElement.readyState && !/loaded|complete/.test(window.event.srcElement.readyState))
        {
            return;
        }
        
		this._iframe.getWin().onfocus = Ext.bind(this._onIframeFocus, this);
		this.showRefreshed();
    },
    
    /**
     * @private
     * Listener when iframe takes the focus.
     * Force the focus on tool.
     */
    _onIframeFocus: function ()
    {
    	// Force focus on tool
    	this.focus();
    }
});