import { Command } from '@ckeditor/ckeditor5-core';
import { findAttributeRange } from '@ckeditor/ckeditor5-typing';
import { toMap, first } from '@ckeditor/ckeditor5-utils';
import { isImageAllowed, TOOLTIP_ATTRIBUTE } from './utils';

export default class TooltipCommand extends Command {
  
	refresh() {
		const model = this.editor.model;
		const doc = model.document;
		const selectedElement = first(doc.selection.getSelectedBlocks());
 
		// A check for the `LinkImage` plugin. If the selection contains an element, get values from the element.
		// Currently the selection reads attributes from text nodes only. See #7429 and #7465.
		if (isImageAllowed(selectedElement, model.schema)) {
			this.isEnabled = model.schema.checkAttribute(selectedElement, TOOLTIP_ATTRIBUTE);
		} else {
			this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, TOOLTIP_ATTRIBUTE);
		}
	}
 
	execute(options) {
		const model = this.editor.model;
		const selection = model.document.selection;
        model.change(writer => {
			// If selection is collapsed then update selected link or insert new one at the place of caret.
			if (selection.isCollapsed) {
				const position = selection.getFirstPosition();

				// When selection is inside text with `qmsTooltip` attribute.
				if (selection.hasAttribute(TOOLTIP_ATTRIBUTE)) {
					// Then update `qmsTooltip` value.
					const linkRange = findAttributeRange(position, TOOLTIP_ATTRIBUTE, selection.getAttribute(TOOLTIP_ATTRIBUTE), model);
 
					const attributes = toMap(selection.getAttributes());

					attributes.set(TOOLTIP_ATTRIBUTE, options.content);

					const { end: positionAfter } = model.insertContent(writer.createText(options.text, attributes), linkRange);

					// Put the selection at the end of the inserted link.
					// Using end of range returned from insertContent in case nodes with the same attributes got merged.
					writer.setSelection(positionAfter);
				}
				else if (options.text !== '') {
					const attributes = toMap(selection.getAttributes());

					attributes.set(TOOLTIP_ATTRIBUTE, options.content);

					const { end: positionAfter } = model.insertContent(writer.createText(options.text, attributes), position);

					// Put the selection at the end of the inserted link.
					// Using end of range returned from insertContent in case nodes with the same attributes got merged.
					writer.setSelection(positionAfter);
				}
			} else {
				const attributes = toMap(selection.getAttributes());

				attributes.set(TOOLTIP_ATTRIBUTE, options.content);

				const { end: positionAfter } = model.insertContent(writer.createText(options.text, attributes), selection);

				writer.setSelection(positionAfter);
			}
			// Remove the `qmsTooltip` attribute and all link decorators from the selection.
			// It stops adding a new content into the link element.
			[TOOLTIP_ATTRIBUTE].forEach(item => {
				writer.removeSelectionAttribute(item);
			});
			console.log('this.editor.getData()' + new Date().toISOString());
			console.log(this.editor.getData());
		});
	}
}

