/* 
TODO 修改完成
*/
import { insidePolygon, pointInsideCircle, pointInBuffer } from './utils/geo';
import * as L from 'leaflet';
import '@src/libs/echarts-leaflet';
import 'leaflet/dist/leaflet.css';
// import "leaflet.chinesetmsproviders";
import 'mapbox-gl-leaflet';

import { LMConfig } from "@zenview/micro-basic-library";


class MapInstance {
  constructor() {
    this.mapInstance = null;
  }
  __createMap(ele, config, mapOptions) {
    this.mapOptions = Object.assign({}, LMConfig.LMapConfig, mapOptions);

    const { url, urls, isWmts, wmtsLayerOptions } = LMConfig.LMapConfig || {};
    let layerUrls=urls||[url]
    let tileLayers=[]
    if (isWmts) {
      layerUrls.forEach(v => {
        tileLayers.push(L.tileLayer.wmts(v, wmtsLayerOptions))
      })
    } else {
      layerUrls.forEach(v => {
        tileLayers.push(L.tileLayer(v, {
          ...this.mapOptions.layerOptions,
          subdomains: this.mapOptions.subdomains || [],
          detectRetina: false,
        }))
      })
    }

    this.mapConfig = Object.assign({}, config, {
      layers: tileLayers,
      zoomControl: false,
      attributionControl: false,
      center: [this.mapOptions.layerOptions.center[0],this.mapOptions.layerOptions.center[1]],
      maxZoom: this.mapOptions.layerOptions.maxZoom,
      minZoom: this.mapOptions.layerOptions.minZoom,
      zoom: this.mapOptions.layerOptions.zoom,
      ...(this.mapOptions.maxBounds && this.mapOptions.maxBounds.length > 0 ? { maxBounds: L.latLngBounds(this.mapOptions.maxBounds[0], this.mapOptions.maxBounds[1]) } : {}),
    });
    if (this.mapOptions.crs) {
      this.mapConfig.crs = L.CRS[this.mapOptions.crs]|| L.CRS.EPSG3857;
    }

    this.mapInstance = L.map(ele, this.mapConfig);
    window.lmap = this.mapInstance;
  }

  addLayer(layer) {
    this.mapInstance.addLayer(layer);
  }
  removeLayer(layer) {
    this.mapInstance.removeLayer(layer);
  }

  addOverlayers(layers = []) {
    layers.forEach((v) => this.addLayer(v));
  }
  removeOverlayers(layers = []) {
    layers.forEach((v) => this.removeLayer(v));
  }
  get map() {
    return this.mapInstance ? this.mapInstance : undefined;
  }
  setZoomAndCenter(zoom, center) {
    this.mapInstance.setView(center, zoom);
  }
  setZoom(zoom) {
    return this.mapInstance.setZoom(zoom);
  }
  getZoom() {
    return this.mapInstance.getZoom();
  }
  setCenter(center) {
    this.setZoomAndCenter(this.getZoom(), center);
  }
  getCenter() {
    let center = this.mapInstance.getCenter();
    return [center.lat, center.lng];
  }
  /**
   * 获取中心点和缩放级别
   */
  getZoomAndCenter() {
    return {
      zoom: this.mapInstance.getZoom(),
      center: this.mapInstance.getCenter(),
    };
  }

  latLngToLayerPoint(latlng) {
    return this.mapInstance.latLngToLayerPoint(latlng);
  }
  layerPointToLatLng(point) {
    return this.mapInstance.layerPointToLatLng(point);
  }

  setBounds(latLngBounds) {
    this.mapInstance.fitBounds(latLngBounds);
  }
  /**
   * 计算点位是否在缓冲区中
   */
  computedPointsInBuffer(point, buffer) {
    return pointInBuffer(point, buffer);
  }

  /**
   * 计算区域的多个点位
   */
  computedPointsInArea(list, area) {
    return list.filter((v) => {
      if (v.longitude && v.latitude) {
        return this.computedPointInArea([v.latitude, v.longitude], area);
      } else {
        return false;
      }
    });
  }
  /**
   * 计算区域的点位
   */
  computedPointInArea(testPoint, points) {
    return insidePolygon(testPoint, points);
  }

  /**
   * 计算多个点是否在园内
   */
  computedPointsInCircle(list, circle, r) {
    return list.filter((v) => {
      if (v.longitude && v.latitude) {
        return this.computedPointInCircle([v.latitude, v.longitude], circle, r);
      } else {
        return false;
      }
    });
  }
  /**
   * 计算点是否在园内
   */
  computedPointInCircle(point, circle, r) {
    return pointInsideCircle(point, circle, r);
  }

  destroy() {
    this.mapInstance && this.mapInstance.remove();
  }
}

export default MapInstance;
