import { Command } from '@ckeditor/ckeditor5-core';
import * as QMSCKEditorConstant from '../common/qmsCKEditorConstant';
import * as QMSCKEditorService from '../common/qmsCKEditorService';
import { IMAGEMAP_COMMANDS, createInsertedEvent } from '../imagemap/utils';
import { debounced } from '../common/qmsCKEditorUtil';

export default class BpmnDialogCommand extends Command {
	execute() {
    const editor = this.editor;
    const isBpmn = this.isBpmnImage(editor.editing.view.document.selection.getSelectedElement(), editor.plugins.get('Image'))
    if (isBpmn) {
      const parentElement = editor.editing.view.document.selection.getSelectedElement();
      const altAtr = parentElement.getChild(0).getAttribute('alt');
      const arrAtr = altAtr.split("#");
      const id = arrAtr[1];
      QMSCKEditorService.bpmnNotify(id);
    }
    else {
      QMSCKEditorService.bpmnNotify('');
    }
    window[QMSCKEditorConstant.QMSCK_BPMN_IS_PROCESSING] = false;
    window.addEventListener(QMSCKEditorConstant.QMSCK_BPMN_PLUGIN_RESP, function (evt) {
      if (window[QMSCKEditorConstant.QMSCK_BPMN_IS_PROCESSING]) {
          return;
      }
      window[QMSCKEditorConstant.QMSCK_BPMN_IS_PROCESSING] = true;
      editor.model.change( writer => {
        const options = evt.detail.imagemap || {};
          const imageElement = writer.createElement( 'image', {
              src: evt.detail.values,
              alt: evt.detail.key
          });
          // Insert the image in the current selection location.
          editor.model.insertContent( imageElement, editor.model.document.selection );
          if(!!options.areas && options.areas.length > 0){
              editor.model.createSelection(imageElement, "in");//Set the current selection in the inserted image
              const imageMapCommand = editor.commands.get(IMAGEMAP_COMMANDS.IMAGEMAP);
              if(!!imageMapCommand){
                imageMapCommand.set('isEnabled', true);//force to run the command
                options.oldName = options.name;//Delete old image map if any
                imageMapCommand.execute(options);
                //resize image map when image width is greater than the widget width (600px)
                if(options.imageWidth > 600){
                  const resizeImageMapCommand = editor.commands.get(IMAGEMAP_COMMANDS.RESIZE_IMAGEMAP);
                  if(!!resizeImageMapCommand){
                    imageMapCommand.once(createInsertedEvent(options.name), debounced(() => {
                        resizeImageMapCommand.set('isEnabled', true);//force to run the command
                        resizeImageMapCommand.execute();
                    }, 500));
                  }
                }
              }
          }
      });
    }, { once: true });
	}

  isBpmnImage( element, image ) {
    const isImage = element && image.isImageWidget( element );

    if ( !isImage ) {
      return false;
    }

    const altAtr = element.getChild(0).getAttribute('alt')
    if (altAtr && altAtr.includes(QMSCKEditorConstant.QMSCK_BPMN)) {
      return true;
    }
    else {
      return false;
    }
  }
}

