import { Command } from 'ckeditor5/src/core';
import { isVideoAllowed } from "../video/utils";
import * as QMSCKEditorService from '../../common/qmsCKEditorService';
import * as QMSCKEditorConstant from '../../common/qmsCKEditorConstant';

export default class BrowseVideoDialogCommand extends Command {
    refresh() {
        const videoElement = this.editor.model.document.selection.getSelectedElement();
        const isVideo = videoElement && videoElement.name === 'video' || false;
        this.isEnabled = isVideoAllowed( this.editor.model ) || isVideo;
    }

    execute(options) {
      const editor = this.editor;
      QMSCKEditorService.uploadVideoNotify({...options});
      window[QMSCKEditorConstant.QMSCK_BROWSE_VIDEO_IS_PROCESSING] = false;
      window.addEventListener(QMSCKEditorConstant.QMSCK_BROWSE_VIDEO_RESP, (evt) => {
            if(window[QMSCKEditorConstant.QMSCK_BROWSE_VIDEO_IS_PROCESSING] || !evt.detail){
              return;
            }
            window[QMSCKEditorConstant.QMSCK_BROWSE_VIDEO_IS_PROCESSING] = true;
            editor.commands.get('uploadVideo').execute(evt.detail);
      }, { once: true});
    }
}
