/*
 * goeditor-setup.js
 * Called before init() events in every goeditor app
 * Initializes the diagrams, palettes, and the Inspector
 * You may need to edit or override things in your own code
 * DO NOT edit this file -- make app-specific overrides or custom changes in your own code
 * DO NOT delete or rename this file
 */

import * as go from 'gojs';
import { Inspector } from './DataInspector.js';

export class EditorHelper {

  public diagrams: Array<go.Diagram>;
  public overviews: Array<go.Overview>;
  public palettes: Array<go.Palette>;
  public defaultModel: string | undefined;

  public JQUERY: any;

  public inspector: Inspector;

  constructor(diagramsCount: number, palettesCount: number, diagramsType: any, JQUERY: any) {
    if (diagramsType === null || !diagramsType) {
      diagramsType = go.Diagram;
    }

    JQUERY = JQUERY;

    // build diagrams
    this.diagrams = [];
    this.overviews = [];
    const editorHelper = this;
    for (let i = 0; i < diagramsCount; i++) {
      const diagram = new diagramsType('ge-diagram-' + i);  // create a Diagram for the DIV HTML element
      diagram.undoManager.isEnabled = true;
      // When diagram is modified, change title to include a *
      diagram.addChangedListener(function(e: go.InputEvent) {
        // maybe update the file header
        const currentFile = document.getElementById('ge-filename') as HTMLElement;
        if (currentFile) {
          const idx: number = (currentFile.textContent as string).indexOf('*');
          if (e.diagram.isModified) {
            if (idx < 0) currentFile.textContent = currentFile.textContent + '*';
          } else {
            if (idx >= 0) currentFile.textContent = (currentFile.textContent as string).slice(0, idx);
          }
        }
      });

      this.diagrams[i] = diagram;

      // make an overview for each diagram
      const overview = new go.Overview('ge-overview-' + i, { observed: diagram });
      this.overviews[i] = overview;

    }

    // if there are no diagrams, there will be no overviews, so do not list that option in View menu
    if (diagramsCount < 1) {
      const viewOverviewsOption = document.getElementById('ge-viewoption-overviews') as Node;
      (viewOverviewsOption.parentNode as Node).removeChild(viewOverviewsOption);
    }

    // build palette(s)
    this.palettes = [];
    for (let i = 0; i < palettesCount; i++) {
      const palette = new go.Palette('ge-palette-' + i);
      this.palettes[i] = palette;
    }

    // Format the inspector for your specific needs. You may need to edit the DataInspector class
    this.inspector = new Inspector('ge-inspector', this.diagrams[0],
      {
        includesOwnProperties: true
      }
    );
  }

  // Small, generic helper functions
  public refreshDraggableWindows() {
    this.JQUERY('.gt-menu').draggable({ handle: '.gt-handle', stack: '.gt-menu', containment: 'window', scroll: false });
  }

  public geHideShowWindow(id: string, doShow?: boolean) {
    const geWindow = document.getElementById(id) as HTMLElement;
    let vis: string | null = null;
    if (doShow === undefined) vis = geWindow.style.visibility === 'visible' ? 'hidden' : 'visible';
    else if (doShow) vis = 'visible';
    else vis = 'hidden';

    let pn: ParentNode | null = null;
    if (((geWindow as HTMLElement).parentNode as HTMLElement).classList.contains('ge-menu')) {
      pn = geWindow.parentNode;
    }
    if (pn) {
      (pn as HTMLElement).style.visibility = vis;
    }
    geWindow.style.visibility = vis;
  }
}
