/*
 * 维护大屏配置文件
 *
 */
class Style {
  constructor(styleure) {
    let style = Object.assign({}, styleure)
    style.layers = []
    this._style = style
  }

  getStyle() {
    return JSON.parse(JSON.stringify(this._style))
  }

  getZoom() {
    return this._style.zoom
  }

  getCenter() {
    return this._style.center
  }

  getPitch() {
    return this._style.pitch
  }

  getBearing() {
    return this._style.bearing
  }

  setMapStyle(basemap) {
    this._style.basemap = basemap
  }

  addLayer(layer, beforeId) {
    let index = this._style.layers.findIndex(l => l.id === beforeId)
    if (index === -1) {
      index = this._style.layers.length
    }
    this._style.layers.splice(index, 0, layer)
  }

  /**
   * 移动图表的z方向顺序
   * @id {String} 需要移动的图表id
   * @beforeId  {String}  移动图表的位置，将目标图表移动到该图表的前面（上面）
   * 注：这里的前后是相对于Dom上的显示顺序，其对应的配置文件中的顺序于此相反
   */
  moveLayer(id, beforeId) {
    let index = this._style.layers.findIndex(l => l.id === id)
    let t = this._style.layers.splice(index, 1)[0]
    let beforeIndex = this._style.layers.findIndex(l => l.id === beforeId)
    this._style.layers.splice(beforeIndex + 1, 0, t)
  }

  removeLayer(id) {
    let index = this._style.layers.findIndex(l => l.id === id)
    if (index !== -1) {
      this._style.layers.splice(index, 1)
    }
  }

  setProperty(name, value) {
    this._style[name] = value
  }

  updateLayerProperty(id, name, value) {
    let layer = this._style.layers.find(l => l.id === id)
    layer[name] = value
  }

  getSourceProperty(id, name) {
    let layer = this._style.layers.find(l => l.id === id)
    return layer.source[name]
  }

  getEvent(id) {
    let layer = this._style.layers.find(l => l.id === id)
    return layer.events
  }

  updateEvent(id, events) {
    let layer = this._style.layers.find(l => l.id === id)
    layer.events = events
  }
}

export default Style
