import { Disposable, ICommandService, Inject } from '@univerjs/core';
import { SHEETS_IMAGE_MENU_ID } from '@univerjs/sheets-drawing-ui';
import { ComponentManager, IMenuManagerService, RibbonInsertGroup } from '@univerjs/ui';

import {
  JnpfSheetsFocusFloatImageOperation,
  JnpfSheetsInsertedFloatImageOperation,
  JnpfSheetsInsertFloatImageOperation,
} from '../commands/operations/sheet-float-image.operation';
import univerFloatImageComponent from '../components/Image/float.vue';
import { JnpfUniverFloatImageKey } from '../utils/define';
import { JnpfSheetsInsertFloatImageMenuFactory } from './menus/sheet-float-image.menu';

export class JnpfSheetsFloatImageController extends Disposable {
  constructor(
    @ICommandService private readonly _commandService: ICommandService,
    @IMenuManagerService private readonly _menuManagerService: IMenuManagerService,
    @Inject(ComponentManager) private readonly _componentManager: ComponentManager,
  ) {
    super();

    this._initCommands();
    this._registerComponents();
    this._initMenus();
  }

  private _initCommands(): void {
    [JnpfSheetsInsertFloatImageOperation, JnpfSheetsInsertedFloatImageOperation, JnpfSheetsFocusFloatImageOperation].forEach(command => {
      this.disposeWithMe(this._commandService.registerCommand(command));
    });
  }

  private _initMenus(): void {
    this._menuManagerService.mergeMenu({
      [RibbonInsertGroup.MEDIA]: {
        [SHEETS_IMAGE_MENU_ID]: {
          [JnpfSheetsInsertFloatImageOperation.id]: {
            menuItemFactory: JnpfSheetsInsertFloatImageMenuFactory,
            order: 10,
          },
        },
      },
    });
  }

  private _registerComponents(): void {
    // 注册图片组件进来
    this.disposeWithMe(this._componentManager.register(JnpfUniverFloatImageKey, univerFloatImageComponent, { framework: 'vue3' }));
  }
}
