import defaultSeriesProperty from '../constants/default_series'
import symbols from '../constants/symbols'
import util from './util'

function mergeSeries(obj, type) {
  let dsp = util.deepCopy(defaultSeriesProperty[type])
  return util.mergeObjs(dsp, obj)
}

function getSeries(paint, type) {
  let newSeries = []
  let seriesProperties = paint.series
  let newSeriesProperty = {}
  if (seriesProperties && seriesProperties.length) {
    for (let i = 0; i < seriesProperties.length; i++) {
      let series = {}
      switch (type) {
        case 'bar':
        case 'bar_h':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'bar')
          series = getBar(newSeriesProperty)
          break
        case 'bar_custom':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'pictorialBar')
          series = getPictorialBar(newSeriesProperty)
          break
        case 'line':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'line')
          series = getLine(newSeriesProperty)
          break
        case 'double_axis':
          if (seriesProperties[i].type === 'line') {
            newSeriesProperty = mergeSeries(seriesProperties[i], 'line')
            series = getLine(newSeriesProperty)
          } else {
            newSeriesProperty = mergeSeries(seriesProperties[i], 'bar')
            series = getBar(newSeriesProperty)
          }
          break
        case 'scatter':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'scatter')
          series = getScatter(newSeriesProperty)
          break
        case 'gauge':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'gauge')
          series = getGauge(newSeriesProperty)
          break
        case 'pie':
        case 'pie_ring':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'pie')
          series = getPie(newSeriesProperty)
          break
        case 'funnel':
          newSeriesProperty = mergeSeries(seriesProperties[i], 'funnel')
          series = getFunnel(newSeriesProperty)
      }
      newSeries.push(series)
    }
  } else {
    switch (type) {
      case 'word_cloud':
        newSeriesProperty = mergeSeries(seriesProperties, 'wordCloud')
        newSeries = getWordCloud(newSeriesProperty)
        break
      case 'radar':
        newSeries = getRadar()
        break
    }
  }
  return newSeries
}

function getBar(config) {
  if (Array.isArray(config.barColor)) {
    config.barColor = util.gradient(
      'echarts',
      config.gradientDirection,
      ...config.barColor
    )
  }
  return {
    name: config.name,
    type: 'bar',
    data: [],
    yAxisIndex: config.yAxisIndex,
    barWidth: config.barWidth,
    itemStyle: {
      color: config.barColor,
      barBorderRadius: config.barBorderRadius
    },
    label: {
      show: config.showLabel,
      color: config.labelColor,
      fontSize: config.labelSize,
      position: config.labelPosition
    }
  }
}

function getLine(config) {
  if (Array.isArray(config.lineColor)) {
    config.lineColor = util.gradient(
      'echarts',
      config.gradientDirection || 'top',
      ...config.lineColor
    )
  }
  if (Array.isArray(config.areaColor)) {
    config.areaColor = util.gradient(
      'echarts',
      config.gradientDirection || 'top',
      ...config.areaColor
    )
  }
  let series = {
    name: config.name,
    type: 'line',
    data: [],
    yAxisIndex: config.yAxisIndex,
    symbol: config.symbol,
    symbolSize: config.symbolSize,
    itemStyle: {
      color: config.lineColor,
      opacity: config.opacity
    },
    lineStyle: {
      color: config.lineColor,
      width: config.lineWidth,
      type: config.lineType
    },
    label: {
      show: config.showLabel,
      color: config.labelColor,
      fontSize: config.labelSize,
      position: config.labelPosition
    },
    smooth: config.smooth
  }
  if (config.areaColor) {
    series.areaStyle = {
      opacity: config.areaOpacity,
      color: config.areaColor
    }
  }
  return series
}

function getPictorialBar(config) {
  if (Array.isArray(config.barColor)) {
    config.barColor = util.gradient(
      'echarts',
      config.gradientDirection,
      ...config.barColor
    )
  }
  return {
    name: config.name,
    type: 'pictorialBar',
    data: [],
    symbol: symbols[config.symbol] || config.symbol,
    symbolSize: config.symbolSize,
    symbolRepeat: config.symbolRepeat,
    symbolMargin: config.symbolInterval,
    barWidth: config.barWidth,
    barGap: config.barGap,
    itemStyle: {
      color: config.barColor,
      opacity: config.opacity
    },
    label: {
      show: config.showLabel,
      color: config.labelColor,
      fontSize: config.labelSize,
      position: config.labelPosition
    }
  }
}

