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

/**
 * Abbreviation action singleton.
 * Insert an abbreviation.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.SemanticAnnotation', {
	singleton: true,
	
	/**
	 * Insert a semantic annotation tag around the selection.
	 * @param {Ametys.plugins.cms.editor.controller.SemanticAnnotationController} controller The controller of the pressed button
	 * @param {Ametys.ui.fluent.ribbon.controls.Button} button The pressed button
	 * @param {Boolean} pressed The button state.
	 * @private
	 */
	apply: function(controller, button, pressed)
	{
		Ametys.cms.editor.InsertElementHelper.actionDirect({
			tagName :"span",
			field: controller.getCurrentField(),
		   	attributes: {
		   		'class': 'semantic',
		   		title: button.getText(),
		   		annotation: button.annotationName
		   	}
		});
	},
	
	/**
	 * Update the menu gallery according the selected rich text and enable/disable and toggle/untoggle controller according if the current selection has selantic annotation or not
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller
	 * @param {Ametys.cms.form.widget.RichText} field The current field. Can be null
	 * @param {HTMLElement} node The current selected node. Can be null.
	 */
	selectionListener: function (controller, field, node)
	{
		
		var off = !field || !node;
		var state = !off;
		var disabled = off;
		
		if (!off)
		{
			var nodesel = field.getEditor().selection.getNode();
			var elt = field.getEditor().dom.getParent(nodesel, "span[annotation]");
			var sel = field.getEditor().selection.getContent();
			
			disabled = (/<p[> \/]|<td[> \/]|<th[> \/]|<li[> \/]|<div[> \/]/.test(sel)) || (sel == '' && !elt) || nodesel != null && field.getEditor().dom.hasClass(nodesel, "mceNonEditable");
			state = elt != null || new RegExp("<span[^>]*annotation=").test(sel);
		}

		controller.toggle(state);
		controller.setDisabled(disabled);
		controller.setAdditionalDescription(disabled ? "{{i18n CONTENT_EDITION_SEMANTIC_ANNOTATION_DESCRIPTION_DISABLED}}" : '');
		
		if (!disabled)
		{
			var message = Ametys.message.MessageBus.getCurrentSelectionMessage();
			
			var formTarget = message.getTarget('form');
			var fieldTarget = message.getTarget('field');
			
			if (!formTarget || !fieldTarget)
			{
				controller.disable();
				return;
			}
			
			var form = formTarget.getParameters().object;
			var fieldName = fieldTarget.getParameters().name;
			var richtext = form.findField (fieldName);
			
			if (richtext.hasSemanticAnnotations())
			{
				controller.updateMenuGallery (fieldName, richtext);
				controller.setAdditionalDescription('');
			}
			else
			{
				controller.disable();
				controller.setAdditionalDescription("{{i18n CONTENT_EDITION_SEMANTIC_ANNOTATION_NO_ANNOTATION}}");
			}
		}
	}
});