import { mapType } from '../js/MapLocalConfig'
import * as esriLoader from 'esri-loader'
/**
 * @class MapLoader
 * @classdesc 地图API加载器,基于高德JSAPI 2.0，百度JavaScript API GL,arcgis api for javascript 3.23,diva sdk 1.2.0,superAPI6.6.0
 */
export class MapLoader {
  // url //api url
  // scriptElement //插入api script的节点
  // cssElement //插入css script的节点
  /**
   * 加载地图API
   * @param {json} mapconfig
   * @returns 成功、失败回调
   */
  static load(mapconfig) {
    window.mapconfig = mapconfig
    var promis
    switch (window.mapconfig.mapType) {
      case mapType.Gaode:
        this.url =
          'http://webapi.amap.com/maps?v=2.0' +
          '&key=' +
          window.mapconfig.key +
          '&plugin=AMap.MouseTool,AMap.PolyEditor'
        window._AMapSecurityConfig = {
          securityJsCode: window.mapconfig.securityCode
        }
        promis = this.scriptLoader()
        break
      case mapType.BD:
        this.url =
          'https://api.map.baidu.com/api?v=1.0' +
          '&type=webgl&ak=' +
          window.mapconfig.key +
          '&s=1&callback=onBMapCallback'
        promis = this.baiduAPILoadr()
        break
      case mapType.Esri:
        this.url = window.mapconfig.apiUrl
        var css = window.mapconfig.mapstyle
        promis = this.esriAPILoader(css)
        break
      case mapType.Diva:
        // this.url = 'https://diva.sheencity.cn/docs/_lib/diva-sdk-1.2.0.min.js'
        // promis = this.scriptLoader()
        promis = new Promise((resolve) => {
          resolve()
        })
        break
      case mapType.SuperAPI:
        this.url = 'http://superapi.51aes.com/dist/superAPI-6.6.0.min.js'
        promis = this.scriptLoader()
        break
      case mapType.TacosSDK:
        promis = new Promise((resolve) => {
          resolve()
        })
        break
    }
    return promis
  }
  static baiduAPILoadr() {
    var that = this
    var libs = [
      'https://mapopen.bj.bcebos.com/github/BMapGLLib/InfoBox/src/InfoBox.min.js', //添加百度BMapGLLib信息窗js
      'https://mapopen.bj.bcebos.com/github/BMapGLLib/DrawingManager/src/DrawingManager.min.js', //添加要素编辑器js
      // 'https://mapopen.bj.bcebos.com/github/BMapGLLib/Lushu/src/Lushu.min.js',    //轨迹动画js
      'https://api.map.baidu.com/library/LuShu/gl/src/LuShu_min.js',
      'http://mapopen.bj.bcebos.com/github/BMapGLLib/RichMarker/src/RichMarker.min.js' //富文本标记
    ]
    this.libslen = libs.length
    this.count = 0
    return new Promise((resolve, reject) => {
      // 如果已加载直接返回
      if (typeof window.BMapGL !== 'undefined') {
        resolve(window.BMapGL)
        return true
      }
      // 插入script脚本
      this.addScript()
      this.blibs = []
      // 异步加载回调处理
      window.onBMapCallback = function () {
        libs.forEach((lib) => {
          that.loadLib(lib, resolve, reject)
        })
        console.log('API初始化成功...')
      }
    })
  }
  static loadLib(src, resolve, reject) {
    var sLib = document.createElement('script')
    sLib.setAttribute('type', 'text/javascript')
    sLib.setAttribute('async', 'sync')
    sLib.src = src
    document.body.appendChild(sLib)

    this.blibs.push(sLib)

    sLib.onerror = function () {
      reject('API初始化失败...')
    }
    var that = this
    sLib.onload = function (e) {
      that.count += 1
      if (that.count === that.libslen) {
        // libs全部加载成功才返回
        resolve(window.BMapGL, window.BMapGLLib)
      }
    }
  }
  static scriptLoader() {
    this.addScript()

    return new Promise((resolve, reject) => {
      this.scriptElement.addEventListener(
        'load',
        (e) => {
          console.log('API初始化成功...')
          resolve(e)
        },
        false
      )

      this.scriptElement.addEventListener(
        'error',
        (e) => {
          this.removeScript()
          reject(e)
        },
        false
      )
    })
  }
  static removeScript() {
    if (this.scriptElement) {
      document.body.removeChild(this.scriptElement)
      this.scriptElement = null
    }
  }
  static addScript() {
    this.scriptElement = document.createElement('script')
    this.scriptElement.setAttribute('type', 'text/javascript')
    this.scriptElement.setAttribute('async', 'sync')
    this.scriptElement.src = this.url
    document.body.appendChild(this.scriptElement)
  }
  static esriAPILoader(css) {
    const options = {
      url: this.url
    }

    return esriLoader.loadScript(options).then(function () {
      esriLoader.loadCss(css)
      console.log('API初始化成功...')
    })
  }
  static removeLibs() {
    this.blibs.forEach((lib) => {
      document.body.removeChild(lib)
    })
    this.blibs = []
  }
  static unLoad() {
    switch (window.mapconfig.mapType) {
      case mapType.Gaode:
        this.removeScript()
        break
      case mapType.BD:
        this.removeScript()
        this.removeLibs()
        break
      case mapType.SuperAPI:
        this.removeScript()
        break
    }
  }
}
