/*
 *  Copyright 2014 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 class provides a TreePanel of the explorer that displays attachments.
 * @private
 */
Ext.define('Ametys.plugins.cms.content.tree.AttachmentsExplorerTree', {
	extend: 'Ametys.explorer.tree.ExplorerTree',
	
	/**
	 * Current node config for the attachments owner
	 * @property
	 * @private
	 */
	_attachmentsOwnerNodeConfig: null,
	
	/**
	 * The right identifier of the right to be checked for drag&drop and rename operations.
	 * @property
	 * @private
	 */
	_rightId: null,
	
	/**
	 * The context of rights to be used when checking rights for drag&drop and rename operations.
	 * @property
	 * @private
	 */
	_rightContext: null,
	
	/**
	 * @inheritdoc
	 */
	setRootNodes: function()
	{
		var rootNodes = [];
		if (this._attachmentsOwnerNodeConfig)
		{
			var attachmentRoot = Ext.apply({
				iconCls: 'ametysicon-clip26',
				allowDrag: false,
				allowDrop: false,
				path: '/dummy/attachments',
				type: 'collection',
				name: 'attachments',
				isModifiable: false,
				canCreateChild: false,
				hasChildNodes: false,
				hasResources: false
			}, this._attachmentsOwnerNodeConfig);
			
			rootNodes.push(attachmentRoot);
		}
		
		this.callParent([rootNodes]);
	},

	getMessageTargetConfiguration: function(record)
	{
		if (this._attachmentsOwnerNodeConfig != null)
		{
			var explorerTarget = this.callParent(arguments);
			
			return {
				id: Ametys.message.MessageTarget.CONTENT,
				parameters: { ids: [this._attachmentsOwnerNodeConfig.contentId] },
				subtargets: explorerTarget || []
			};
		}
		else
		{
			return null;
		}
	},
	
	/**
	 * Set the new owner of the tree and displays its attachments.
	 * @param {Object} ownerProperties The properties of the object. For a content, see {@link Ametys.cms.content.Content#getProperties}
	 * @param {Function} [userCb] A callback function to be executed after the attachments owner info have been retrieved.
	 * @param {Object} [userCbScope=window] The callback scope.
	 */
	setAttachmentsOwner: function(ownerProperties, userCb, userCbScope)
	{
		if (ownerProperties && ownerProperties.id)
		{
			Ametys.data.ServerComm.callMethod({
				role: "org.ametys.cms.attachments.AttachmentsHelper",
				methodName: 'getAttachmentsRootNode',
				parameters: [ownerProperties.id],
                callback: {
                	scope: this,
                	handler: this._setAttachmentsOwnerCb,
                	arguments: [ownerProperties, userCb, userCbScope],
                    ignoreOnError: false
                },
                errorMessage: {
					msg: "{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTSEXPLORERTREE_ERROR}}",
					category: this.self.getName()
				}
			});
		}
		else
		{
			this._attachmentsOwnerNodeConfig = null;
			this.setRootNodes();
			
			// User callback
			userCb = userCb || Ext.emptyFn;
			userCbScope = userCbScope || window;
			userCb.call(userCbScope, ownerProperties);
		}
	},
	
	/**
	 * Callback function of #setAttachmentsOwner when the server response is available.
	 * @param {Object} response the server response
	 * @param {Boolean} response.canCreateChild true if the node can have childs
	 * @param {Boolean} response.isModifiable true if the node is modifiable
	 * @param {String} response.rootID the id of the tree's root
	 * @param {Object[]} args The callback arguments.
	 * @private
	 */
	_setAttachmentsOwnerCb: function(response, args)
	{
		var ownerProperties = args[0];
		var userCb = args[1] || Ext.emptyFn;
		var userCbScope = args[2] || window;
		
		if (ownerProperties == null)
		{
			this._attachmentsOwnerNodeConfig = null;
		}
		else
		{
			this._attachmentsOwnerNodeConfig = {
				id: response['rootID'] || Ext.id(),
				contentId: ownerProperties.id,
				text: "{{i18n PLUGINS_CMS_CONTENT_ATTACHMENTS_ROOT_NODE}}" + ' (' + ownerProperties.title + ')',
				canCreateChild: response['canCreateChild'],
				hasChildNodes: response['hasChildNodes'],
				hasResources: response['hasResources']
			};
		}
		// Request the reload of the root nodes.
		this.setRootNodes();
		
		// Load attachments
		this.refreshRootNodes();
		
		// User callback
		userCb.call(userCbScope, ownerProperties);
	},
	
	/**
	 * Set the right parameters to be used by the tree
	 * @param {String} rightId The right identifier.
	 * @param {String} rightCtx The right context.
	 */
	setRightsParameters: function(rightId, rightCtx)
	{
		this._rightId = rightId;
		this._rightContext = rightCtx;
	},
	
	getRightIdOnRename: function (node)
	{
		return this._rightId;
	},
	
	getRightIdOnDrop: function (node)
	{
		return this._rightId;
	},
	
	getRightIdOnDrag: function (node)
	{
		return this._rightId;
	}
});