import * as layer from 'ol/layer'
import * as source from 'ol/source'
import HaituImg from './images/bg-ocean.png'
import LutuImg from './images/bg-land.png'
import WeixingImg from './images/bg-sky.png'
import store from '@/store/index'

const isInternet = false

const baseMapView = {
  center: [109.6915958479584, 19.111636735969318],
  projection: "EPSG:4326",
  zoom: 9
  // extent: [120.8953306326286,31.3667480047968,121.37735577911297,31.692561298253832]
}
const initBaseLayer = (obj: any) => {
  const layers: any = []
  obj.layersUrl.forEach((v: any) => {

    const s = new source.XYZ({
      projection: "EPSG:4326",
      url: v.url,
    })
    const l = new layer.Tile({
      source: s
    })
    if (v.extent?.length > 0) {
      l.setExtent(v.extent)
    }
    if (v.maxZoom) {
      l.setMaxZoom(v.maxZoom)
    }
    if (v.minZoom) {
      l.setMinZoom(v.minZoom)
    }
    layers.push(l)
  })
  const layerGroup = new layer.Group({
    layers: layers,
    visible: obj.visible,
  })
  layerGroup.set('_maxZoom', obj.maxZoom)
  layerGroup.set('_minZoom', obj.minZoom)
  layerGroup.set('_easyMapOl_layerName', obj.name)
  layerGroup.set('_label', obj.label)
  layerGroup.set('_img', obj.img)
  return layerGroup
}
const initInternet = (obj) => {
  const _layer = new layer.Tile({
    source: new source.XYZ({
      url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
    }),
    visible: obj.visible,
  })
  _layer.set('_maxZoom', obj.maxZoom)
  _layer.set('_minZoom', obj.minZoom)
  _layer.set('_easyMapOl_layerName', obj.name)
  _layer.set('_label', obj.label)
  _layer.set('_img', obj.img)
  return _layer
}

const baseMapLayers = [
  {
    key: 'key1',
    name: 'base_key1',
    label: 'key1',
    maxZoom: 18,
    minZoom: 1,
    visible: true,
    img: HaituImg,
    layersUrl: [
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/vec_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=vec&style=default&opacity=1&zIndex=1&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap2Api}/mapserver/vmap/hn_bigdata_2018dt/getMAP?x={x}&y={y}&l={z}&styleId=hn_bigdata_2018dtys1&ratio=1&tilesize=256&clientVersion=jssdk_bate@%20leaflet%202.1.5`,
        extent: [108.50508936658522, 18.127326424708514, 111.09756659719326, 20.163718157197533]
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/cva_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=cva&style=default&zIndex=1&tilematrix={z}&tilerow={y}&tilecol={x}`
      },
    ]
  },
  {
    key: 'key2',
    name: 'base_key2',
    label: 'key2',
    maxZoom: 18,
    minZoom: 1,
    visible: false,
    img: HaituImg,
    layersUrl: [
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/img_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=img&style=default&opacity=0.6&zIndex=1&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap3Api}/ime-cloud/rest/hainan_img_2018/wmts?service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=default028mm&format=image%2Fjpgpng&height=256&width=256&layer=hainan_img_2018&style=default&minZoom=8&maxZoom=18&zIndex=4&bounds=13.257020557028%2C103.34794430865948%2C23.46552385201528%2C114.30558768331025&tilematrix={z}&tilerow={y}&tilecol={x}`,
        extent: [103.34794430865948, 13.257020557028, 114.30558768331025, 23.46552385201528]
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/cia_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=cia&style=default&zIndex=4&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
    ]
  },
  {
    key: 'key3',
    name: 'base_key3',
    label: 'key3',
    maxZoom: 18,
    minZoom: 1,
    visible: false,
    img: HaituImg,
    layersUrl: [
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/ter_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=ter&style=default&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap3Api}/ime-cloud/rest/hainan_dem/wmts?service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=default028mm&format=image%2Fjpgpng&height=256&width=256&layer=hainan_dem&style=default&minZoom=8&maxZoom=18&zIndex=4&bounds=17.17616389817063%2C106.9017014531631%2C21.04474386095089%2C113.04819115525501&tilematrix={z}&tilerow={y}&tilecol={x}`,
        maxZoom: 18,
        minZoom: 8,
        extent: [106.9017014531631, 17.17616389817063, 113.04819115525501, 21.04474386095089]
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/cta_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=cta&style=default&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
    ]
  },
  {
    key: 'key4',
    name: 'base_key4',
    label: 'key4',
    maxZoom: 18,
    minZoom: 1,
    visible: false,
    img: HaituImg,
    layersUrl: [
      {
        url: `/${store.state.app.apiProxy.BaseMap2Api}/mapserver/vmap/hn_bigdata_2018dt/getMAP?x={x}&y={y}&l={z}&styleId=hn_bigdata_2018dtys2&ratio=1&tilesize=256&clientVersion=jssdk_bate@%20leaflet%202.1.5`,
      },
      {
        url: `/${store.state.app.apiProxy.BaseMap1Api}/cta_c/wmts?tk=%E6%82%A8%E7%9A%84%E5%AF%86%E9%92%A5&service=WMTS&request=GetTile&version=1.0.0&layers=&styles=&tilematrixSet=c&format=tiles&height=256&width=256&layer=cta&style=default&tilematrix={z}&tilerow={y}&tilecol={x}`,
      },
    ]
  },
]

export default {
  baseMapView,
  baseMapLayers,
  initBaseLayer,
  initInternet,
  isInternet
}
