import DataUtils from '../utils/DataUtils';
import VerifyUtils from '../utils/VerifyUtils';
import LayerProvider from '../geog/LayerProvider';
import SimpleMarkerLayer from '../common/SimpleMarkerLayer';
import SimpleLineLayer from '../common/SimpleLineLayer';

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

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

    /**
     * 图标ID
     * @type {String}
     */
    this._icon = DataUtils.default(options.icon, 'busi_flow');

    /**
     * 线颜色 #5F9EA0
     * @type {String}
     */
    this._lineColor = DataUtils.default(options.lineColor, '#FF4500');

    /**
     * 线宽
     * @type {Number}
     */
    this._lineWidth = DataUtils.default(options.lineWidth, 3.5);

    /**
     * 过滤条件数组
     * @type {Array}
     */
    this._filter = DataUtils.default(options.filter, undefined);

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

    /**
     * 图层ID
     * @type {String}
     */
    this._layerId = DataUtils.default(options.layerId, this._sourceId);
    // 流向图层ID
    this._flowLayerId = this._layerId + '-flow';

    /* 图层对象 */
    this._lineLayer = null; // 管线
    this._flowLayer = null; // 流向
    // 初始化图层
    this._initLayers();
  }

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

    if (data) {
      this._lineLayer.setData(data);
      this._flowLayer.setData(data);
    }
  }

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

    if (filter && VerifyUtils.isArray(filter)) {
      // 设置筛选器
      this._lineLayer.setFilter(filter);
      this._flowLayer.setFilter(filter);
    }
  }

  /**
   * 设置绘制属性
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setPaintProperty(name, value) {
    this._lineLayer.setPaintProperty(name, value);
    this._flowLayer.setPaintProperty(name, value);
  }

  /**
   * 设置布局属性
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setLayoutProperty(name, value) {
    this._lineLayer.setLayoutProperty(name, value);
    this._flowLayer.setLayoutProperty(name, value);
  }

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

    // 显示图层
    this.showLayer(true);
  }

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

  /**
   * 判断图层是否已加载
   *
   * @returns {Boolean}
   */
  hasLayer() {
    let has = has = (
      this._lineLayer.hasLayer() &&
      this._flowLayer.hasLayer()
    );

    return has;
  }

  /**
   * 移除图层
   */
  removeLayer() {
    this._flowLayer.removeLayer();
    this._lineLayer.removeLayer();
    // 移除数据源
    this._lineLayer.removeSource();
  }

  /** 私有方法 **/
  /**
   * 初始化图层
   */
  _initLayers() {
    const map = this._map;
    const minZoom = this._minzoom;
    const maxZoom = map.getMaxZoom() - 1;

    // 管线图层
    this._lineLayer = new SimpleLineLayer({
      map: map,
      minzoom: minZoom,
      metrics: true,
      color: this._lineColor,
      width: this._lineWidth,
      dasharray: [3.5, 3.5],
      fixed: true,
      sourceId: this._sourceId,
      layerId: this._layerId
    });

    // 流向图层
    this._flowLayer = new SimpleMarkerLayer({
      map: map,
      minzoom: minZoom,
      icon: this._icon,
      filter: this._filter,
      sourceId: this._sourceId,
      layerId: this._flowLayerId
    });
    // 设置自定义样式
    this._flowLayer.setLayoutProperty('symbol-placement', 'line');
    this._flowLayer.setLayoutProperty('symbol-spacing', [
      "interpolate",
      ["linear"],
      ["zoom"],
      minZoom, 50,
      maxZoom, 250
    ]);
  }
}
