import {
  createPoint
} from './factory'
import { TileSuperMapRest } from '@/supermap/openlayers'
var overLays = {}
export const isPoint = obj => obj.lng && obj.lat
export const checkType = val => Object.prototype.toString.call(val).slice(8, -1)
export const getPosition = (ol, point) => isPoint(point) ? createPoint(ol, point) : point

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 addOverLay(id, overlay) {
  overLays[id] = overlay
}
export function gerOverLay(id) {
  return overLays[id]
}
export function removeOverLay(id) {
  overLays[id] = []
}
// 十六进制颜色值转换为rgb形式
export function changeColorFormat(color) {
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  var sColor = color.toLowerCase()
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = '#'
      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
      }
      sColor = sColorNew
    }
    // 处理六位的颜色值
    var sColorChange = []
    for (var j = 1; j < 7; j += 2) {
      sColorChange.push(parseInt('0x' + sColor.slice(j, j + 2)))
    }
    return 'rgb(' + sColorChange.join(',') + ')'
  } else {
    return sColor
  }
}
export function getFeatureByMaxZIndex(featureList) {
  if (featureList.length > 0) {
    var max = parseInt(featureList[0][1])
    var index = 0
    featureList.forEach((element, i) => {
      if (parseInt(element[1]) > max) {
        max = parseInt(element[1])
        index = i
      }
    })
    return featureList[index][0]
  } else {
    return null
  }
}
export function getMapResolutions(url, originResult) {
  const tileGrid = TileSuperMapRest.optionsFromMapJSON(url, originResult).tileGrid
  return tileGrid.getResolutions()
}
