/*
 *  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 content attachments tool. Derived from the default explorer tool.
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tool.ContentAttachmentsTool', {
	extend: 'Ametys.cms.attach.AttachmentsTool',
	
	getNoSelectionMessage: function ()
	{
		return "{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTS_NO_SELECTION}}";
	},
	
	getOwnerMessageTargetConfiguration: function ()
	{
		return {
			id: Ametys.message.MessageTarget.CONTENT,
			parameters: { ids: [this._ownerId]}
		}
	},
	
	getRootNodeConfiguration: function (id, callback)
	{
		Ametys.data.ServerComm.callMethod({
			role: "org.ametys.cms.repository.ContentDAO",
			methodName: 'getAttachmentsRootNode',
			parameters: [id],
            callback: {
            	scope: this,
            	handler: this._getAttachmentsRootNodeCb,
            	arguments: [callback],
                ignoreOnError: false
            },
            errorMessage: {
				msg: "{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTSEXPLORERTREE_ERROR}}",
				category: this.self.getName()
			}
		});
	},
	
	/**
	 * @private
	 * Callback function invoked after retrieving the content'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_CMS_CONTENT_ATTACHMENTS_ROOT_NODE}}" + ' (' + response.title + ')',
				canCreateChild: response.canCreateChild,
				hasChildNodes: response.hasChildNodes,
				hasResources: response.hasResources
			});
        }
        else
        {
            this._setInNoSelectionState("{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTS_NO_RIGHT}}");
        }
	},
	
	getTargetFilter: function()
	{
		return Ametys.message.MessageTarget.CONTENT;
	},
	
	getOwnerMessageInfo: function (target)
	{
		return "{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTS_HINT_TEXT}}" + '<b>' + Ext.String.escapeHtml(target.getParameters().title) + '</b>';
	},
	
	getRightId: function ()
	{
		return 'CMS_Rights_Content_Attachments';
	}
	
});