/**
 * @author xiaoping
 * @email edwardhjp@gmail.com
 * @create date 2017-05-09 03:56:33
 * @modify date 2017-05-18 06:16:45
 * @desc [bmap options]
*/

import {
  setUndef,
} from '@/utils/options'
import bmapStyles from './styleJson'

const initOptions = {
  series: []
}

// 设置图表参数
const setProps = (ret, options) => {
  let bmap = options.bmap
  // let geo = options.geoProps
  ret.bmap = {
    // 百度地图中心经纬度
    center: bmap.center || [],
    // 百度地图缩放，不可缩放的时候需要指定
    zoom: bmap.zoom || 13,
    // 是否开启拖拽缩放，可以只设置 'scale' 或者 'move'
    roam: bmap.roam || false,
    // 百度地图的自定义样式，见 http://developer.baidu.com/map/jsdevelop-11.htm
    mapStyle: { styleJson: bmapStyles[bmap.style || 'normal'] }
  }
  // ret.geo = {}
  ret.autoPlay = options.autoPlay || false
  ret.playDuration = options.playDuration || 4000
}

// 设置图表系列
const setSeries = (ret, options) => {
  let item = options.series
  ret.series[0] = {
    type: item.type || 'effectScatter',
    silent: setUndef(item.silent, true),
    coordinateSystem: item.coordinateSystem || 'bmap', // bmap=百度地图, geo=地理地图
    effectType: item.effectType || 'ripple', // 动效
    rippleEffect: item.rippleEffect || { // 动效设置
      period: 4,
      scale: 2.5,
      brushType: 'fill',
    },
    showEffectOn: item.showEffectOn || 'render', // 动效出现时机
    symbol: item.symbol || 'circle',
    symbolSize: item.symbolSize || 4,
    symbolRotate: item.symbolRotate || 0,
    label: {
      normal: {
        show: setUndef(item.labelShow, true),
        position: item.labelPosition || 'right',
        formatter: item.labelFormatter || '{c}',
        textStyle: {
          color: item.labelColor,
          fontSize: item.labelFontSize || 12,
          fontWeight: item.labelFontWeight || 'normal',
        },
      },
      emphasis: {
        show: setUndef(item.elabelShow, true),
        position: item.elabelPosition || item.labelPosition || 'right',
        formatter: item.elabelFormatter || item.labelFormatter || '{c}',
        textStyle: {
          color: item.elabelColor || item.labelColor,
          fontSize: item.elabelFontSize || item.labelFontSize || 12,
          fontWeight: item.elabelFontWeight || item.labelFontWeight || 'normal',
        },
      },
    },
    itemStyle: {
      normal: {
        color: item.itemColor,
        borderColor: item.itemBorderColor || '#000',
        borderWidth: item.itemBorderWidth || 0,
        opacity: item.itemColor ? 1 : 0,
      },
      emphasis: {
        color: item.eitemColor || item.itemColor,
        borderColor: item.eitemBorderColor || item.itemBorderColor || '#000',
        borderWidth: item.eitemBorderWidth || item.itemBorderWidth || 0,
        opacity: (item.eitemColor || item.itemColor) ? 1 : 0,
      },
    },
  }
}

// 设置图表数据
const setData = (ret, options) => {
  if (options.datas) {
    options.datas.forEach((data, index) => {
      let temp = ret.series[index]
      temp && (temp.data = data)
    })
  }
}

const setOptions = (options) => {
  let ret = Object.assign({}, initOptions)
  setProps(ret, options)
  setSeries(ret, options)
  setData(ret, options)
  return options.custom ? options : ret
}

export {
  initOptions,
  setOptions
}
