/*
 *  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 is a relation handler between:
 * * source : explorer node or explorer resource of content attachments
 * * destination : explorer node of content attachments
 * 
 * E.g. when you drag file or folder attachment to an folder of content attachments
 * @private
 */
Ext.define('Ametys.plugins.cms.relations.ContentAttachmentsRelationHandler', {
	extend: 'Ametys.plugins.explorer.relations.ExplorerResourcesRelationHandler',
	
	sourceFilter: function (target)
	{
		var targetId = Ext.getClassName(target) == "Ametys.message.MessageTarget" ? target.getId() : target.id;
		if (targetId == Ametys.message.MessageTarget.EXPLORER_COLLECTION || targetId == Ametys.message.MessageTarget.RESOURCE)
		{
			// If target is not ready, returns true
			return Ext.getClassName(target) == "Ametys.message.MessageTarget" ? target.getParameters().rootOwnerType == 'content' : true;
		}
		return false;
	},
	
	targetFilter: function (target)
	{
		var targetId = Ext.getClassName(target) == "Ametys.message.MessageTarget" ? target.getId() : target.id;
		if (targetId == Ametys.message.MessageTarget.EXPLORER_COLLECTION)
		{
			// If target is not ready, returns true
			return Ext.getClassName(target) == "Ametys.message.MessageTarget" ? target.getParameters().rootOwnerType == 'content' : true;
		}
		return false;
	},
	
	sourceFolderFilter: function (target)
	{
		return target.getId() == Ametys.message.MessageTarget.EXPLORER_COLLECTION && target.getParameters().rootOwnerType == 'content';
	},
	
	sourceFileFilter: function (target)
	{
		return target.getId() == Ametys.message.MessageTarget.RESOURCE && target.getParameters().rootOwnerType == 'content';
	},
	
	canRemoveFromSource: function (sourceId, rights)
	{
		return !rights || Ext.Array.contains(rights, "CMS_Rights_Content_Attachments")
	},
	
	canAddToTarget: function (rights, sourceContainsFiles, sourceContainsFolders)
	{
		return !rights || Ext.Array.contains(rights, "CMS_Rights_Content_Attachments");
	}
});