import DataUtils from '../utils/DataUtils';
import LayerProvider from '../geog/LayerProvider';
import AnnotationLayer from '../common/AnnotationLayer';
import SimpleFillLayer from '../common/SimpleFillLayer';

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

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

    /**
     * 数据源ID
     * @type {String}
     */
    this._sourceId = DataUtils.default(options.sourceId, 'pipe_patrolarea');

    // 图层ID
    this._layerId = this._sourceId;
    this._annoLayerId = this._sourceId + '-anno';

    // 图层
    this._fillLayer = null;
    this._annoLayer = null;
    // 初始化图层
    this._initLayers();
  }

  /** 公有方法 **/
  /**
   * 设置数据
   * @param {Object} data -GeoJSON要素/要素集
   */
  setData(data = null) {
    // 加载图层
    this.loadLayer();

    if (data) {
      this._fillLayer.setData(data);
    }
  }

  /**
   * 设置过滤器
   * @param {Array} filter -过滤器
   */
  setFilter(filter) {
    this._fillLayer.setFilter(filter);
  }

  /**
   * 加载图层
   */
  loadLayer() {
    // 加载图层
    if (!this.hasLayer()) {
      this._fillLayer.loadLayer();

      this._annoLayer.loadLayer();
    }

    this.showLayer(true);

    // 移动图层
    LayerProvider.moveLayer(this._map, this._layerId, 'map_nh_building');
  }

  /**
   * 设置筛选器
   * @param {Array} filter -筛选器
   */
  setFilter(filter) {
    this._fillLayer.setFilter(filter);

    this._annoLayer.setFilter(filter);
  }

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

    this._annoLayer.showLayer(isShow);
  }

  /**
   * 判断图层是否已加载
   *
   * @returns {Boolean}
   */
  hasLayer() {
    return this._fillLayer.hasLayer();
  }

  /**
   * 移除图层
   */
  removeLayer() {
    this._annoLayer.removeLayer();
    this._fillLayer.removeLayer();
  }

  /**
   * 移除数据源
   */
  removeSource() {
    this._fillLayer.removeSource();
  }

  /**
   * 绑定单击事件
   * @param {Function} event -事件函数
   */
  onClick(event) {
    this._fillLayer.onClick(event);
  }

  /**
   * 绑定事件
   * @param {String} name -事件名称
   * @param {Function} event -事件函数
   */
  onEvent(name, event) {
    this._fillLayer.onEvent(name, event);
  }

  /** 私有方法 **/
  /**
   * 初始化图层
   */
  _initLayers() {
    // 填充图层
    this._fillLayer = new SimpleFillLayer({
      map: this._map,
      minzoom: this._minzoom,
      color: '#00CED1',
      opacity: 0.1,
      outline: true,
      lineColor: '#00CED1',
      lineWidth: 2.0,
      lineOpacity: 0.5,
      dasharray: [2.5, 1.5],
      sourceId: this._sourceId,
      layerId: this._layerId
    });

    // 标注图层
    this._annoLayer = new AnnotationLayer({
      map: this._map,
      minzoom: this._minzoom,
      field: 'area_name',
      sourceId: this._sourceId,
      layerId: this._annoLayerId
    });
  }
}
