import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeResourceType } from '@alilc/lowcode-types';
import { Workspace as InnerWorkSpace } from '@alilc/lowcode-workspace';
import { Plugins } from '@alilc/lowcode-shell';
import { workspaceSymbol } from '../symbols';
import { Resource as ShellResource, Window as ShellWindow } from '../model';

export class Workspace implements IPublicApiWorkspace {
  readonly [workspaceSymbol]: InnerWorkSpace;

  constructor(innerWorkspace: InnerWorkSpace) {
    this[workspaceSymbol] = innerWorkspace;
  }

  get resourceList() {
    return this[workspaceSymbol].getResourceList().map((d) => new ShellResource(d));
  }

  setResourceList(resourceList: IPublicResourceList) {
    this[workspaceSymbol].setResourceList(resourceList);
  }

  onResourceListChange(fn: (resourceList: IPublicResourceList) => void): () => void {
    return this[workspaceSymbol].onResourceListChange(fn);
  }

  get isActive() {
    return this[workspaceSymbol].isActive;
  }

  get window() {
    return new ShellWindow(this[workspaceSymbol].window);
  }

  registerResourceType(resourceTypeModel: IPublicTypeResourceType): void {
    this[workspaceSymbol].registerResourceType(resourceTypeModel);
  }

  openEditorWindow(resourceName: string, title: string, extra: object, viewName?: string) {
    this[workspaceSymbol].openEditorWindow(resourceName, title, extra, viewName);
  }

  openEditorWindowById(id: string) {
    this[workspaceSymbol].openEditorWindowById(id);
  }

  removeEditorWindow(resourceName: string, title: string) {
    this[workspaceSymbol].removeEditorWindow(resourceName, title);
  }

  removeEditorWindowById(id: string) {
    this[workspaceSymbol].removeEditorWindowById(id);
  }

  get plugins() {
    return new Plugins(this[workspaceSymbol].plugins, true);
  }

  get windows() {
    return this[workspaceSymbol].windows.map((d) => new ShellWindow(d));
  }

  onChangeWindows(fn: () => void) {
    return this[workspaceSymbol].onChangeWindows(fn);
  }

  onChangeActiveWindow(fn: () => void) {
    return this[workspaceSymbol].onChangeActiveWindow(fn);
  }
}
