/**
 * @author xiaoping
 * @email edwardhjp@gmail.com
 * @create date 2017-05-18 09:26:02
 * @modify date 2017-06-02 07:20:35
 * @desc [通用图options]
*/

const setUndef = (bool, ret) => {
  return bool === undefined ? ret : bool
}

// 设置初始化内容
const setInitial = (ret, options) => {
  ['color', 'backgroundColor', 'textStyle'].forEach(item => {
    if (options[item]) {
      ret[item] = options[item]
    }
  })
  ;['legend', 'tooltip', 'title'].forEach(item => {
    if (options[item]) {
      ret[item] = options[item]
      ret[item].textStyle = {
        color: ret[item].color,
        fontStyle: ret[item].fontStyle,
        fontWeight: ret[item].fontWeight,
        fontFamily: ret[item].fontFamily,
        fontSize: ret[item].fontSize,
      }
    }
  })
  // 初始化grid
  ret.grid = Object.assign({}, {
    top: '10%',
    bottom: '10%',
    left: '10%',
    right: '10%',
  }, options.grid)
  // 初始化series
  ret.series = []
  return ret
}

// 设置图表数据
const setData = (ret, options) => {
  let datas = options.datas || {}
  if (datas.legend) {
    if (!ret.legend) ret.legend = {}
    ret.legend.data = datas.legend.map(item => {
      return {
        name: item.name,
        icon: item.icon || 'circle',
        textStyle: {
          color: item.color,
          fontStyle: item.fontStyle,
          fontWeight: item.fontWeight,
          fontFamily: item.fontFamily,
          fontSize: item.fontSize,
        },
      }
    })
  }
  return ret
}

function setInitOpts(options = {}) {
  let ret = {}
  setInitial(ret, options)
  setData(ret, options)
  return options.custom ? options : ret
}

const setAxis = (ret, options) => {
  let x = options.xAxis || {}
  let y = options.yAxis || {}
  ret.xAxis = Object.assign({}, x)
  ret.yAxis = Object.assign({}, y)

  ;[ret.xAxis, ret.yAxis].forEach((item, index) => {
    let key = index === 0 ? x : y
    if (index === 0) {
      item.type = key.type || 'category'
    } else {
      item.type = key.type || 'value'
    }
    item.boundaryGap = setUndef(key.boundaryGap, true)
    item.nameGap = setUndef(key.nameGap, 15)
    item.splitNumber = setUndef(key.splitNumber, 5)
    item.nameTextStyle = {
      color: item.nameColor,
      fontStyle: key.nameFontStyle,
      fontWeight: key.nameFontWeight,
      fontFamily: key.nameFontFamily,
      fontSize: key.nameFontSize,
    }
    item.axisLine = {
      show: key.lineShow,
      onZero: key.lineOnZero,
      lineStyle: {
        color: key.lineColor,
        width: key.lineWidth,
        type: key.lineType,
        shadowBlur: key.lineShadowBlur,
        shadowColor: key.lineShadowColor,
        shadowOffsetX: key.lineShadowOffsetX,
        shadowOffsetY: key.lineShadowOffsetY,
        opacity: key.lineOpacity,
      }
    }
    item.axisTick = {
      show: key.tickShow,
      alignWithLabel: key.tickAlignWithLabel,
      interval: setUndef(key.tickInterval, 'auto'),
      inside: key.tickInside,
      length: setUndef(key.tickLength, 5),
      lineStyle: {
        color: key.tickColor,
        width: key.tickWidth,
        type: key.tickType,
        shadowBlur: key.tickShadowBlur,
        shadowColor: key.tickShadowColor,
        shadowOffsetX: key.tickShadowOffsetX,
        shadowOffsetY: key.tickShadowOffsetY,
        opacity: key.tickOpacity,
      }
    }
    item.axisLabel = {
      show: setUndef(key.labelShow, true),
      interval: setUndef(key.labelInterval, 'auto'),
      inside: key.labelInside,
      rotate: key.labelRotate,
      margin: setUndef(key.labelMargin, 8),
      formatter: key.labelFormatter,
      showMinLabel: key.labelShowMaxLabel,
      showMaxLabel: key.labelShowMaxLabel,
      textStyle: {
        color: key.labelColor,
        fontStyle: key.labelFontStyle,
        fontWeight: key.labelFontWeight,
        fontFamily: key.labelFontFamily,
        fontSize: key.labelFontSize,
        align: key.labelAlign,
        baseline: key.labelBaseline,
      }
    }
    item.splitLine = {
      show: setUndef(key.splitLineShow, true),
      interval: setUndef(key.splitLineInterval, 'auto'),
      lineStyle: {
        color: key.splitLineColor,
        width: setUndef(key.splitLineWidth, 1),
        type: key.splitLineType,
        shadowBlur: key.splitLineShadowBlur,
        shadowColor: key.splitLineShadowColor,
        shadowOffsetX: key.splitLineShadowOffsetX,
        shadowOffsetY: key.splitLineShadowOffsetY,
        opacity: key.splitLineOpacity,
      }
    }
    item.splitArea = {
      show: key.splitAreaShow,
      interval: setUndef(key.splitAreaInterval, 'auto'),
      areaStyle: {
        color: key.splitAreaColor,
        shadowBlur: key.splitAreaShadowBlur,
        shadowColor: key.splitAreaShadowColor,
        shadowOffsetX: key.splitAreaShadowOffsetX,
        shadowOffsetY: key.splitAreaShadowOffsetY,
        opacity: key.splitAreaOpacity,
      }
    }
  })
  return ret
}

