import DataUtils from "../utils/DataUtils";
import LayerProvider from "../geog/LayerProvider";
import GeoJsonProvider from "../geog/GeoJsonProvider";
import FlowingLayer from "../common/FlowingLayer";
import PipenetFlowingLayer from "../pipenet/PipenetFlowingLayer";

import { pipenetMergeAnalystResult } from "../utils/PipenetUtils";
import { getApi } from "../../api/http";

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

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

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

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

    /**
     * 是否固定(线宽)
     * @type {Boolean}
     */
    this._fixed = DataUtils.default(options.fixed, true);

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

    // 动态流向图层
    //this._flowingLayer = new FlowingLayer({
    this._flowingLayer = new  PipenetFlowingLayer({
      map: this._map,
      minzoom: this._minzoom,
      color: this._lineColor,
      width: this._lineWidth,
      fixed: this._fixed,
      sourceId: this._sourceId
    });
  }

  /** 公有方法 **/
  /**
   * 设置数据
   */
  setData() {
    const fc = GeoJsonProvider.getEmptyFC();

    const url = LayerProvider.getBaseLayerQueryUrl(this._map, 'pipenet-equ');
    if (!url) {
      this._flowingLayer.setData(fc);
      return;
    }

    // 获取地图服务接口
    getApi(url).then((res) => {
      if (res && res.data) {
        fc.features = res.data.features;
        // 合并管线数据
        fc.features = pipenetMergeAnalystResult(fc.features);
      }

      // 设置流向数据
      this._flowingLayer.setData(fc);
      this._flowingLayer.start();
    });
  }

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