
import { Command } from '@ckeditor/ckeditor5-core';
import { Collection, first } from '@ckeditor/ckeditor5-utils';
import * as QMSCKEditorConstant from '../common/qmsCKEditorConstant';
import * as QMSCKEditorEnum from '../common/qmsCKEditorEnum';
import * as QMSCKEditorService from '../common/qmsCKEditorService';
import { isImageAllowed } from './utils';
import AutomaticDecorators from './utils/automaticdecorators';
  
 export default class LinkDialogCommand extends Command {
    constructor( editor ) {
		super( editor );
		this.manualDecorators = new Collection();
		this.automaticDecorators = new AutomaticDecorators();
	}
 
	restoreManualDecoratorStates() {
		for ( const manualDecorator of this.manualDecorators ) {
			manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
		}
	}
 
	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.value = selectedElement.getAttribute( 'linkHref' );
			this.isEnabled = model.schema.checkAttribute( selectedElement, 'linkHref' );
		} else {
			this.value = doc.selection.getAttribute( 'linkHref' );
			this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
		}

		for ( const manualDecorator of this.manualDecorators ) {
			manualDecorator.value = this._getDecoratorStateFromModel( manualDecorator.id );
		}
	}
  
     execute() {
        const editor = this.editor;
        const model = editor.model;
        const selection = model.document.selection;
        const ranges = selection.getFirstRange();

        let link = QMSCKEditorEnum.hyperLink;
        link.typeId = 0;
        link.targetId = 0;
        link.protocolId = 0;
        link.url = '';
        link.title = '';
        link.editorContent = '';
        for (const item of ranges.getItems()) {
            if (item.data) {
                link.title = item.data;
            }
        }
        QMSCKEditorService.linkNotify(link);
        window[QMSCKEditorConstant.QMSCK_LINKED] = false;
        window.addEventListener(QMSCKEditorConstant.QMSCK_LINK_PLUGIN_RESP, function(evt) {
            if (window[QMSCKEditorConstant.QMSCK_LINKED]){
                return;
            }
			window[QMSCKEditorConstant.QMSCK_LINKED] = true;
            const options = evt.detail;
			editor.commands.get('link').execute(options);
        }, { once: true });
     }

     _getDecoratorStateFromModel( decoratorName ) {
		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 ) ) {
			return selectedElement.getAttribute( decoratorName );
		}

		return doc.selection.getAttribute( decoratorName );
	}
 }
 