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

/**
 * The page attachments tool. Derived from the default explorer tool.
 * @private
 */
Ext.define('Ametys.plugins.web.page.tool.PageAttachmentsTool', {
	extend: 'Ametys.cms.attach.AttachmentsTool',
	
	getNoSelectionMessage: function (message)
	{
        return message || "{{i18n PLUGINS_WEB_PAGE_ATTACHMENTS_NO_SELECTION}}";
	},
	
	getOwnerMessageTargetConfiguration: function ()
	{
		return {
			id: Ametys.message.MessageTarget.PAGE,
			parameters: { ids: [this._ownerId]}
		}
	},
	
	getRootNodeConfiguration: function (id, callback)
	{
		Ametys.data.ServerComm.callMethod({
			role: "org.ametys.web.repository.page.PageDAO",
			methodName: 'getAttachmentsRootNode',
			parameters: [id],
            callback: {
            	scope: this,
            	handler: this._getAttachmentsRootNodeCb,
            	arguments: [callback],
                ignoreOnError: false
            },
            errorMessage: {
				msg: "{{i18n PLUGINS_WEB_PAGE_ATTACHMENTS_ERROR}}",
				category: this.self.getName()
			}
		});
	},
	
	/**
	 * @private
	 * Callback function invoked after retrieving the page's attachment root node
	 * @param {Object} response The root properties
	 * @param {Object[]} args the callback arguments
	 */
	_getAttachmentsRootNodeCb: function (response, args)
	{
        if (response.id)
        {
            args[0]({
	            id: response.id,
	            text: "{{i18n PLUGINS_WEB_PAGE_ATTACHMENTS_ROOT_NODE}}" + ' (' + response.title + ')',
	            canCreateChild: response.canCreateChild,
	            hasChildNodes: response.hasChildNodes,
	            hasResources: response.hasResources
	        });
        }
        else
        {
            this._setInNoSelectionState("{{i18n PLUGINS_WEB_PAGE_ATTACHMENTS_NO_RIGHT}}");
        }
		
	},
	
	getTargetFilter: function()
	{
		return Ametys.message.MessageTarget.PAGE;
	},
	
	getOwnerMessageInfo: function (target)
	{
		return "{{i18n PLUGINS_WEB_PAGE_ATTACHMENTS_HINT_TEXT}}" + '<b>' + Ext.String.escapeHtml(target.getParameters().title) + '</b>';
	},
	
	getRightId: function ()
	{
		return 'Web_Rights_Page_Attachments';
	}
	
});