import { MapCoordTrans } from '../export/MapCoordTrans'

/**
 * @class BasicAdapter
 * @classdesc 适配器基类，各适配器继承此类
 */
export default class BasicAdapter {
  /**
   * 构造函数
   * @param {string} div 地图容器dom id
   */
  constructor(div) {
    this.div = div // 地图容器
    this.map = null // 地图实例
    this.events = [] //地图事件集合
    this.mapLayers = [] // 地图覆盖物图层 MapOverLayer
    this.coordTrans = new MapCoordTrans() //坐标转换
    this.baseMapLayers = [] //底图图层（gd:TileLayer;bd:BaseMapType;esri:esri/layers/...)
  }
  /**
   * 添加地图事件
   * @param {function} event 事件
   * @param {string} type 事件类型
   */
  addEvent(event, type) {
    this.events.push({ event: event, type: type })
  }

  /**
   * 根据图层名获得图层索引
   * @param {string} layername 图层名
   * @returns 索引，number
   */
  getLyrIndexByName(layername) {
    var inx = this.mapLayers.findIndex((item) => item.name === layername)
    return inx
  }
  /**
   * 根据图层名获得图层
   * @param {string} layername 图层名
   * @returns OverlayGroup，array
   */
  getLyrByName(layername) {
    var lyr = this.mapLayers.find((item) => item.name === layername)
    if (lyr == undefined) {
      return
    } else {
      return lyr.element
    }
  }
  /**
   * 根据图层名、图层类型获得图层
   * @param {string} layername 图层名
   * @param {MapLayerType} type 图层类型
   * @returns OverlayGroup，array
   */
  getLyrByNameandGeometry(layername, type) {
    var lyr = this.mapLayers.find((item) => item.name === layername && item.type === type)
    if (lyr == undefined) {
      return
    } else {
      return lyr.element
    }
  }
  /**
   * 根据图层名、图层类型获得图层索引
   * @param {string} layername 图层名
   * @param {MapLayerType} type 图层类型
   * @returns 索引，number
   */
  getLyrIndexByNameandGeometry(layername, type) {
    var inx = this.mapLayers.findIndex((item) => item.name === layername && item.type === type)
    return inx
  }
  /**
   * 根据可见性、图层类型获得图层
   * @param {bool} visible 图层是否可见
   * @param {MapLayerType} geometry  图层类型
   * @returns OverlayGroup，array
   */
  getLayerbyVisibilityandGeometry(visible, geometry) {
    var lyr = this.mapLayers.find((item) => item.visible === visible && item.type === geometry)
    if (lyr == undefined) {
      return
    } else {
      return lyr.element
    }
  }
  /**
   * 根据可见性获取图层
   * @param {bool} visible 图层是否可见
   * @returns array
   */
  getLayerNamebyVisibility(visible) {
    let layers = []
    for (let i = 0, len = this.mapLayers.length; i < len; i++) {
      if (this.mapLayers[i].visible === visible) {
        layers.push(this.mapLayers[i].name)
      }
    }
    return layers
  }
  /**
   * 根据图层类型获得图层名
   * @param {MapLayerType} geometry  图层类型
   * @returns string
   */
  getLayerNamebyGeometry(geometry) {
    var lyr = this.mapLayers.find((item) => item.type === geometry)
    if (lyr == undefined) {
      return
    } else {
      return lyr.name
    }
  }
  /**
   * 移除地图图层
   */
  removeMapLayers() {
    var len = this.mapLayers.length
    this.mapLayers.splice(0, len)
  }
  /**
   * 根据索引移除图层
   * @param {number} ind 图层索引
   */
  removeMapLayer(ind) {
    this.mapLayers.splice(ind, 1)
  }
  /**
   * 根据图层索引设置图层可见性
   * @param {number} index 图层索引
   * @param {bool} visible 图层是否可见
   */
  setLayerVisibility(index, visible) {
    this.mapLayers[index].visible = visible
  }
  /**
   * 销毁地图实例，清除地图事件、地图图层
   */
  destroyMap() {
    if (this.map) {
      this.map.destroy()
      this.events = []
      this.mapLayers = []
    }
  }
  /**
   * 添加地图图层
   * @param {EsriMapOverLayer｜GDMapOverLayer} layer 地图图层
   */
  addMapLayers(layer) {
    this.mapLayers.push(layer)
  }
  getZoom() {
    if (this.map) {
      return this.map.getZoom()
    }
  }
}
