/*
 *  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 displays the reported contents of the parameterized type and their reported comments 
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tool.ReportsTool', {
	extend: "Ametys.tool.Tool",
    mixins: {common: "Ametys.plugins.cms.content.tool.CommonCommentsAndReportsTool"},
	
    /**
     * @property {Ext.data.ArrayStore} _store The store with the comments and reports
     * @private
     */
    
    /**
     * @property {Ext.tree.Panel} _treePanel The tree panel displaying the comments and reports
     * @private
     */
    
    /**
     * @property {String} _contentTypeId The content type identifier
     * @private
     */
    
    /**
     * @property {String} _label The tool's label
     * @private
     */
    
    /**
     * @property {String} _description The tool's description
     * @private
     */
    
    /**
     * @property {String} _emptyMessage The message to show if there is no reported sontents or comments
     * @private
     */
	
	constructor: function(config)
	{
		this.callParent(arguments);
		this._initialize();
	},
	
	createPanel: function()
	{
        this._store = this._getTreeStore({
            proxy: {
				role:  'org.ametys.cms.repository.comment.ui.CommentsAndReportsTreeComponent', // default
                methodName: 'getCommentsAndReportsFromContentTypeId', // default
                methodArguments: ['contentTypeId'],
                errorMessage: "{{i18n PLUGINS_CMS_CONTENT_REPORTS_LOADING_CONTENTS_ERROR}}"
            },
            root: {
                loaded: true
            },
            autoLoad: false
        });
        
        this._treePanel = this._getTreePanel({store: this._store});
        
        return this._treePanel;
	},
    
    setParams: function(params)
    {
        this.callParent(arguments);
        this._contentTypeId = params['contentTypeId'];
        this._label = params['label'];
        this._description = params['description'];
        this._emptyMessage = params['empty-message'];
		
		// Proxy configuration can be override by configuration
		var proxy = this._store.getProxy();
		proxy.role =  params['serverRole'] || proxy.role;
		proxy.methodName = params['methodName'] || proxy.methodName;
		
        this.refresh(false, true);
    },
	
    getMBSelectionInteraction: function()
    {
        return Ametys.tool.Tool.MB_TYPE_ACTIVE;
    },
	
	sendCurrentSelection: function()
	{
       this.onSelectComment(); 
    },
    
    refresh: function ()
    {
        this.showRefreshing();
        
        this.setTitle(this._label);
        this.setDescription(this._description);
        
        this._treePanel.unmask();
        
        this._store.load({callback: this.showRefreshed, scope: this});
    },
    
    /**
     * Function before loading the store
     * @param {Ext.data.Store} store The store
     * @param {Ext.data.operation.Operation} operation The object that will be passed to the Proxy to load the store
     * @private
     */
    _onBeforeLoad: function (store, operation)
    {
        operation.setParams( Ext.apply(operation.getParams(), {
            contentTypeId: this._contentTypeId
        }));
    },
	
	/**
     * Function called when the sore is loaded
     * @param {Ext.data.TreeStore} store The store
     * @param {Ext.data.TreeModel[]} records The array of records
     * @param {Boolean} success True if the operation was successful
     * @private
     */
	_onLoad: function(store, records, success)
	{
        if (records.length == 0)
        {
            this._treePanel.mask(this._emptyMessage, 'ametys-mask-unloading');
        }
		this._treePanel.expandAll();
	}
});