/*
 * 维护大屏配置文件
 *
 */
import util from './libs/util'

class Config {
  constructor(configure) {
    let config = Object.assign({}, configure)
    config.components = []
    this._config = config
  }

  getConfig() {
    return JSON.parse(JSON.stringify(this._config))
  }

  getWidth() {
    return this._config.width
  }

  getHeight() {
    return this._config.height
  }

  getZoomMode() {
    return this._config.scale
  }

  insertComponent(component, beforeId) {
    let index = this._config.components.findIndex(l => l.id === beforeId)
    if (index === -1) {
      index = this._config.components.length
    }
    this._config.components.splice(index, 0, component)
  }

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

  removeComponent(id) {
    let index = this._config.components.findIndex(l => l.id === id)
    if (index !== -1) {
      this._config.components.splice(index, 1)
    }
  }

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

  updateLayoutProperty(id, name, value) {
    let component = util.findComponent(this._config.components, id)
    component.layout[name] = value
  }

  getLayoutProperty(id, name) {
    let component = util.findComponent(this._config.components, id)
    return component.layout[name]
  }

  updateLayout(id, layout) {
    let component = util.findComponent(this._config.components, id)
    component.layout = layout
  }

  updatePaint(id, paint) {
    let component = util.findComponent(this._config.components, id)
    component.paint = paint
  }

  updatePaintProperty(id, name, value) {
    let component = util.findComponent(this._config.components, id)
    component.paint[name] = value
  }

  getPaintProperty(id, name) {
    let component = util.findComponent(this._config.components, id)
    return component.paint[name]
  }

  updateSource(id, source) {
    let component = util.findComponent(this._config.components, id)
    component.source = source
  }

  updateSourceProperty(id, name, value) {
    let component = util.findComponent(this._config.components, id)
    component.source[name] = value
  }

  getSourceProperty(id, name) {
    let component = util.findComponent(this._config.components, id)
    return component.source[name]
  }

  getEvent(id) {
    let component = util.findComponent(this._config.components, id)
    return component.events
  }

  updateEvent(id, events) {
    let component = util.findComponent(this._config.components, id)
    component.events = events
  }
}

export default Config
