import {
  DEFAULT_AXIS_COLOR,
  DEFAULT_LEGEND_BAR_ICON,
  DEFAULT_PARALLEL_COLORS,
  DEFAULT_TITLE_STYLE,
} from '../../constants'
import { formatData, getMaxData } from './utils'

function getLegend(legendMap) {
  if (!legendMap) return {}
  return {
    left: '2%',
    itemHeight: 12,
    itemWidth: 12,
    itemGap: 25,
    top: 10,
    data: legendMap.map((item, index) => {
      let icon = DEFAULT_LEGEND_BAR_ICON
      const obj = {
        name: item,
        icon: icon,
      }
      return obj
    }),
  }
}

function getParallelAxis(metrics, values, units) {
  const parallelAxis = []
  metrics.map((v, i) => {
    const max = getMaxData(values, v)
    if (units[v]) v = v + '/' + units[v]
    parallelAxis[i] = {
      dim: i,
      name: v,
      min: 0,
      max: max,
      interval: max / 5,
    }
  })
  return parallelAxis
}

function getSeries(metrics, values, dimension) {
  const series = []
  const dataTemp = formatData(values, metrics, dimension)
  const lineStyle = {
    normal: {
      width: 1,
      opacity: 0.5,
    },
  }
  values.forEach((item, index) => {
    let seriesItem = {
      name: item[dimension],
      type: 'parallel',
      data: [dataTemp[item[dimension]]],
      smooth: true,
      lineStyle,
    }
    series.push(seriesItem)
  })
  return series
}

function getParallel() {
  const parallel = {
    left: '5%',
    right: '5%',
    bottom: '15%',
    top: '30%',
    parallelAxisDefault: {
      type: 'value',
      nameGap: 20,
      nameLocation: 'start',
      splitNumber: 5,
      nameTextStyle: {
        color: '#4A4A4A',
        fontSize: 12,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: true,
        margin: 8,
        color: '#4A4A4A',
        fontSize: 12,
      },
      axisLine: {
        show: true,
        lineStyle: {
          width: 1,
          type: 'solid',
          color: DEFAULT_AXIS_COLOR,
        },
      },
    },
  }
  return parallel
}

export const parallel = (keys, values, settings, extra) => {
  keys = Array.isArray(keys) ? keys : []
  values = Array.isArray(values) ? values : []

  // 数据 自定义单位
  let units = {}
  // 默认数据项
  let tempMetrics = keys.slice(1).map((v, i) => {
    if (Array.isArray(v)) {
      units[v[0]] = v[1]
      return v[0]
    }
    return v
  })

  let legendMap = values.map((v, i) => {
    return v[keys[0]]
  })

  const {
    // 纬度
    dimension = [keys[0]],
    // 自定义数据项
    metrics = tempMetrics,
    grid,
    titleText,
  } = settings
  const { legendVisible } = extra

  const legend = legendVisible && getLegend(legendMap)
  const parallelAxis = getParallelAxis(metrics, values, units)
  const series = getSeries(metrics, values, dimension)
  const parallel = getParallel()
  const title = {
    text: titleText || '',
    textStyle: DEFAULT_TITLE_STYLE,
  }
  const options = {
    title,
    parallel,
    parallelAxis,
    legend,
    series,
    color: DEFAULT_PARALLEL_COLORS,
  }
  if (grid) options.grid = grid
  return options
}
