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

@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 clipping;

  constructor(
    private viewerService: ViewerService,
  ) { }

  ngOnInit() {
  }

  enableClipping(enable?: boolean) {
    const currentLayerPrimitive = this.viewerService.getPrimitiveByLayerName(this.viewerService.currentLayerName);
    if (!(currentLayerPrimitive instanceof Cesium.Cesium3DTileset)) {
      alert('当前图层不是模型、倾斜或点云，不能编辑。');
      return;
    }

    const viewer = this.viewerService.viewer;

    if (undefined !== 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,
    };

    this.clipping = new smart3d.Clipping(viewer, clippingOptions);
    this.clipping.start(currentLayerPrimitive, smart3d.ClippingMode[this.clippingPlane]);
    viewer.flyTo(currentLayerPrimitive);
  }

}
