import {
  getGeom
} from '@/base/GeometryUtil'
import {
  GeometryType
} from '@/base/GeometryType'

function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]'
}
/**
 * 单个多边形的自相交判断
 * @param {*} feature
 */
function isSelfIntersects(points) {
  if (!isArray(points)) {
    points = points.split(',')
  }
  var paths = []
  var isInterset = false
  for (var i = 0; i < points.length - 1; i++) {
    var path = getGeom({
      type: GeometryType.POLYLINE,
      points: points[i][0] + ',' + points[i][1] + ',' + points[i + 1][0] + ',' + points[i + 1][1]
    })
    paths.push(path)
  }
  for (var j = 0; j < paths.length; j++) {
    for (var k = 2; k < paths.length - 1; k++) {
      var n = j + k
      if (n >= paths.length) {
        n = n % paths.length
      }
      isInterset = paths[j].intersectsExtent(paths[n].getExtent())
      if (isInterset) {
        return isInterset
      } else {
        continue
      }
    }
    if (isInterset) {
      return isInterset
    } else {
      continue
    }
  }
  return isInterset
}
/**
 * 多面的自相交判断
 * @param {*} feature
 */
export function isSelfInterMultiPolygon(feature) {
  var isInterset = false
  if (feature) {
    var multPolygon = getGeom(feature.geom)
    var polygonlist = multPolygon.getPolygons()
    if (polygonlist.length > 0) {
      for (var i = 0; i < polygonlist.length; i++) {
        var points = polygonlist[i].getCoordinates()
        isInterset = isSelfIntersects(points[0])
        if (isInterset) {
          return isInterset
        } else {
          continue
        }
      }
    }
  }
  return isInterset
}
/**
 * 判断一个多面中的多个面相交判断
 * @param {*} feature
 * @param {*} featurelist
 */
export function isInterMultiPolygon(feature) {
  if (!feature) {
    return
  }
  var multPolygon = getGeom(feature.geom)
  var polygonlist = multPolygon.getPolygons()
  var isInterset = false
    // 判断一个多面中多边形是否相交
  for (var i = 0; i < polygonlist.length; i++) {
    for (var j = i + 1; j < polygonlist.length; j++) {
      isInterset = isInterPolygons(polygonlist[i], polygonlist[j])
      if (isInterset) {
        return isInterset
      } else {
        continue
      }
    }
  }
  // 判断该多面是否与其他多面相交
  return isInterset
}
/**
 * 多面之间的相交判断
 * @param {*} feature
 * @param {*} featurelist
 */
export function isInterBetweenMultiPolygon(feature, featurelist) {
  if (!feature) {
    return
  }
  var multPolygon = getGeom(feature.geom).getPolygons()
  var isInterset = false
  if (featurelist.length > 0) {
    for (var i = 0; i < featurelist.length; i++) {
      var polygons = getGeom(featurelist[i].geom).getPolygons()
      for (var j = 0; j < multPolygon.length; j++) {
        isInterset = isInter(multPolygon[j], polygons)
        if (isInterset) {
          return isInterset
        }
      }
      if (isInterset) {
        return isInterset
      }
    }
  }
  return isInterset
}
// 判断该多变形是否与其他多边形相交
function isInter(feature, areas) {
  var coodsArr = feature.getCoordinates()[0]
  for (var i = 0; i < areas.length; i++) {
    // 如果该多边形的点与其他多边形相交，则返回true。 否则继续
    var isInterset = false
    for (var j = 0; j < coodsArr.length; j++) {
      var point = getGeom({
        type: GeometryType.POINT,
        points: coodsArr[j]
      })
      isInterset = areas[i].intersectsExtent(point.getExtent())
      if (isInterset) { // 相交
        return isInterset
      } else {
        continue
      }
    }
  }
  return isInterset // 该值如果不为null,表示包含或与在边界上 --- 即就是相交
}
// 判断两个多边形之间是否相交
function isInterPolygons(polygon1, polygon2) {
  var isInterset = false
  var coodsArr = polygon2.getCoordinates()[0]
    // 如果此几何体包含指定的坐标，则返回true。 如果坐标位于几何体的边界上，则返回false。
  for (var i = 0; i < coodsArr.length; i++) {
    var point = getGeom({
      type: GeometryType.POINT,
      points: coodsArr[i]
    })
    isInterset = polygon1.intersectsExtent(point.getExtent())
    if (isInterset) {
      return isInterset
    } else {
      continue
    }
  }
  return isInterset // 该值如果不为null,表示包含或与在边界上 --- 即就是相交
}

function isContainer(areas, feature) {
  var coodsArr = feature.getCoordinates()[0]
  for (var i = 0; i < areas.length; i++) {
    // 如果此几何体包含指定的坐标，则返回true。 如果坐标位于几何体的边界上，则返回false。
    var isCon = false
    var num = 0
    for (var j = 0; j < coodsArr.length; j++) {
      isCon = areas[i].intersectsCoordinate(coodsArr[j])
      if (isCon) { // 不包含
        num = num + 1
        continue
      } else {
        break
      }
    }
    if (num === coodsArr.length) {
      return true
    } else {
      continue
    }
  }
  return isCon // 该值如果不为null,表示包含或与在边界上 --- 即就是相交
}
/**
 * 判断多面之间是否包含
 * @param {area} 已添加的多面
 * @param {feature} 新添加的多变
 */
export function isContainerInPolygons(area, feature) {
  if (!area || !feature) {
    return
  }
  var areas = getGeom(area.geom).getPolygons()
  var features = getGeom(feature.geom).getPolygons()
  var isContain = false
  if (features.length > 0) {
    var num = 0
    for (var i = 0; i < features.length; i++) {
      isContain = isContainer(areas, features[i])
      if (!isContain) {
        return isContain
      } else {
        num = num + 1
        continue
      }
    }
    if (num === features.length) {
      return true
    }
  }
  return isContain
}
