import React, { useEffect, useMemo, useRef, useState } from 'react'
import Helmet from 'react-helmet'
import Container from 'components/Container'
import { init, EChartOption, ECharts } from 'echarts'
import styles from './index.less'
import api from 'utils/api'
import request from 'utils/request'
import ICON_BM from 'assets/images/icon_bm.png'
import ICON_ML from 'assets/images/icon_mulu.png'
import ICON_XT from 'assets/images/icon_xl.png'
import ICON_ZY from 'assets/images/icon_zy.png'
import IMG_BG_BM from 'assets/images/img_bg_bm.png'
import IMG_BG_ML from 'assets/images/img_bg_mulu.png'
import IMG_BG_XT from 'assets/images/img_bg_xl.png'
import IMG_BG_ZY from 'assets/images/img_bg_zy.png'

const DEFAULT_COLORS = [
  '#5470c6',
  '#91cc75',
  '#fac858',
  '#ee6666',
  '#73c0de',
  '#3ba272',
  '#fc8452',
  '#9a60b4',
  '#ea7ccc'
]

// tslint:disable-next-line:interface-name
interface DataOverviewState {
  originDept: number, // 来源部门数量
  countCatalogue: number, // 近一个月目录增长数量
  originSystem: number, // 来源系统数量
  dataSource: number, // 数据资源数量
  industryData: Array<{ name: string, value: number }>, // 行业分类饼图
  originDeptData: Array<{ name: string, value: number }>, // 来源部门柱状图
}

export default function DataOverview() {
  const pieElementRef = useRef<HTMLDivElement>(null)
  const barElementRef = useRef<HTMLDivElement>(null)
  const pieRef = useRef<ECharts>(null)
  const barRef = useRef<ECharts>(null)
  const [overview, setOverview] = useState<DataOverviewState>(
    {
      originDept: 0,
      countCatalogue: 0,
      originSystem: 0,
      dataSource: 0,
      industryData: [],
      originDeptData: []
    })

  const getDataScreening = async() => {
    try {
      const data = await request(api.dataScreening, { method: 'get' })
      setOverview((data as unknown as DataOverviewState))
    } catch (e) {
      console.log(e)
    }
  }
  const pieOption = useMemo<EChartOption>(() => ({
    tooltip: {
      trigger: 'item'
    },
    color: DEFAULT_COLORS,
    legend: {
      orient: 'vertical',
      right: 10,
      top: 'middle'
    },
    series: [
      {
        type: 'pie',
        radius: '50%',
        data: overview.industryData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }), [overview.industryData])
  const barOption = useMemo<EChartOption>(() => ({
    tooltip: {},
    dataset: {
      dimensions: ['name', 'value'],
      source: overview.originDeptData
    },
    xAxis: {},
    yAxis: {
      type: 'category',
      data: overview.originDeptData.map((o) => o.name)
    },
    series: [{
      type: 'bar',
      data: overview.originDeptData.map((o, idx) => ({
        value: o.value, itemStyle: {
          color: DEFAULT_COLORS[idx]
        }
      }))
    }]
  }), [overview.originDeptData])

  useEffect(() => {
    getDataScreening()
  }, [])


  useEffect(() => {
    barRef.current = init(barElementRef.current)
    barRef.current.setOption(barOption)
  }, [barOption])

  useEffect(() => {
    pieRef.current = init(pieElementRef.current)
    pieRef.current.setOption(pieOption)
  }, [pieOption])
  return (
    <Container>
      <Helmet title='数据总览' />

      <div className={styles['overview-container']}>
        <div className={styles['card-list']}>
          {[
            {
              title: '数据资源数量',
              num: overview.dataSource,
              icon: ICON_ZY,
              bg: IMG_BG_ZY
            },
            {
              title: '来源部门数量',
              num: overview.originDept,
              icon:  ICON_BM,
              bg: IMG_BG_BM
            },
            {
              title: '来源系统数量',
              num: overview.originSystem,
              icon: ICON_XT,
              bg: IMG_BG_XT
            },
            {
              title: '近一月目录新增数量',
              num: overview.countCatalogue,
              icon: ICON_ML,
              bg: IMG_BG_ML
            }
          ].map((item) => (
            <div className={styles.item} key={item.title} style={{backgroundImage: `url("${item.bg}")` }}>
              <div className={styles.title}>{item.title}</div>
              <div>
                <span className={styles.num}>{item.num}</span> 个
              </div>
              <img src={item.icon} alt={item.title} />
            </div>
          ))}
        </div>
        <div className={styles.charts}>
          <div>
            <div className={styles['chart-title']}>行业分类</div>
            <div className={styles['chart-desc']}>资源目录各行业资源数量</div>
            <div ref={pieElementRef} />
          </div>
          <div>
            <div className={styles['chart-title']}>数据资源来源部门</div>
            <div className={styles['chart-desc']}>数据资源来源部门TOP10</div>
            <div ref={barElementRef} />
          </div>
        </div>
      </div>
    </Container>
  )
}
