/**
 * 胡红勋 2019地图符号工具转化类
 */
import SymbolConfig from './SymbolConfig.js'
import Style from 'ol/style/Style'
import Fill from 'ol/style/Fill'
import Icon from 'ol/style/Icon'
import Stroke from 'ol/style/Stroke'
import Text from 'ol/style/Text'
import CircleStyle from 'ol/style/Circle'
export function convertObjToStyle(symbolObj) {
    let style = new Style({})
    var config = symbolObj
    var { fill, image, stroke, label, circle } = config
    if (fill) {
        let color = fill.color
        style.setFill(new Fill({ color }))
    }
    if (stroke) {
        let lineSymol = new Stroke({})
        let { color, lineCap, lineJoin, lineDash, lineDashOffset, miterLimit, width } = stroke
        if (color) {
            lineSymol.setColor(color)
        } else {
            lineSymol.setColor('#fff')
        }
        if (lineCap) {
            lineSymol.setLineCap(lineCap)
        }
        if (lineJoin) {
            lineSymol.setLineJoin(lineJoin)
        }
        if (lineDash) {
            lineSymol.setLineCap(lineDash)
        }
        if (lineDashOffset) {
            lineSymol.setLineDashOffset(lineDashOffset)
        }
        if (miterLimit) {
            lineSymol.setMiterLimit(miterLimit)
        }
        if (width) {
            lineSymol.setWidth(width)
        } else {
            lineSymol.setWidth(2)
        }
        style.setStroke(lineSymol)
    }
    if (image) {
        let { rotateWithView, rotation, size, src, opacity, offset, offsetOrigin, scale } = image
        src = src || 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3FpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmP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nfmHYd+k3XJEgDyPDzCweBnsdVobXH/8zbDr1C9MFn7//Z3Cv/8Rw4d4fhhWlvAyF/pwMjEDLdOWZGfa28DMYKbMwhHV9RtEMAhiBCHJqfSQXQ7YXB8PR67/BGh+9/scQ3fuZ4f7LfxhewxkL4bbsDP3J3AwHrvwGewnkQmwAbzQqSzIz3H/xl+Hff/yx4Igrnd99jlczSI8jE1IKxGkQNo2wFIkc6YQMQtGILyWiG4RVIwwABBgAs89jIMcqV/IAAAAASUVORK5CYII='
        let imageStyle = null
        offset = offset || [0, 0]
        offsetOrigin = offsetOrigin || 'top-left'
        imageStyle = new Icon({ src, size, offset, offsetOrigin })
        if (rotateWithView) {
            imageStyle.setRotateWithView(rotateWithView)
        }
        if (rotation) {
            imageStyle.setRotation(rotation)
        }
        if (opacity) {
            imageStyle.setOpacity(opacity)
        }
        if (scale) {
            imageStyle.setScale(scale)
        }
        style.setImage(imageStyle)
    }
    if (label) {
        let {
            font,
            offsetX,
            offsetY,
            scale,
            rotateWithView,
            rotation,
            text,
            textAlign,
            fillColor,
            stroke,
            backgroundFillColor,
            backgroundStroke,
            padding
        } = label
        let textSymbol = null
        rotateWithView = rotateWithView || false
        text = text || ''
        textSymbol = new Text({ text, rotateWithView })
        if (font) {
            textSymbol.setFont(font)
        }
        if (offsetX) {
            textSymbol.setOffsetX(offsetX)
        }
        if (offsetY) {
            textSymbol.setOffsetY(offsetY)
        }
        if (scale) {
            textSymbol.setScale(scale)
        }
        if (rotation) {
            textSymbol.setRotation(rotation)
        }
        if (textAlign) {
            textSymbol.setTextAlign(textAlign)
        }
        if (fillColor) {
            textSymbol.setFill(new Fill({ color: fillColor }))
        }
        if (stroke) {
            textSymbol.setStroke(new Stroke(stroke))
        }
        if (backgroundFillColor) {
            textSymbol.setBackgroundFill(new Fill({ color: backgroundFillColor }))
        }
        if (backgroundStroke) {
            textSymbol.setBackgroundStroke(new Stroke(backgroundStroke))
        }
        if (padding) {
            textSymbol.setPadding(padding)
        }
        style.setText(textSymbol)
    }
    if (circle) {
        let { fillColor, radius, strokeWidth, strokeColor } = circle
        fillColor = fillColor || '#fff'
        strokeWidth = strokeWidth || 1
        strokeColor = strokeColor || '#fff'
        radius = radius || 5
        let circleStyle = new CircleStyle({
            radius,
            fill: new Fill({ color: fillColor }),
            stroke: new Stroke({ color: strokeColor, width: strokeWidth })
        })
        style.setImage(circleStyle)
    }
    return style
}