<template>
  <div class="vue2leaflet-map">
    <slot v-if="ready" />
  </div>
</template>

<script>
import { optionsMerger, propsBinder, debounce } from '../utils/utils.js';
import Options from '../mixins/Options.js';
import { CRS, DomEvent, map, latLngBounds, latLng } from 'leaflet';

/**
 * 基本组件，包含并用此包住所有其他retina-gis组件。
 */
export default {
  name: 'LMap',
  mixins: [Options],
  props: {
    /**
     * 地图中心，支持 .sync 修饰符
     */
    center: {
      type: [Object, Array],
      custom: true,
      default: () => [0, 0],
    },
    /**
     * 地图边界，支持 .sync 修饰符
     */
    bounds: {
      type: [Array, Object],
      custom: true,
      default: null,
    },
    /**
     * 地图最大边界
     */
    maxBounds: {
      type: [Array, Object],
      default: null,
    },
    /**
     * 地图缩放级别，支持 .sync 修饰符
     */
    zoom: {
      type: Number,
      custom: true,
      default: 0,
    },
    /**
     * 地图最小缩放级别
     */
    minZoom: {
      type: Number,
      default: null,
    },
    /**
     * 地图最大缩放级别
     */
    maxZoom: {
      type: Number,
      default: null,
    },
    /**
     * The paddingBottomRight of the map
     * 地图的填充底部右侧
     */
    paddingBottomRight: {
      type: Array,
      custom: true,
      default: null,
    },
    /**
     * The paddingTopLeft of the map
     */
    paddingTopLeft: {
      type: Array,
      custom: true,
      default: null,
    },
    /**
     * The padding of the map
     */
    padding: {
      type: Array,
      custom: true,
      default: null,
    },
    /**
     * The worldCopyJump option for the map
     */
    worldCopyJump: {
      type: Boolean,
      default: false,
    },
    /**
     * The crs option for the map
     * @values CRS.EPSG3857
     */
    crs: {
      type: Object,
      custom: true,
      default: () => CRS.EPSG3857,
    },
    maxBoundsViscosity: {
      type: Number,
      default: null,
    },
    inertia: {
      type: Boolean,
      default: null,
    },
    inertiaDeceleration: {
      type: Number,
      default: null,
    },
    inertiaMaxSpeed: {
      type: Number,
      default: null,
    },
    easeLinearity: {
      type: Number,
      default: null,
    },
    zoomAnimation: {
      type: Boolean,
      default: null,
    },
    zoomAnimationThreshold: {
      type: Number,
      default: null,
    },
    fadeAnimation: {
      type: Boolean,
      default: null,
    },
    markerZoomAnimation: {
      type: Boolean,
      default: null,
    },
    noBlockingAnimations: {
      type: Boolean,
      default: false,
    },
  },
  data () {
    return {
      ready: false,
      lastSetCenter: null,
      lastSetBounds: null,
      lastSetZoom: null,
      layerControl: undefined,
      layersToAdd: [],
      layersInControl: [],
    };
  },
  computed: {
    fitBoundsOptions () {
      const options = {
        animate: this.noBlockingAnimations ? false : null,
      };
      if (this.padding) {
        options.padding = this.padding;
      } else {
        if (this.paddingBottomRight) {
          options.paddingBottomRight = this.paddingBottomRight;
        }
        if (this.paddingTopLeft) {
          options.paddingTopLeft = this.paddingTopLeft;
        }
      }
      return options;
    },
  },
  beforeDestroy () {
    if (this.mapObject) {
      this.mapObject.remove();
    }
  },
  mounted () {
    const options = optionsMerger(
      {
        minZoom: this.minZoom,
        maxZoom: this.maxZoom,
        maxBounds: this.maxBounds,
        maxBoundsViscosity: this.maxBoundsViscosity,
        worldCopyJump: this.worldCopyJump,
        crs: this.crs,
        center: this.center,
        zoom: this.zoom,
        inertia: this.inertia,
        inertiaDeceleration: this.inertiaDeceleration,
        inertiaMaxSpeed: this.inertiaMaxSpeed,
        easeLinearity: this.easeLinearity,
        zoomAnimation: this.zoomAnimation,
        zoomAnimationThreshold: this.zoomAnimationThreshold,
        fadeAnimation: this.fadeAnimation,
        markerZoomAnimation: this.markerZoomAnimation,
      },
      this
    );
    this.mapObject = map(this.$el, options);
    this.setBounds(this.bounds);
    this.mapObject.on('moveend', debounce(this.moveEndHandler, 100));
    this.mapObject.on('overlayadd', this.overlayAddHandler);
    this.mapObject.on('overlayremove', this.overlayRemoveHandler);
    DomEvent.on(this.mapObject, this.$listeners);
    propsBinder(this, this.mapObject, this.$options.props);
    this.ready = true;
    /**
     * DEPRECATED event
     * @deprecated
     */
    this.$emit('leaflet:load');
    this.$nextTick(() => {
      /**
       * Triggers when the component is ready
       * @type {object}
       * @property {object} mapObject - reference to leaflet map object
       */
      this.$emit('ready', this.mapObject);
    });
  },
  methods: {
    registerLayerControl (lControlLayers) {
      this.layerControl = lControlLayers;
      this.mapObject.addControl(lControlLayers.mapObject);
      this.layersToAdd.forEach(layer => {
        this.layerControl.addLayer(layer);
      });
      this.layersToAdd = [];
    },
    addLayer (layer, alreadyAdded) {
      if (layer.layerType !== undefined) {
        if (this.layerControl === undefined) {
          this.layersToAdd.push(layer);
        } else {
          const exist = this.layersInControl.find(
            l => l.mapObject._leaflet_id === layer.mapObject._leaflet_id
          );
          if (!exist) {
            this.layerControl.addLayer(layer);
            this.layersInControl.push(layer);
          }
        }
      }
      if (!alreadyAdded && layer.visible !== false) {
        this.mapObject.addLayer(layer.mapObject);
      }
    },
    hideLayer (layer) {
      this.mapObject.removeLayer(layer.mapObject);
    },
    removeLayer (layer, alreadyRemoved) {
      if (layer.layerType !== undefined) {
        if (this.layerControl === undefined) {
          this.layersToAdd = this.layersToAdd.filter(
            l => l.name !== layer.name
          );
        } else {
          this.layerControl.removeLayer(layer);
          this.layersInControl = this.layersInControl.filter(
            l => l.mapObject._leaflet_id !== layer.mapObject._leaflet_id
          );
        }
      }
      if (!alreadyRemoved) {
        this.mapObject.removeLayer(layer.mapObject);
      }
    },
    setZoom (newVal, oldVal) {
      this.mapObject.setZoom(newVal, {
        animate: this.noBlockingAnimations ? false : null,
      });
    },
    setCenter (newVal, oldVal) {
      if (newVal == null) {
        return;
      }
      const newCenter = latLng(newVal);
      const oldCenter = this.lastSetCenter || this.mapObject.getCenter();
      if (oldCenter.lat !== newCenter.lat || oldCenter.lng !== newCenter.lng) {
        this.lastSetCenter = newCenter;
        this.mapObject.panTo(newCenter, {
          animate: this.noBlockingAnimations ? false : null,
        });
      }
    },
    setBounds (newVal, oldVal) {
      if (!newVal) {
        return;
      }
      const newBounds = latLngBounds(newVal);
      if (!newBounds.isValid()) {
        return;
      }
      const oldBounds = this.lastSetBounds || this.mapObject.getBounds();
      const boundsChanged = !oldBounds.equals(newBounds, 0); // set maxMargin to 0 - check exact equals
      if (boundsChanged) {
        this.lastSetBounds = newBounds;
        this.mapObject.fitBounds(newBounds, this.fitBoundsOptions);
      }
    },
    setPaddingBottomRight (newVal, oldVal) {
      this.paddingBottomRight = newVal;
    },
    setPaddingTopLeft (newVal, oldVal) {
      this.paddingTopLeft = newVal;
    },
    setPadding (newVal, oldVal) {
      this.padding = newVal;
    },
    setCrs (newVal, oldVal) {
      const mapObject = this.mapObject,
        prevBounds = mapObject.getBounds();
      mapObject.options.crs = newVal;
      mapObject.fitBounds(prevBounds, { animate: false, padding: [0, 0] });
    },
    fitBounds (bounds) {
      this.mapObject.fitBounds(bounds, {
        animate: this.noBlockingAnimations ? false : null,
      });
    },
    moveEndHandler () {
      /**
       * Triggers when zoom is updated
       * @type {number,string}
       */
      this.$emit('update:zoom', this.mapObject.getZoom());
      const center = this.mapObject.getCenter();
      /**
       * Triggers when center is updated
       * @type {object,array}
       */
      this.$emit('update:center', center);
      const bounds = this.mapObject.getBounds();
      /**
       * Triggers when bounds are updated
       * @type {object}
       */
      this.$emit('update:bounds', bounds);
    },
    overlayAddHandler (e) {
      const layer = this.layersInControl.find(l => l.name === e.name);
      if (layer) {
        layer.updateVisibleProp(true);
      }
    },
    overlayRemoveHandler (e) {
      const layer = this.layersInControl.find(l => l.name === e.name);
      if (layer) {
        layer.updateVisibleProp(false);
      }
    },
  },
};
</script>

