/* 双轴折线图 */
import Line from './line'
import getOption from '../../libs/get_option'

class DoubleAxis extends Line {
  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 { title, legend, grid, xAxis, series, tooltip } = getOption(
      paint,
      this.type
    )
    let yAxis = this.getYAxis(paint.yAxis)
    xAxis.data = this.data.xData || []
    for (let i = 0; i < series.length; i++) {
      series[i].data = this.data.seriesData[i] || []
      legend.data.push(series[i].name)
    }
    return { title, legend, grid, xAxis, yAxis, series, tooltip }
  }

  getYAxis(axisProperty) {
    let yAxis1 = {}
    let yAxis2 = {}
    if (!axisProperty) {
      axisProperty = [{}, {}]
    }
    if (Array.isArray(axisProperty)) {
      yAxis1 = getOption({ yAxis: axisProperty[0] || {} }, this.type).yAxis
      yAxis2 = getOption({ yAxis: axisProperty[1] || {} }, this.type).yAxis
    } else {
      yAxis1 = getOption({ yAxis: axisProperty }, this.type).yAxis
      yAxis2 = getOption({ yAxis: axisProperty }, this.type).yAxis
    }
    yAxis1.position = 'left'
    yAxis2.position = 'right'
    return [yAxis1, yAxis2]
  }
}

export default DoubleAxis
