import DataUtils from '../utils/DataUtils';
import GeogUtils from "../utils/GeogUtils";
import VerifyUtils from "../utils/VerifyUtils";
import GeoJsonProvider from "../geog/GeoJsonProvider";
import SimpleLineLayer from "../common/SimpleLineLayer";
import SimpleFillLayer from '../common/SimpleFillLayer';

/**
 * 缓冲区图层
 * @alias BufferLayer
 *
 * @summary <用于缓冲区加载>
 */
 export default class BufferLayer {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

    /**
     * 最小缩放层级
     * @type {Number}
     */
    this._minzoom = DataUtils.default(options.minzoom, 10);

    /**
     * 填充颜色
     * @type {String}
     */
    this._fillColor = DataUtils.default(options.color, '#00FFF0');

    /**
     * 外边框颜色
     * @type {String}
     */
    this._lineColor = DataUtils.default(options.lineColor, '#00EEFF');

    // 中心几何图形(点、折线)
    this._centerGeom = null;
    // 缓冲区要素集
    this._fc = GeoJsonProvider.getEmptyFC();

    // 缓冲区数据源
    this._sourceId = 'polygon_buffer';
    this._layerId = this._sourceId;

    // 缓冲区边框线数据源
    this._outlineSourceId = this._sourceId + '-outline';
    this._outlineLayerId = this._layerId + '-outline';

    // 填充图层
    this._fillLayer = new SimpleFillLayer({
      map: this._map,
      minzoom: this._minzoom,
      color: this._fillColor,
      opacity: 0.25,
      sourceId: this._sourceId,
      layerId: this._layerId
    });
    // 边框线图层
    this._outlineLayer = new SimpleLineLayer({
      map: this._map,
      minzoom: this._minzoom,
      opacity: 0.5,
      width: 2.5,
      color: this._lineColor,
      sourceId: this._outlineSourceId,
      layerId: this._outlineLayerId
    });

    // 缓冲区外框线图层事件
    this._enterEvent = null;
    this._moveEvent = null;
    this._clickEvent = null;
  }

  /**
   * 设置数据
   * @param {Object} data -GeoJSON要素/要素集
   * @param {Number} radius -半径 (单位：kilometers)
   */
  setData(data, radius = 0.2) {
    let queryFea;
    if (VerifyUtils.isGeoJsonFC(data)) {
      queryFea = data.features[0];
    } else if (VerifyUtils.isGeoJsonFeature(data)) {
      queryFea = data;
    }

    if (!queryFea) return;

    this._centerGeom = queryFea.geometry;

    // 创建缓冲区
    let buffFea = GeogUtils.buffer(queryFea, radius);
    this._fc.features = [ buffFea ];

    // 设置数据
    this._fillLayer.setData(this._fc);
    this._outlineLayer.setData(this._fc);
  }

  /**
   * 获取数据
   *
   * @returns {Object}
   */
  getData() {
    return this._fc;
  }

  /**
   * 显示/隐藏图层
   * @param {Boolean} isShow -是否显示图层
   */
  showLayer(isShow) {
    this._fillLayer.showLayer(isShow);
    this._outlineLayer.showLayer(isShow);
  }

  /**
   * 删除图层
   */
  removeLayer() {
    // 移除事件监听
    this._map.off('mouseenter', this._outlineLayerId, this._enterEvent);

    this._fillLayer.removeLayer();
    this._outlineLayer.removeLayer();

    this._fillLayer.removeSource();
    this._outlineLayer.removeSource();

    this._fc.features = [];
  }
}
