/**
 * 管线分析基本工具集合
 * @author wanghai
 */
import {
  PrjCesiumToSuperMap
} from './Convert.js';
export default {
  /**
   * 判断两管线是否相交
   * @param sourcegeometry 判断管线
   * @param comparegeometry 比较管线
   * @returns {crossPoint:{x:x,y:y},iscross:Boolean(true相交，false不相交)}
   */
  IsOrNotIntersect: function (sourcegeometry, comparegeometry) {
    let crossPt = {
      x: 0,
      y: 0
    };
    let wgssourcePoint1 = sourcegeometry.components[0];
    let wgssourcePoint2 = sourcegeometry.components[1];
    let wgscomparePoint1 = comparegeometry.components[0];
    let wgscomparePoint2 = comparegeometry.components[1];
    let isCover = this.IsPointsCover([wgssourcePoint1, wgssourcePoint2, wgscomparePoint1,
      wgscomparePoint2
    ]);
    if (isCover)
      return {
        crossPoint: crossPt,
        iscross: true
      };
    let sourcePoint1 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgssourcePoint1.x,
      wgssourcePoint1.y
    ]);
    let sourcePoint2 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgssourcePoint2.x,
      wgssourcePoint2.y
    ]);
    let comparePoint1 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgscomparePoint1.x,
      wgscomparePoint1.y
    ]);
    let comparePoint2 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgscomparePoint2.x,
      wgscomparePoint2.y
    ]);
    let a1 = sourcePoint2.y - sourcePoint1.y;
    let b1 = sourcePoint1.x - sourcePoint2.x;
    let c1 = sourcePoint1.x * sourcePoint2.y - sourcePoint2.x * sourcePoint1.y;
    let a2 = comparePoint2.y - comparePoint1.y;
    let b2 = comparePoint1.x - comparePoint2.x;
    let c2 = comparePoint1.x * comparePoint2.y - comparePoint2.x * comparePoint1.y;
    let det = a1 * b2 - a2 * b1;
    if (det == 0)
      return {
        crossPoint: crossPt,
        iscross: false
      };
    let x = (c1 * b2 - c2 * b1) / det;
    let y = (a1 * c2 - a2 * c1) / det;
    if ((Math.abs(x - (sourcePoint1.x + sourcePoint2.x) / 2) <= Math.abs(sourcePoint2.x -
        sourcePoint1.x) / 2) &&
      (Math.abs(y - (sourcePoint1.y + sourcePoint2.y) / 2) <= Math.abs(sourcePoint2.y -
        sourcePoint1.y) / 2) &&
      (Math.abs(x - (comparePoint1.x + comparePoint2.x) / 2) <= Math.abs(comparePoint2.x -
        comparePoint1.x) / 2) &&
      (Math.abs(y - (comparePoint1.y + comparePoint2.y) / 2) <= Math.abs(comparePoint2.y -
        comparePoint1.y) / 2)) {
      crossPt.x = x;
      crossPt.y = y;
      return {
        crossPoint: crossPt,
        iscross: true
      };
    }
    return {
      crossPoint: crossPt,
      iscross: false
    };
  },
  /**
   * 判断管线两点是否有重合点
   * @param points
   * @returns Boolean(true重合，false不重合)
   */
  IsPointsCover: function (points) {
    for (let i = 0; i < points.length - 1; i++) {
      {
        let point1 = points[i];
        for (let j = i + 1; j < points.length; j++) {
          let point2 = points[j];
          if (point1.x == point2.x && point1.y == point2.y)
            return true;
        }
      }
    }
    return false;
  },
  /**
   * 计算最短水平距离(使用外壁最小距离)
   * @param sourcegeometry 管线图形
   * @param comparegeometry 比较管线图形
   * @param sdiameter 管径，单位：mm
   * @param cdiameter 比较管线管径，单位：mm
   */
  GetShortestDistance: function (sourcegeometry, comparegeometry, sdiameter, cdiameter) {
    let sourcePoint1 = sourcegeometry.components[0];
    let sourcePoint2 = sourcegeometry.components[1];
    let comparePoint1 = comparegeometry.components[0];
    let comparePoint2 = comparegeometry.components[1];
    let sd = 0,
      cd = 0;
    if (sdiameter.indexOf("X") > 0) {
      sd = sdiameter.split("X")[0];
    } else if (sdiameter.indexOf("*") > 0) {
      sd = sdiameter.split("*")[0];
    } else
      sd = sdiameter;

    if (cdiameter.indexOf("X") > 0) {
      cd = cdiameter.split("X")[0];
    } else if (cdiameter.indexOf("*") > 0) {
      cd = cdiameter.split("*")[0];
    } else
      cd = cdiameter;
    let s_cdiameter = sd / 2000.0 + cd / 2000.0;
    let distances = [];
    comparePoint1 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [comparePoint1.x,
      comparePoint1.y
    ]);
    comparePoint2 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [comparePoint2.x,
      comparePoint2.y
    ]);
    sourcePoint1 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [sourcePoint1.x, sourcePoint1
      .y
    ]);
    sourcePoint2 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [sourcePoint2.x, sourcePoint2
      .y
    ]);
    let distance1 = this.GetPointToLineDistance(comparePoint1, sourcePoint1, sourcePoint2);
    distances.push((distance1 - s_cdiameter).toFixed(4));
    let distance2 = this.GetPointToLineDistance(comparePoint2, sourcePoint1, sourcePoint2);
    distances.push((distance2 - s_cdiameter).toFixed(4));
    let distance3 = this.GetPointToLineDistance(sourcePoint1, comparePoint1, comparePoint2);
    distances.push((distance1 - s_cdiameter).toFixed(4));
    let distance4 = this.GetPointToLineDistance(sourcePoint2, comparePoint1, comparePoint2);
    distances.push((distance1 - s_cdiameter).toFixed(4));
    return Math.min.apply(Math, distances);
  },
  /**
   * 以参考对象作为线，比较对象端点作为求取点
   */
  GetPointToLineDistance: function (Point, LinePointa, LinePointb) {
    let cross = (LinePointb.x - LinePointa.x) * (Point.x - LinePointa.x) + (LinePointb.y -
      LinePointa.y) * (Point.y - LinePointa.y);
    if (cross <= 0)
      return Math.sqrt((Point.x - LinePointa.x) * (Point.x - LinePointa.x) + (Point.y - LinePointa
        .y) * (Point.y - LinePointa.y));

    let d2 = (LinePointb.x - LinePointa.x) * (LinePointb.x - LinePointa.x) + (LinePointb.y -
      LinePointa.y) * (LinePointb.y - LinePointa.y);
    if (cross >= d2)
      return Math.sqrt((Point.x - LinePointb.x) * (Point.x - LinePointb.x) + (Point.y - LinePointb
        .y) * (Point.y - LinePointb.y));

    let r = cross / d2;
    let px = LinePointa.x + (LinePointb.x - LinePointa.x) * r;
    let py = LinePointa.y + (LinePointb.y - LinePointa.y) * r;
    return Math.sqrt((Point.x - px) * (Point.x - px) + (py - Point.y) * (py - Point.y));
  },
  /**
   * 获交点深
   */
  GetCrossPointDepth: function (point, geometry, startdepth, enddepth) {
    let wgsgeometryPoint1 = geometry.components[0];
    let wgsgeometryPoint2 = geometry.components[1];
    let geometryPoint1 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgsgeometryPoint1.x,
      wgsgeometryPoint1.y
    ]);
    let geometryPoint2 = PrjCesiumToSuperMap.wgsToGcsXY(Cesium, SuperMap, [wgsgeometryPoint2.x,
      wgsgeometryPoint2.y
    ]);
    let lineLength = Math.sqrt(Math.pow((geometryPoint1.x - geometryPoint2.x), 2) + Math.pow((
      geometryPoint1.y - geometryPoint2.y), 2));
    let pointLength = Math.sqrt(Math.pow((geometryPoint1.x - point.x), 2) + Math.pow((
      geometryPoint1.y - point.y), 2));
    let elevation;
    if (lineLength != 0) {
      elevation = (pointLength / lineLength) * (startdepth - enddepth);
    }
    return Math.min.apply(Math, [startdepth, enddepth]) + elevation;
  },
  /**
   * 计算最短垂直距离(使用外壁最小距离)
   * @param sourceElevation 管线高度
   * @param compareElevation 比较管线高度
   * @param sdiameter 管径，单位：mm
   * @param cdiameter 比较管线管径，单位：mm
   */
  GetShortestVDistance: function (sourceElevation, compareElevation, sdiameter, cdiameter) {
    let D_value = Math.abs(sourceElevation - compareElevation);
    let sd = 0,
      cd = 0;
    if (sdiameter.indexOf("X") > 0) {
      sd = sdiameter.split("X")[0];
    } else if (sdiameter.indexOf("*") > 0) {
      sd = sdiameter.split("*")[0];
    } else
      sd = sdiameter;

    if (cdiameter.indexOf("X") > 0) {
      cd = cdiameter.split("X")[0];
    } else if (cdiameter.indexOf("*") > 0) {
      cd = cdiameter.split("*")[0];
    } else
      cd = cdiameter;
    let s_cdiameter = sd / 2000.0 + cd / 2000.0;
    return D_value - s_cdiameter;
  },

  /**
   * 处理半径小数点
   * @param diameter
   */
  FixedDiameter: function (diameter) {
    let dinfo = diameter + "";
    if (dinfo.indexOf(".") > -1 && dinfo.indexOf("*") <= -1 && dinfo.indexOf("X") <= -1) {
      return dinfo.split(".")[0];
    } else {
      return diameter
    }
  },

  /**
   * 处理数据小数点
   * @param diameter 数字
   * @param num 保留小数位数
   */
  FixedDiameterNumber: function (diameter, num) {
    let dinfo = diameter + "";
    if (dinfo.indexOf(".") > -1) {
      return parseFloat(dinfo).toFixed(num);
    } else {
      return diameter
    }
  },

  /**
   * 取出两个数组中相同的元素
   */
  GetArrEqual(arr1, arr2) {
    let newArr = [];
    if (arr1 && arr2) {
      for (let i = 0; i < arr2.length; i++) {
        for (let j = 0; j < arr1.length; j++) {
          if (arr1[j] === arr2[i]) {
            newArr.push(arr1[j]);
          }
        }
      }
    }
    return newArr;
  },

  /**
   * where条件中in的字符串组合
   * @param ids
   */
  GetWhereStr(ids) {
    let str = "(";
    for (let i = 0; i < ids.length; i++) {
      if (i != ids.length - 1) {
        str += ids[i] + ",";
      } else {
        str += ids[i] + ")";
      }
    }
    return str;
  },

  /**
   * 从第一个数组中移除第二个数组相同的
   */
  RemoveArrEqual(arr1, arr2) {
    let newArr = [];
    if (!arr2 || arr2.length == 0) {
      return arr1;
    }
    if (arr2 && arr1) {
      newArr = arr1;
      for (let i = 0; i < arr2.length; i++) {
        let index = newArr.indexOf(arr2[i]);
        if (index > -1) {
          newArr.splice(index, 1);
        }
      }
    }

    return newArr;
  },

  /**
   * 复制数组
   */
  CopyArr(arr2) {
    let newArr = [];
    if (arr2) {
      for (let i = 0; i < arr2.length; i++) {
        newArr.push(arr2[i]);
      }
    }
    return newArr;
  },

  /**
   * 开启或关闭右键调试菜单
   * @param open true打开，false关闭
   */
  HideRight(open) {
    if (open) {
      let oncontextmenu = "return true";
      document.oncontextmenu = function (e) {
        return true;
      }
    } else {
      let oncontextmenu = "return false";
      document.oncontextmenu = function (e) {
        return false;
      }
    }
  }
}
