/* 基本折线图 */
import Echart from '../echart'
import util from '../../libs/util'
import getOption from '../../libs/get_option'

class Line extends Echart {
  constructor(layer, screen) {
    super(layer, screen)
    this.data = {
      xData: [],
      seriesData: []
    }
  }

  setPaint() {
    let paint = this._initPaint()
    let option = this._getOption(paint)
    this._render(option)
  }

  _getOption(paint) {
    let option = getOption(paint, this.type)
    option.xAxis.data = this.data.xData || []
    let subType = paint.style
    if (subType === 'overlay') {
      // 堆叠柱状图
      for (let i = 0; i < option.series.length; i++) {
        option.series[i].stack = 'total'
        option.series[i].data = this.data.seriesData[i] || []
        option.legend.data.push(option.series[i].name)
      }
    } else {
      for (let i = 0; i < option.series.length; i++) {
        option.series[i].data = this.data.seriesData[i] || []
        option.legend.data.push(option.series[i].name)
      }
    }
    return option
  }

  reduceData(paint) {
    if (!this.originalData || !this.originalData.length) {
      this.data = {
        xData: [],
        seriesData: []
      }
      return
    }
    if (paint.xAxis && paint.xAxis.field) {
      let xField = paint.xAxis.field
      this.data.xData = util.pickColData(xField, this.originalData)
    }

    let series = paint.series
    let seriesData = []
    series.forEach(s => {
      let t = []
      if (s.field) {
        t = util.pickColData(s.field, this.originalData)
      }
      seriesData.push(t)
    })
    this.data.seriesData = seriesData
  }
}

export default Line
