/**
 * 胡红勋 2017-12-21 几何转换工具类
 */
import {
  GeometryType
} from './GeometryType.js'
import {
  checkType
} from './util.js'
import ol from 'openlayers'
export function getGeom(geometry) {
  if (!geometry) {
    return null
  }
  var geom = null
  switch (geometry.type) {
    case GeometryType.POINT:
      {
        geom = _convertToPoint(geometry.points)
        break
      }
    case GeometryType.POLYLINE:
      {
        geom = _convertToPolyline(geometry.points)
        break
      }
    case GeometryType.POLYGON:
      {
        geom = _convertToPolygon(geometry.points)
        break
      }
    case GeometryType.MULTIPOINT:
      {
        geom = _convertToMultiPoint(geometry.points)
        break
      }
    case GeometryType.MULTIPOLYLINE:
      {
        geom = _convertToMultiPolyline(geometry.points)
        break
      }
    case GeometryType.MULTIPOLYGON:
      {
        geom = _convertToMultiPolygon(geometry.points)
        break
      }
    default:
      geom = null
  }
  return geom
}

function _convertPointStrToArray (points) {
  var pointArr = points.split(',')
  var arrLen = pointArr.length
  var tmpArr = []
  for (var i = 0; i < arrLen; i += 2) {
    tmpArr.push([parseFloat(pointArr[i]), parseFloat(pointArr[i + 1])])
  }
  return tmpArr
}

function _convertToPoint(points) {
  var type = checkType(points)
  var param = null
  if (type === 'String') {
    var pointArr = points.split(',')
    param = [parseFloat(pointArr[0]), parseFloat(pointArr[1])]
  } else {
    param = points
  }
  return new ol.geom.Point(param)
}

function _convertToPolyline(points) {
  var type = checkType(points)
  var param = null
  if (type === 'String') {
    param = _convertPointStrToArray(points)
  } else {
    param = points
  }
  return new ol.geom.LineString(param)
}

function _convertToPolygon(points) {
  var type = checkType(points)
  var param = null
  if (type === 'String') {
    param = _convertPointStrToArray(points)
  } else {
    param = points
  }
  return new ol.geom.Polygon([param])
}

function _convertToMultiPoint(points) {
  var pointArr = points.split('|')
  var mutiPoint = new ol.geom.MultiPoint()
  for (let points of pointArr.values()) {
    var _point = _convertToPoint(points)
    mutiPoint.appendPoint(_point)
  }
  return mutiPoint
}

function _convertToMultiPolyline(points) {
  var multiLine = new ol.geom.MultiLineString()
  var pointArr = points.split('|')
  for (let points of pointArr.values()) {
    var _line = _convertToPolyline(points)
    multiLine.appendLineString(_line)
  }
  return multiLine
}

function _convertToMultiPolygon(points) {
  var mutiPolygon = new ol.geom.MultiPolygon()
  var pointArr = points.split('|')
  for (let points of pointArr.values()) {
    var _polygon = _convertToPolygon(points)
    mutiPolygon.appendPolygon(_polygon)
  }
  return mutiPolygon
}

export function convertToFeature(feature) {
  let {
    geom
  } = feature
  var geometry = getGeom(geom)
  var vector = new ol.Feature(geometry)
  return vector
}
