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

@Component({
  selector: 'app-particle-effect',
  templateUrl: './particle-effect.component.html',
  styleUrls: ['./particle-effect.component.css']
})
export class ParticleEffectComponent implements OnInit {
  private particleEffect;
  public rainEffect;
  public snowEffect;
  public fogEffect;

  constructor(
    private viewerService: ViewerService,
  ) { }

  ngOnInit(
  ) {
    const viewer = this.viewerService.viewer;
    this.particleEffect = new smart3d.ParticleEffect(viewer);
  }

  disableEffect(type: string) {
    switch (type) {
      case 'rain':
        if (this.rainEffect) {
          this.rainEffect.clear();
          this.rainEffect = undefined;
        }
        break;
      case 'snow':
        if (this.snowEffect) {
          this.snowEffect.clear();
          this.snowEffect = undefined;
        }
        break;
      case 'fog':
        if (this.fogEffect) {
          this.fogEffect.clear();
          this.fogEffect = undefined;
        }
        break;
    }
  }

  enableParticleEffect(type: string) {
    const viewer = this.viewerService.viewer;
    const cartesian = Cesium.Cartesian3.fromDegrees(115, 24, 250);
    let options;

    switch (type) {
      case 'rain':
        if (undefined === this.rainEffect) {
          this.rainEffect = new smart3d.RainEffect(viewer.scene);
          this.rainEffect.add();
        }
        break;
      case 'snow':
        if (undefined === this.snowEffect) {
          this.snowEffect = new smart3d.SnowEffect(viewer.scene);
          this.snowEffect.add();
        }
        break;
      case 'fog':
        if (undefined === this.fogEffect) {
          this.fogEffect = new smart3d.FogEffect(viewer.scene);
          this.fogEffect.add();
        }
        break;
      case 'smoke':
        const entity = viewer.entities.add({
          position: cartesian
        });
        options = {
          particleMode: smart3d.ParticleMode.SMOKE,
          entity,
          minimumSpeed: 1,
          maximumSpeed: 5,
          minimumParticleLife: 1,
          maximumParticleLife: 4,
          startScale: 1,
          endScale: 5,
          emissionRate: 5,
          imageSize: 25
        };
        this.particleEffect.add(options);
        viewer.camera.lookAt(cartesian, new Cesium.Cartesian3(-100, 0, 100));
        viewer.camera._setTransform(Cesium.Matrix4.IDENTITY);
        break;
      case 'fire':
        const fireEntity = viewer.entities.add({
          position: cartesian
        });
        options = {
          particleMode: smart3d.ParticleMode.FIRE,
          entity: fireEntity,
          minimumSpeed: 1,
          maximumSpeed: 5,
          minimumParticleLife: 1,
          maximumParticleLife: 20,
          startScale: 0,
          endScale: 10,
          emissionRate: 14,
          imageSize: 10
        };
        this.particleEffect.add(options);
        viewer.camera.lookAt(cartesian, new Cesium.Cartesian3(-100, 0, 100));
        viewer.camera._setTransform(Cesium.Matrix4.IDENTITY);
        break;
    }

  }

  handleChangeRange(type: string, e): void {
    const value: number = Cesium.Math.clamp(e.target.value, 0.0, 1.0);
    switch (type) {
      case 'rain':
        break;
      case 'snow':
        this.snowEffect.update(value);
        break;
      case 'fog':
        this.fogEffect.update(value);
        break;
    }
  }
}