function getScatter(config) {
  if (Array.isArray(config.color)) {
    config.color = util.gradient(
      'echarts',
      config.gradientDirection,
      ...config.color
    )
  }
  return {
    name: config.name,
    type: 'scatter',
    symbol: config.symbol,
    symbolSize: config.symbolSize,
    itemStyle: {
      opacity: config.opacity,
      color: config.color
    },
    label: {
      show: config.showLabel,
      color: config.labelColor,
      formatter: config.labelFormatter,
      fontSize: config.labelSize,
      position: config.labelPosition
    }
  }
}

function getGauge(config) {
  /*if (Array.isArray(config.color)) {
    config.color = util.gradient('echarts', config.gradientDirection, ...config.color)
  }*/

  let series = {
    name: config.name,
    type: 'gauge',
    center: config.center,
    radius: config.radius,
    min: config.min,
    max: config.max,
    startAngle: config.startAngle,
    endAngle: config.endAngle,
    splitNumber: config.splitNumber,
    axisLine: {
      lineStyle: {
        width: config.width
      }
    },
    axisTick: {
      show: config.showTick,
      length: 15,
      lineStyle: {
        color: 'auto'
      }
    },
    splitLine: {
      show: config.showTick,
      length: 20,
      lineStyle: {
        color: 'auto'
      }
    },
    axisLabel: {
      show: config.showLabel,
      color: config.labelColor,
      fontSize: config.labelSize
    },
    title: {
      show: config.showTitle,
      color: config.titleColor,
      fontSize: config.titleSize,
      offsetCenter: config.titlePosition
    },
    detail: {
      show: config.showValue,
      color: config.valueColor,
      fontSize: config.valueSize,
      offsetCenter: config.valuePosition,
      formatter: config.valueFormatter
    }
  }
  if (config.decimalDigits !== undefined) {
    let decimalDigits = Number(config.decimalDigits) || 0
    series.detail.formatter = value => {
      return value.toFixed(decimalDigits)
    }
  }
  let colors = config.colors
  if (colors && colors.length) {
    let step = 1 / colors.length
    colors = colors.map((c, index) => [step * (index + 1), c])
    series.axisLine.lineStyle.color = colors
  }
  return series
}

function getPie(config) {
  return {
    name: config.name,
    type: 'pie',
    radius: config.radius,
    center: config.center,
    startAngle: config.startAngle,
    roseType: config.rosePie ? 'radius' : undefined,
    labelLine: {
      show: config.showLabelLine
    },
    label: {
      color: config.labelColor,
      fontSize: config.labelSize,
      show: config.showLabel,
      formatter: config.labelFormatter,
      rich: {
        center: {
          align: 'center',
          fontSize: config.labelSize,
          color: config.labelColor,
          lineHeight: config.labelSize + 10
        }
      }
    },
    data: []
  }
}

function getFunnel(config) {
  return {
    name: config.name,
    type: 'funnel',
    left: config.left,
    top: config.top,
    width: config.width,
    height: config.height,
    minSize: config.minSize,
    maxSize: config.maxSize,
    gap: config.gap,
    sort: config.inverse ? 'ascending' : 'descending',
    labelLine: {
      show: config.showLabelLine,
      length: 20
    },
    itemStyle: {
      borderWidth: 0
    },
    label: {
      color: config.labelColor,
      fontSize: config.labelSize,
      show: config.showLabel,
      position: config.labelPosition,
      formatter: config.labelFormatter
    },
    data: []
  }
}

function getWordCloud(config) {
  return [
    {
      name: config.name,
      type: 'wordCloud',
      left: 'center',
      top: 'center',
      width: '100%',
      height: '100%',
      gridSize: config.gap,
      drawOutOfBound: false,
      sizeRange: [config.minSize, config.maxSize],
      rotationRange: [-90, 90],
      maskImage: null,
      textStyle: {
        normal: {
          color: function() {
            if (config.colors && config.colors.length) {
              let num = Math.round(Math.random() * (config.colors.length - 1))
              return config.colors[num]
            }
            return (
              'rgb(' +
              [
                Math.round(Math.random() * 160),
                Math.round(Math.random() * 160),
                Math.round(Math.random() * 160)
              ].join(',') +
              ')'
            )
          }
        },
        emphasis: {
          shadowBlur: 5,
          shadowColor: '#333'
        }
      },
      data: []
    }
  ]
}

function getRadar() {
  return [
    {
      type: 'radar',
      data: []
    }
  ]
}
export default getSeries
