import { Component, OnInit,Input,Host  } from '@angular/core';
import { Interaction} from 'ol/interaction';
import { MapComponent } from '@farmmaps/ng-openlayers';
import OLCesium from 'olcs/OLCesium';
import RasterSynchronizer from 'olcs/RasterSynchronizer';
import VectorSynchronizer from 'olcs/VectorSynchronizer';
import { mapReducers,mapActions } from '@farmmaps/common-map';
import { Store } from '@ngrx/store';

@Component({
    selector: 'fm-map3d-switch2d3d',
    templateUrl: './switch2d3d.component.html',
    styleUrls: ['./switch2d3d.component.scss'],
    standalone: false
})
export class Switch2D3DComponent {

  @Input() enable:boolean;
  public label = "3D";
  private ol3d: OLCesium;
  private synchronizers:any[];
  public loading = true;
  private interactions:Interaction[] = [];


  constructor(private map: MapComponent,private store: Store<mapReducers.State>) {
    
  }

  ngOnInit() {   
   
    this.ol3d = new OLCesium({ map: this.map.instance, createSynchronizers: (map,scene) => {
        this.synchronizers = [
            new RasterSynchronizer(map,scene),
            new VectorSynchronizer(map,scene)
        ];
        this.loading=false;
        return this.synchronizers;
    }, stopOpenLayersEventsPropagation:true}); 
    this.ol3d.warmUp(3000,5000);
  }

  synchronize() {
      this.synchronizers.forEach((synchronizer) => {
        synchronizer.synchronize();
      });
  }

  disableInteractions() {
      this.interactions=[];
      this.map.instance.getInteractions().forEach((i) => {
        if(i.getActive()) {
          // AW-6241 TODO How to fix?
          // error TS2345: Argument of type 'import("C:/Project/Farmmaps/FarmMapsLib/node_modules/ol/interaction/Interaction").default'
          // is not assignable to parameter of type 'import("C:/Project/Farmmaps/FarmMapsLib/projects/common-map3d/node_modules/ol/interaction/Interaction").default'.
          // Line below commented out
          // this.interactions.push(i);
          i.setActive(false);
        }
      });
  }

  enableInteractions() {
    this.interactions.forEach((i) => {
      i.setActive(true);
    });
  }

  handleClick(event) {
    this.enable = !this.enable; 
    if(this.enable) {
      this.store.dispatch(new mapActions.SetViewState(false));
      this.disableInteractions();
      this.synchronize();
      this.ol3d.setEnabled(true);
    } else {
      this.ol3d.setEnabled(false);
      this.enableInteractions();
      this.store.dispatch(new mapActions.SetViewState(true));
    }
    
    this.label = this.enable?"2D":"3D";    
  }
}
