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

/**
 * This tool displays the comments and reports on selected contents
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tool.CommentsAndReportsTool', {
	extend: "Ametys.tool.SelectionTool",
    mixins: {common: "Ametys.plugins.cms.content.tool.CommonCommentsAndReportsTool"},
	
	/**
     * @property {Ext.data.ArrayStore} _store The store with the comments and reports
     * @protected
     */
    
    /**
     * @property {Ext.tree.Panel} _treePanel The tree panel displaying the comments and reports
     * @protected
     */
    
    constructor: function(config)
	{
		this.callParent(arguments);
		this._initialize();
	},
	
	createPanel: function()
	{
        this._store = this._getTreeStore({
            proxy: {
                methodName: 'getCommentsAndReportsFromSelectedContents',
                methodArguments: ['contentIds']
            }
        });
        
        this._treePanel = this._getTreePanel({store: this._store});
        
        return this._treePanel;
	},
	
    _isRefreshNeeded: function (message)
    {
        var targets = message.getTargets(function(target) {
            return target.getId() == Ametys.message.MessageTarget.CONTENT
                || target.getId() == Ametys.message.MessageTarget.COMMENT;
        });
        
        if (targets != null && targets.length > 0)
        {
            for (var i=0; i < targets.length; i++)
            {
                var target = targets[i];
                if (target.getId() == Ametys.message.MessageTarget.COMMENT)
                {
                    if (this._store.find('contentId', target.getParameters().content) == -1)
                    {
                        return this.callParent(arguments);
                    }
                }
                else
                {
                    if (this._store.find('contentId', target.getParameters().id) == -1)
                    {
                        return this.callParent(arguments);
                    }
                }
            }
            return false;
        }
        else
        {
            return this.callParent(arguments);
        }
    },
    
    refresh: function ()
    {
        this.showRefreshing();
        
        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)
    {
        var ids = [];
        
        var targets = this.getCurrentSelectionTargets();
        if (targets != null)
        {
            Ext.Array.each (targets, function (target) {
                if (target.getId() == Ametys.message.MessageTarget.COMMENT)
                {
                    ids.push(target.getParameters().content);
                }
                else
                {
                    ids.push(target.getParameters().id);
                }
            });
        }
        
        if (ids.length == 0)
        {
            return false;
        }
        
        operation.setParams( Ext.apply(operation.getParams(), {
            contentIds: ids
        }));
        
        this._initialContentIds = ids;
    },
	
	/**
     * 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()
	{
		this._treePanel.expandAll();
	},
	
	setNoSelectionMatchState: function (message)
	{
		this.callParent(arguments);
        this._store.getRoot().removeAll(); // Empty the store
        this._treePanel.mask(message, 'ametys-mask-unloading');
	}
});