import { Component, OnInit } from '@angular/core';
import { ViewerService } from 'src/app/services';

const tilesetUrl = 'https://www.south-smart.com/cesium/modeldata/nanfangcehuiBIM/NFCH0911/CHDLFC/tileset.json';

@Component({
  selector: 'app-tileset-clipping',
  templateUrl: './tileset-clipping.component.html',
  styleUrls: ['./tileset-clipping.component.css']
})
export class TilesetClippingComponent implements OnInit {
  public clippingPlane: 'X' | 'Y' | 'Z' = 'Z';

  private tileset;
  private clipping;

  constructor(
    private viewerService: ViewerService,
  ) { }

  ngOnInit() {
  }

  enableClipping(enable?: boolean) {
    const viewer = this.viewerService.viewer;

    if (this.clipping) {
      this.clipping.destroy();
    }

    if (false === enable) {
      return;
    }

    const clippingOptions = {
      dimensions: 2,
      material: Cesium.Color.BLUE.withAlpha(0.2),
      outline: true,
      outlineColor: Cesium.Color.RED,
      edgeStylingEnabled: true,
      debugBoundingVolumesEnabled: true,
    };
    const that = this;

    function createAndStartClipping() {
      that.clipping = new smart3d.Clipping(viewer, clippingOptions);
      that.clipping.start(that.tileset, smart3d.ClippingMode[that.clippingPlane]);
    }

    if (undefined === this.tileset) {
      this.tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
        url: tilesetUrl,
      }));
      this.tileset.readyPromise.then(tileset => {
        createAndStartClipping();
        viewer.flyTo(tileset);
      });
    } else {
      createAndStartClipping();
      viewer.flyTo(this.tileset);
    }
  }
}
