import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  effect,
  inject,
  input,
  OnDestroy
} from '@angular/core';

import VectorTileLayer from 'ol/layer/VectorTile';
import { applyStyle } from 'ol-mapbox-style';

import { VectorLayerStyleFactory } from './vectorLayerStyleFactory';
import { MapComponent } from '../map.component';

import { FBChart } from 'src/app/types';
import { extentFromBounds } from './chart-utils';

// ** Freeboard S-57 Chart **
@Component({
  selector: 'ol-map > fb-s57-chart',
  template: '<ng-content></ng-content>',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false
})
export class S57ChartLayerComponent implements OnDestroy {
  protected chart = input<FBChart>();
  protected zIndex = input<number>();

  private layer: VectorTileLayer;
  private vectorLayerStyleFactory = inject(VectorLayerStyleFactory);
  private changeDetectorRef = inject(ChangeDetectorRef);
  private mapComponent = inject(MapComponent);

  constructor() {
    this.changeDetectorRef.detach();
    effect(() => {
      this.chart();
      this.zIndex();
      this.parseChart();
    });
  }

  ngOnDestroy() {
    const map = this.mapComponent.getMap();
    if (this.layer) {
      map.removeLayer(this.layer);
      map.render();
    }
  }

  private parseChart(chart: FBChart = this.chart()) {
    const map = this.mapComponent.getMap();
    if (!map) {
      return;
    }

    if (!this.layer) {
      const minZ =
        chart[1].minZoom && chart[1].minZoom >= 0.1
          ? chart[1].minZoom - 0.1
          : chart[1].minZoom;
      const maxZ = chart[1].maxZoom;

      const styleFactory = this.vectorLayerStyleFactory.CreateVectorLayerStyler(
        chart[1]
      );
      this.layer = styleFactory.CreateLayer();
      if (this.layer) {
        styleFactory.ApplyStyle(this.layer as VectorTileLayer<never>);
        this.layer.setZIndex(this.zIndex());
        this.layer.setOpacity(chart[1].defaultOpacity ?? 1);
        if (chart[1].style) {
          applyStyle(this.layer as any, chart[1].style);
        }
        this.layer.setExtent(extentFromBounds(chart[1].bounds));

        this.layer.set('id', chart[0]);
        this.layer.set('chartId', chart[0]);
        this.layer.set('chartType', chart[1].type);
        this.layer.set('chartFormat', chart[1].format);
        map.addLayer(this.layer);
      }
    } else {
      this.layer.setZIndex(this.zIndex());
      this.layer.setOpacity(chart[1].defaultOpacity ?? 1);
      this.layer.setExtent(extentFromBounds(chart[1].bounds));
    }
    map.render();
  }
}
