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

@Component({
  selector: 'app-viewshed',
  templateUrl: './viewshed.component.html',
  styleUrls: ['./viewshed.component.css']
})
export class ViewshedComponent implements OnInit {

  public horizontalAngle = 45;
  public verticalAngle = 60;
  public distance = 100;
  public showFrustum = true;
  public showViewshed = true;

  private viewshed;

  constructor(
    private viewerService: ViewerService,
  ) { }

  ngOnInit() {
  }

  buildViewshed() {
    const viewer = this.viewerService.viewer;
    const drawHandler = new smart3d.DrawHandler(viewer, smart3d.DrawMode.Line);
    const positions = [];
    drawHandler.anchorEvent.addEventListener(position => {
      if (positions.push(position) >= 2) {
        drawHandler.clear();

        const options = {
          cameraPosition: positions[0],
          viewPosition: positions[1],
          horizontalAngle: this.horizontalAngle,
          verticalAngle: this.verticalAngle,
          distance: this.distance,
          frustum: this.showFrustum,
          show: this.showViewshed,
          visibleAreaColor: Cesium.Color.BLUE,
          hiddenAreaColor: Cesium.Color.YELLOW,
          alpha: 1,
        };
        // 需要开启 depthTestAgainstTerrain，可视域才能贴地
        if (viewer.scene.globe.depthTestAgainstTerrain !== true) {
          viewer.scene.globe.depthTestAgainstTerrain = true;
        }
        if (this.viewshed) {
          this.viewshed.destroy();
        }
        this.viewshed = new smart3d.ViewShed3D(viewer, options);
        this.viewshed.start();

        drawHandler.destroy();
      }
    });
    drawHandler.activate();
  }

}
