import GeoJsonProvider from "../geog/GeoJsonProvider";

/**
 * 合并管网分析结果
 * @param {Array} features -要素集合
 *
 * @returns {Array} 合并后的管段要素数组
 */
export function pipenetMergeAnalystResult(features) {
  const resultFeatures = [];

  if (features && features.length) {
    // 起始要素
    let startFeature = GeoJsonProvider.getLineStringFeature(features[0].geometry.coordinates, {
      source: features[0].properties.source,
      target: features[0].properties.target,
      path: features[0].properties.path,
    });
    features[0].properties.ignored = true;
    // 添加到结果要素数组
    resultFeatures.push(startFeature);

    for (let i = 1; i < features.length; i++) {
      const feature = features[i];
      if (feature.properties.ignored) continue;

      let filteredFeas = filterFeatures(features, startFeature);
      while (filteredFeas.length === 1) {
        const filteredFea = filteredFeas[0];
        filteredFea.properties.ignored = true;

        switch (filteredFea.properties.updown) {
          case 'up': { // 上游
            // 坐标插入到当前要素坐标列表最前面
            startFeature.geometry.coordinates = filteredFea.geometry.coordinates.concat(startFeature.geometry.coordinates);
            startFeature.properties.source = filteredFea.properties.source;
            startFeature.properties.path = filteredFea.properties.source + '-' + startFeature.properties.path;

            break;
          }
          case 'down': { // 下游
            // 坐标添加到当前要素坐标列表最后面
            startFeature.geometry.coordinates = startFeature.geometry.coordinates.concat(filteredFea.geometry.coordinates);
            startFeature.properties.target = filteredFea.properties.target;
            startFeature.properties.path = startFeature.properties.path + '-' + filteredFea.properties.target;

            break;
          }
          default:
            break;
        }

        filteredFeas = filterFeatures(features, startFeature);
      }

      if (!feature.properties.ignored) {
        // 获取新的起始要素
        startFeature = GeoJsonProvider.getLineStringFeature(feature.geometry.coordinates, {
          source: feature.properties.source,
          target: feature.properties.target,
          path: feature.properties.path,
        });
        feature.properties.ignored = true;
        // 添加到结果要素数组
        resultFeatures.push(startFeature);
      }
    } // end of for (let i = 1; i < features.length; i++)
  }

  /**
   * 过滤管网要素
   */
  function filterFeatures(features, feature) {
    // 上游
    let fiteredFeas = features.filter((fea) => {
      return !fea.properties.ignored &&
             fea.properties.target === feature.properties.source
    });
    if (fiteredFeas.length > 0) {
      if (fiteredFeas.length === 1) {
        fiteredFeas[0].properties.updown = 'up';
      }

      return fiteredFeas;
    }

    // 下游
    fiteredFeas = features.filter((fea) => {
      return !fea.properties.ignored &&
             fea.properties.source === feature.properties.target
    });
    if (fiteredFeas.length === 1) {
      fiteredFeas[0].properties.updown = 'down';
    }

    return fiteredFeas;
  }

  return resultFeatures;
}