const setLabel = (item) => {
  let ret = {}
  ;['label', 'elabel'].forEach(pre => {
    ret[pre === 'label' ? 'normal' : 'emphasis'] = {
      show: setUndef(item[pre + 'Show'], true),
      position: item[pre + 'Position'],
      formatter: setUndef(item[pre + 'Formatter'], '{c}'),
      textStyle: {
        color: item[pre + 'Color'],
        fontStyle: item[pre + 'FontStyle'],
        fontWeight: item[pre + 'FontWeight'],
        fontFamily: item[pre + 'FontFamily'],
        fontSize: item[pre + 'FontSize'],
      },
    }
  })
  return ret
}

const setItem = (item) => {
  let ret = {}
  ;['item', 'eitem'].forEach(pre => {
    ret[pre === 'item' ? 'normal' : 'emphasis'] = {
      color: item[pre + 'Color'],
      borderColor: item[pre + 'BorderColor'],
      borderWidth: item[pre + 'BorderWidth'],
      borderType: item[pre + 'BorderType'],
      barBorderRadius: item[pre + 'BarBorderRadius'],
      shadowBlur: item[pre + 'ShadowBlur'],
      shadowColor: item[pre + 'ShadowColor'],
      shadowOffsetX: item[pre + 'ShadowOffsetX'],
      shadowOffsetY: item[pre + 'ShadowOffsetY'],
      opacity: item[pre + 'Opacity'],
    }
  })
  return ret
}

const setOthers = (item, key, type) => {
  let ret = {}
  switch (type) {
    case 'line':
      if (key === 'lineStyle') {
        let pre = 'line'
        ret.normal = {
          color: item[pre + 'Color'],
          width: item[pre + 'Width'],
          shadowBlur: item[pre + 'ShadowBlur'],
          shadowColor: item[pre + 'ShadowColor'],
          shadowOffsetX: item[pre + 'ShadowOffsetX'],
          shadowOffsetY: item[pre + 'ShadowOffsetY'],
          opacity: item[pre + 'Opacity'],
        }
      } else if (key === 'areaStyle') {
        let pre = 'area'
        ret.normal = {
          color: item[pre + 'Color'],
          shadowBlur: item[pre + 'ShadowBlur'],
          shadowColor: item[pre + 'ShadowColor'],
          shadowOffsetX: item[pre + 'ShadowOffsetX'],
          shadowOffsetY: item[pre + 'ShadowOffsetY'],
          opacity: item[pre + 'Opacity'],
        }
        if (item.areaType === 'linear') {
          ret.normal.color = {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: item.areaColor.map((item, index) => {
              return {
                offset: index,
                color: item
              }
            }),
            globalCoord: false
          }
        } else if (item.areaType === 'radial') {
          ret.normal.color = {
            type: 'radial',
            x: 0.5,
            y: 0.5,
            r: 0.5,
            colorStops: item.areaColor.map((item, index) => {
              return {
                offset: index,
                color: item
              }
            }),
            globalCoord: false
          }
        }
      }
      break
    case 'pie':
      if (key === 'labelLine') {
        ['normal', 'emphasis'].forEach(pre => {
          let key = pre === 'normal' ? '' : 'e'
          ret[pre] = {
            show: setUndef(item[key + 'labelLineShow'], true),
            // length: setUndef(item[key + 'labelLineLength'], 5),
            // length2: item[key + 'labelLineLength2'] || 0,
            smooth: item[key + 'labelLineSmooth'] || false,
            lineStyle: item[key + 'labelLineStyle'] || {},
          }
        })
      }
      break
    default:
      break
  }
  return ret
}

export {
  setInitOpts,
  setUndef,
  setAxis,
  setLabel,
  setItem,
  setOthers,
}
