/**
 * 胡红勋 2019-3-12几何转换工具类
 */
import {
    GeometryType
} from './GeometryType.js'
import {
    checkType
} from './util.js'
import Point from 'ol/geom/Point'
import MultiPoint from 'ol/geom/MultiPoint'
import Polygon from 'ol/geom/Polygon'
import LineString from 'ol/geom/LineString'
import MultiLineString from 'ol/geom/MultiLineString'
import MultiPolygon from 'ol/geom/MultiPolygon'
import Feature from 'ol/Feature'
import { convertObjToStyle } from './SymbolUtil'
// 将对象转化为几何体对象---------
export function covertObjToGeometry(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])]
    }
    return new Point(param)
}
// 将线坐标串转化为线要素对象----
function _convertToPolyline(points) {
    var type = checkType(points)
    var param = null
    if (type === 'String') {
        param = _convertPointStrToArray(points)
    }
    return new LineString(param)
}
// 将线坐标串转化为面要素对象----
function _convertToPolygon(points) {
    var type = checkType(points)
    var param = null
    if (type === 'String') {
        param = _convertPointStrToArray(points)
    }
    return new Polygon([param])
}
// 将线坐标串转化为多点要素对象----
function _convertToMultiPoint(points) {
    var pointArr = points.split('|')
    var mutiPoint = new MultiPoint()
    for (let points of pointArr.values()) {
        var _point = _convertToPoint(points)
        mutiPoint.appendPoint(_point)
    }
    return mutiPoint
}
// 将线坐标串转化为多线要素对象----
function _convertToMultiPolyline(points) {
    var multiLine = new 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 MultiPolygon()
    var pointArr = points.split('|')
    for (let points of pointArr.values()) {
        var _polygon = _convertToPolygon(points)
        mutiPolygon.appendPolygon(_polygon)
    }
    return mutiPolygon
}
// 将对象转化为要素对象--------
export function convertObjToFeature(feature) {
    let {
        geom,
        attributes,
        symbol
    } = feature
    var geometry = covertObjToGeometry(geom)
    var style = null
    if (symbol) {
        style = convertObjToStyle(symbol)
    }
    var vector = new Feature(geometry)
    vector.set('attributes', attributes)
    vector.setId(attributes.id)
    vector.set('id', attributes.id)
    vector.set('type', attributes.type)
    if (style) {
        vector.setStyle(style)
    }
    return vector
}