import React from 'react';
import groupBy from 'lodash/groupBy';
import ReactEcharts from 'echarts-for-react';

const xAxis = [...new Array(24)].map((_, index) => add0(index));
export interface Distribute1Props {
    data?: { date: string; hour: string; value: number }[];
}
function add0(num: number): string {
    return num >= 10 ? num.toString() : `0${num}`;
}
function transferData(data: Distribute1Props['data']) {
    const chartData: [number, number, number][] = [];
    const dataGroupByDate = groupBy(data, 'date');
    let count = 0;
    for (const key in dataGroupByDate) {
        dataGroupByDate[key].forEach((item, index) => {
            chartData.push([index, count, item.value]);
        });
        count++;
    }
    return {
        yAxis: Object.keys(dataGroupByDate).map(item => item.substring(5)),
        chartData,
    };
}

const getGaugeOption = ({ data = [] }: Distribute1Props) => {
    const { yAxis, chartData } = transferData(data);
    const max = Math.max(...chartData.map(item => item[2]));
    if (!chartData?.length) return {};
    return {
        tooltip: {
            position: 'top',
            formatter: (params: any) => {
                const { value, marker } = params;
                const date = yAxis[value[1]];
                const hour = `${add0(value[0])}:00-${add0(value[0] + 1)}:00`;
                return `<div>${date} ${hour}</div><div>${marker} 投放次数：${value[2]}次</div>`;
            },
        },
        grid: {
            top: 50,
            bottom: 0,
            right: 0,
            left: 0,
            containLabel: true,
        },
        xAxis: {
            type: 'category',
            data: xAxis,
            splitArea: {
                show: true,
            },
            axisLabel: {
                interval: 0,
                color: '#fff',
            },
            axisTick: {
                show: false,
            },
        },
        yAxis: {
            type: 'category',
            data: yAxis,
            splitArea: {
                show: true,
            },
            axisLabel: {
                color: '#fff',
                interval: 0,
            },
            axisTick: {
                show: false,
            },
        },
        visualMap: {
            type: 'piecewise',
            min: 0,
            max: max,
            calculable: true,
            orient: 'horizontal',
            left: 'center',
            top: 0,
            inRange: {
                color: ['#364357', '#00759C', '#29A8F0', '#F5B829', '#E7473F'],
            },
            textStyle: {
                color: '#fff',
            },
        },
        series: [
            {
                name: '24h分布',
                type: 'heatmap',
                data: chartData,
                label: {
                    show: true,
                },
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowColor: 'rgba(0, 0, 0, 0.5)',
                    },
                },
            },
        ],
    };
};
const Distribute1 = (props: Distribute1Props) => {
    return <ReactEcharts option={getGaugeOption(props)} style={{ height: '100%' }} />;
};

export default Distribute1;
