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

/**
 * 定义管网问题图层提供类
 *
 * @alias PipenetFaultProvider
 *
 * @summary <用于加载管线、检查井问题图层>
 */
export default class PipenetFaultProvider {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

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

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

    /**
     * 线颜色
     * @type {String}
     */
    this._lineColor = DataUtils.default(options.color, '#37A2DA');

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

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

    // 管线问题数据源ID
    this._pipelineSourceId = 'pipe_pipeline_fault';
    // 检查井问题数据源ID
    this._manholeSourceId = 'pipe_manhole_fault';

    /** 图层对象 **/
    this._pipelineLayer = null;
    this._manholeLayer = null;

    // 初始化图层
    this._initLayers();
  }

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

    if (data) {
      let features = [];
      if (VerifyUtils.isGeoJsonFC(data)) {
        features = data.features;
      } else if (VerifyUtils.isArray(data)) {
        if (data.length > 0 && VerifyUtils.isGeoJsonFeature(data[0])) {
          features = data;
        }
      }

      // 过滤点数据
      let pointFC = GeoJsonProvider.getEmptyFC();
      pointFC.features = features.filter((feature) => {
        return feature.geometry.type.toLowerCase === 'point';
      });
      // 过滤线数据
      let lineStringFC = GeoJsonProvider.getEmptyFC();
      lineStringFC.features = features.filter((feature) => {
        return feature.geometry.type.toLowerCase === 'linestring';
      });

      this._pipelineLayer.setData(lineStringFC);
      this._manholeLayer.setData(pointFC);
    }
  }

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

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

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

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

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

      // 检查井
      this._manholeLayer.loadLayer();
    }
  }

  /**
   * 显示/隐藏图层
   * @param {String} type -类型(pipeline、manhole)
   * @param {Boolean} isShow -是否显示图层
   */
  showLayer(type, isShow) {
    switch (type) {
      case 'pipeline': { // 管线
        this._pipelineLayer.showLayer(isShow);
        break;
      }
      case 'manhole': { // 检查井
        this._manholeLayer.showLayer(isShow);
      }
      default:
        break;
    }
  }

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

  /**
   * 移除图层
   */
  removeLayer() {
    // 管线
    this._pipelineLayer.removeLayer();

    // 检查井
    this._manholeLayer.removeLayer();
  }

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

    // 管线
    this._pipelineLayer = new SimpleLineLayer({
      map: map,
      minzoom: minZoom,
      color: this._lineColor,
      width: this._lineWidth,
      filter: this._filter,
      fixed: true,
      sourceId: this._pipelineSourceId,
    });

    // 检查井
    this._manholeLayer = new SimpleMarkerLayer({
      map: map,
      minzoom: minZoom,
      icon: this._icon,
      size: 0.75,
      filter: ['==', 'error_type', 'isolated'],
      sourceId: this._manholeSourceId
    });
  }
}
