import { Circle as CircleStyle, Fill, Stroke, Style, Text } from 'ol/style'
import { createEmpty, getWidth, getHeight, extend } from 'ol/extent'
export const isPoint = obj => obj.lng && obj.lat
export const checkType = val => Object.prototype.toString.call(val).slice(8, -1)
export const toKebabCase = str => str.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`).replace(/^-/, '')
export function generateID(len, seed = '') {
    var idLen = len ? (len < 12 ? 12 : len) : 12
    var idSeed = seed
    var randomSeed = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
    var randomStr = ''
    while (randomStr.length < idLen) {
        randomStr += randomSeed.substr(Math.ceil(Math.random() * 62), 1)
    }
    return idSeed + randomStr
}
// 查询最大索引的要素------
export function getFeatureOfMaxZIndex(featureList) {
    let feature = null
    if (featureList.length > 0) {
        var maxIndex = parseInt(featureList[0].index)
        var index = 0
        featureList.forEach((element, i) => {
            if (parseInt(element.index) > maxIndex) {
                maxIndex = parseInt(element.index)
                index = i
            }
        })
        feature = featureList[index].feature
    }
    return feature
}
var overLays = {}
export function addOverLay(id, overlay) {
    overLays[id] = overlay
}
export function getOverLays() {
    return overLays
}
export function gerOverLay(id) {
    return overLays[id]
}
export function removeOverLay(id) {
    overLays[id] = []
}
export function styleFunction(feature, resolution) {
    var textFill = new Fill({
        color: '#fff'
    })
    var textStroke = new Stroke({
        color: 'rgba(0, 0, 0, 0.6)',
        width: 3
    })
    var style = null
    var size = feature.get('features').length
    if (size > 1) {
        style = new Style({
            image: new CircleStyle({
                radius: 15,
                fill: new Fill({
                    color: [255, 153, 0, Math.min(0.8, 0.4 + (size / 15))]
                })
            }),
            text: new Text({
                text: size.toString(),
                fill: textFill,
                stroke: textStroke
            })
        })
    } else {
        var originalFeature = feature.get('features')[0]
        style = createFeatureStyle(originalFeature)
    }
    return style
}
export function createFeatureStyle(feature) {
    return new Style({
        geometry: feature.getGeometry(),
        image: new CircleStyle({
            radius: 10,
            fill: new Fill({
                color: 'rgba(255, 153, 0, 0.8)'
            }),
            stroke: new Stroke({
                color: 'rgba(255, 204, 0, 0.2)',
                width: 1
            })
        })
    })
}