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

/**
 * <p>This tool displays a tree of sites
 * @private
 */
Ext.define('Ametys.plugins.web.site.SiteStatisticsTool', {
	extend: 'Ametys.tool.Tool',
	
	/**
	 * @private
	 * @property {Ext.tree.Panel} _tree The statistics tree
	 */
	
	getMBSelectionInteraction: function() 
	{
		return Ametys.tool.Tool.MB_TYPE_NOSELECTION;
	},
	
	createPanel: function ()
	{
		this._tree = this._createStatisticsTreePanel();
		return this._tree;
	},
	
	setParams: function (params)
	{
		this.callParent(arguments);
		this._siteName = params.siteName;
		
		this._tree.getRootNode().set('text', this._siteName);
		this._tree.getRootNode().set('label', this._siteName);
		this._tree.getRootNode().commit();
		
		var toolTitle = "{{i18n PLUGINS_WEB_ADMINISTRATOR_UITOOL_SITE_STATISTICS_TITLE}}";
		toolTitle += params.siteTitle ? params.siteTitle + " (" + this._siteName + ")" : this._siteName;
		this.setTitle(toolTitle);
		
		this.refresh();
	},
	
	refresh: function ()
	{
		this.showRefreshing();
		
		this._tree.getRootNode().expand(false, Ext.bind(this.showRefreshed, this));
	},

    /**
     * Get the tree of the tool
     * @return {Ext.tree.Panel} The main tree component of the tool
     */
	getTree: function()
	{
		return this._tree;
	},
	
	/**
	 * @private
	 * Draw the tree panel for the statistics
	 */
	_createStatisticsTreePanel: function()
	{
		return Ext.create('Ext.tree.Panel', {
			
			cls:'site-statistics-tree',
			border: false,
			scrollable: true,
			
			rootVisible: true,
			
			columns: [{
		        xtype: 'treecolumn',
		        text: "{{i18n PLUGINS_WEB_ADMINISTRATOR_SITE_STATISTICS_COL_NAME}}",
		        dataIndex: 'label',
		        width: 300,
		        sortable: true
		    }, {
		        text: "{{i18n PLUGINS_WEB_ADMINISTRATOR_SITE_STATISTICS_COL_VALUE}}",
		        dataIndex: 'value',
		        flex: 1,
		        sortable: true
		    }],
			
			store: Ext.create('Ext.data.TreeStore', {
				model: 'Ametys.plugins.web.site.SiteStatisticsTool.Statistic',
				
				root: {
					icon: Ametys.getPluginResourcesPrefix('web') + '/img/site/statistics_16.png',
					text: 'root',
					type: 'root',
					id: 'root',
					expanded: false
				},	
				
		        proxy: {
		        	type: 'ametys',
					plugin: 'web',
					url: 'administrator/site/statistics.json',
		        	reader: {
		        		type: 'json',
						rootProperty: 'values'
		        	}
		        },
		        
		        listeners: {
					'beforeload': Ext.bind(this._onBeforeLoad, this)
				},
		        
		        sorters: [ { property: 'title', direction: "ASC" }]
		        
			})
		});
	},
	
	/**
	 * @private
	 * Listener before loading tree node
	 * @param {Ext.data.TreeStore} store The tree store
	 * @param {Ext.data.operation.Operation} operation The load operation object
	 */
	_onBeforeLoad: function(store, operation)
    {
		operation.setParams(Ext.apply(operation.getParams() || {}, {
			siteName: this._siteName
		}));
    }
});