import { getTooltip } from './utils'

const singleRadius = '70%'
const ringRadius = ['50%', '70%']

let dataObject = {}

function getSeries({ keys, values, chartColors }) {
  const data0 = keys.map((key, i) => {
    const value = values.find(o => o.name === key)['all_flow'] || 0
    return {
      value,
      name: key,
      itemStyle: {
        color: chartColors[i],
        borderColor: '#fff',
        borderWidth: 2,
      },
      labelLine: {
        length: 40,
        length2: 10,
      },
    }
  })
  let data1 = []

  keys.map((key, i) => {
    const foundItem = values.find(o => o.name === key)
    const items = Object.keys(foundItem)
    // console.log('foundItem', foundItem)
    let dataArray = []
    if (items.length > 3) {
      items.map(item => {
        // console.log('item', item)
        if (item !== 'all_flow' && item !== 'name') {
          const value = foundItem[item] || 0
          let obj = {
            value,
            name: item,
            itemStyle: {
              color: value === 0 ? 'transparent' : chartColors[i + 2],
              opacity: 0.8,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              normal: {
                position: 'outside',
                formatter: '{b} {d}% ',
                color: '#909090',
              },
            },
          }
          dataArray.push(obj)
        }
      })
    }

    dataObject[key] = dataArray
  })

  keys.map(key => {
    data1 = data1.concat(...dataObject[key])
  })
  //   console.log('data0', data0)
  //   console.log('data1', data1)

  const series = [
    {
      name: '访问来源',
      type: 'pie', //饼图
      // left:100,
      animation: false,
      radius: ['32%', '70%'], //内环尺寸
      label: {
        normal: {
          position: 'inside',
          formatter: '{b} {d}% ',
          color: '#fff',
        },
      },
      labelLine: {
        normal: {
          show: true,
        },
      },
      data: data0,
    },
    {
      name: '访问来源',
      type: 'pie',
      // left:100,
      radius: ['70%', '84%'], //外环尺寸
      animation: false,
      data: data1,
    },
  ]

  return series
}

export const pienest = (keys, values, settings, extra) => {
  //   console.log('series', series)

  const { tooltipVisible, legendVisible, tooltipFormatter } = extra

  const chartColors = ['#4884FB', '#FFB727', '#C1D6FF', '#FFDFA0']

  const series = getSeries({ keys, values, chartColors })

  const tooltip =
    tooltipVisible &&
    getTooltip({
      triggerType: 'item',
      tooltipFormatter,
      keys,
      dataObject,
      sortData: 'desc',
    })
  const options = {
    series,
    tooltip,
  }

  return options
}