<style type="text/css">
.vue2leaflet-map {
  height: 100%;
  width: 100%;
}
</style>

<docs>
::: tip 提示
If your markup does not have a root `<l-map>` something is wrong.
:::

## 示例
::: demo
<template>
  <div style="height: 350px;">
    <div class="info" style="height: 15%">
      <span>Center: {{ center }}</span>
      <span>Zoom: {{ zoom }}</span>
      <span>Bounds: {{ bounds }}</span>
    </div>
    <l-map
      style="height: 80%; width: 100%"
      :zoom="zoom"
      :center="center"
      :paddingTopLeft="paddingTopLeft"
      @update:zoom="zoomUpdated"
      @update:center="centerUpdated"
      @update:bounds="boundsUpdated"
    >
      <l-tile-layer :url="url"></l-tile-layer>
    </l-map>
  </div>
</template>

<script>
import { LMap, LTileLayer } from 'retina-gis';

export default {
  components: {
    LMap,
    LTileLayer,
  },
  data () {
    return {
      url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      zoom: 15,
      center: [31.973612,118.750328],
      paddingTopLeft:[400, 400],
      bounds: null
    };
  },
  methods: {
    zoomUpdated (zoom) {
      this.zoom = zoom;
    },
    centerUpdated (center) {
      this.center = center;
    },
    boundsUpdated (bounds) {
      this.bounds = bounds;
    }
  }
}
</script>
:::
</docs>
