import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ViewerService {

  // tslint:disable-next-line: variable-name
  private _viewer;
  private layerManager;
  private measureHandler;

  constructor() {
    console.log('%c viewer service constructed! ', 'background-color: red; color: white; padding: 5px');
    this.handleMeasure = this.handleMeasure.bind(this);
  }

  get viewer() {
    return this._viewer;
  }

  initViewer(container: string | Element, options?: object): object {
    if (!this._viewer) {
      this._viewer = new smart3d.Viewer(container, options || {});
      this.layerManager = new smart3d.LayerManager(this._viewer);

      // tslint:disable-next-line: no-string-literal
      window['viewer'] = this._viewer;
    }
    return this._viewer;
  }

  addLayer(layer) {
    this.layerManager.addLayer(layer);
  }

  flyToLayer(layer) {
    this.layerManager.flyTo(layer);
  }

  handleMeasure(measureMode: number, isGround?: boolean, showLabel?: boolean) {
    if (
      measureMode !== smart3d.MeasureMode.Distance &&
      measureMode !== smart3d.MeasureMode.Angle &&
      measureMode !== smart3d.MeasureMode.Area &&
      measureMode !== smart3d.MeasureMode.DVH
    ) {
      throw new Cesium.DeveloperError('measure mode must be one of smart3d.MeasureMode');
    }

    isGround = undefined === isGround ? false : isGround;
    showLabel = undefined === showLabel ? true : showLabel;

    if (this._viewer) {
      this.measureHandler = new smart3d.MeasureHandler(this.viewer, measureMode, isGround, showLabel);
      this.measureHandler.activate();
    }
  }
}
