import {Geometry, GeometryType} from '@mapgis/webclient-common'

const {
  Cartesian3
} = Cesium

/**
 * 获取点类型的坐标点
 * @private
 * @return {Array<Cartesian3>} Cesium的点坐标
 * */
function getPointPositions(feature) {
  // 获取要素几何
  const _geometry = feature.geometry
  // 根据几何类型获取点坐标
  const _positions = []
  switch (_geometry.type) {
    case GeometryType.point:
      _positions.push(
        Cartesian3.fromDegrees(
          _geometry.coordinates[0],
          _geometry.coordinates[1],
          _geometry.coordinates[2]
        )
      )
      break
    case GeometryType.multiPoint:
      for (let i = 0; i < _geometry.coordinates.length; i++) {
        _positions.push(
          Cartesian3.fromDegrees(
            _geometry.coordinates[i][0],
            _geometry.coordinates[i][1],
            _geometry.coordinates[i][2]
          )
        )
      }
      break
    case GeometryType.polygon:
      const _centroid = Geometry.fromJSON(_geometry).centroid
      _positions.push(
        Cartesian3.fromDegrees(
          _centroid.coordinates[0],
          _centroid.coordinates[1],
          _centroid.coordinates[2]
        )
      )
      break
    case GeometryType.multiPolygon:
      const _polygons = Geometry.fromJSON(_geometry).toPolygonArray()
      for (let i = 0; i < _polygons.length; i++) {
        const _centroid = _polygons[i].centroid
        _positions.push(
          Cartesian3.fromDegrees(
            _centroid.coordinates[0],
            _centroid.coordinates[1],
            _centroid.coordinates[2]
          )
        )
      }
      break
    case GeometryType.extent:
      _positions.push(
        Cartesian3.fromDegrees(
          (_geometry.xmin + _geometry.xmax) / 2,
          (_geometry.ymin + _geometry.ymax) / 2,
          (_geometry.zmin + _geometry.zmax) / 2
        )
      )
      break
    case GeometryType.circle:
      _positions.push(
        Cartesian3.fromDegrees(
          _geometry.center[0],
          _geometry.center[1],
          _geometry.center[2]
        )
      )
      break
    default:
      break
  }
  return _positions
}

export {
  getPointPositions
}
