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

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

/**
 * 定义管网连通分析提供类
 *
 * @alias PipenetConnectivityProvider
 *
 * @summary <用于加载管网连通分析图层>
 */
export default class PipenetConnectivityProvider {
  /**
   * 构造函数
   * @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, 10.5);

    // 管网流向图层
    //this._flowingLayer = new FlowingLayer({
    this._flowingLayer = new PipenetFlowingLayer({
      map: this._map,
      minzoom: this._minzoom,
      color: this._lineColor,
      width: this._lineWidth,
      sourceId: 'pipe_flowing-dijkstra'
    });
  }

  /**
   * 设置管线路径颜色
   * @param {String} color -16进制颜色字符串
   */
  setColor(color) {
    this._flowingLayer.setColor(color);
  }

  /**
   * 查询流向数据
   * @param {String} pipeId -起点管线编码
   * @param {String} pipeId2 -终点管线编码
   * @param {Function} callback -回调函数
   */
  queryData(pipeId, pipeId2, callback=null) {
    const map = this._map;
    const that = this;

    /**
     * 查询流向
     * @param {Function} callback -回调函数
     *
     * @returns {Boolean}
     */
    async function _query(callback=null) {
      // 获取管线网络查询url
      let urlFlow = LayerProvider.getBaseLayerQueryUrl(map, 'pipenet', pipeId);
      let urlFlow2 = LayerProvider.getBaseLayerQueryUrl(map, 'pipenet', pipeId2);
      if (!urlFlow || !urlFlow2) return false;

      let result = null, fc = null, fc2 = null;
      let sourceId = 0;
      let targetId = 0;
      // 查询起点
      result = await getApi(urlFlow);
      fc = result.data
      if (fc && fc.features && fc.features.length > 0) {
        sourceId = fc.features[0].properties.source;
      }
      if (sourceId < 1) return false;

      // 查询终点
      result = await getApi(urlFlow2);
      fc2 = result.data
      if (fc2 && fc2.features && fc2.features.length > 0) {
        targetId = fc2.features[0].properties.target;
      }
      if (targetId < 1) return false;

      // 获取管线最短路径查询url
      let urlQuery = LayerProvider.getBaseLayerQueryUrl(map, 'pipenet-dijkstra', sourceId, targetId);
      if (!urlQuery) return false;

      // 获取地图服务接口数据
      await getApi(urlQuery).then((res) => {
        const resultFC = res.data;
        if (resultFC.features && resultFC.features.length > 0) {
          // 获取唯一要素集合
          resultFC.features = GeoJsonProvider.getUniqueFeatures(resultFC.features, 'path');

          // 合并管线数据
          const mergedFC = GeoJsonProvider.getEmptyFC();
          mergedFC.features = pipenetMergeAnalystResult(resultFC.features);

          // 设置流向数据
          that._flowingLayer.setData(mergedFC);
          // 开始动画
          that._flowingLayer.start();
        } else {
          resultFC.features = [fc.features[0], fc2.features[0]];
        }

        // 回调
        if (callback) {
          callback('connectivity', resultFC);
        }

      }).catch((err) => {
        console.log('connectivity', err);
      });
    }

    _query(callback);
  }

  /**
   * 停止流向
   */
  stop() {
    this._flowingLayer.stop();
    // 移除图层
    this.removeLayer();
  }

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

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