import { $echarts } from '../../index.js';
import echartstyle from '../../libs/echarts-style.js';
import * as $switch from '../../extend/bar-simple.js';

export default ({ dom, param, opt }) => {
    const { $color, $tooltip, $grid } = echartstyle();

    const { data, icons } = param;

    const $data = data;

    /**
     * 导出配置项
     */
    const option = {
        color: $color.theme,
        grid: $grid,
        tooltip: Object.assign(
            {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow',
                },
            },
            $tooltip
        ),
        legend: {
            show: false,
            icon: 'rect',
            top: 10,
            right: 20,
            itemWidth: 10,
            itemHeight: 3,
            itemGap: 15,
            textStyle: {
                color: $color.legendLabel,
                fontSize: 12,
            },
        },
        xAxis: {
            type: 'value',
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: false,
                color: $color.xAxisLabel,
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: $color.xAxisLine,
                },
            },
            splitLine: {
                show: false,
                lineStyle: {
                    color: $color.xAxisLine,
                },
            },
        },
        yAxis: {
            type: 'category',
            data: $data.axis,
            axisTick: {
                show: false,
            },
            axisLabel: {
                color: 'rgba(255 ,255, 255, 1)',
                align: 'left',
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: $color.yAxisLine,
                },
            },
            splitLine: {
                show: false,
                lineStyle: {
                    color: $color.yAxisLine,
                },
            },
            offset: 60,
        },
        series: [],
    };

    /**
     * 数据处理
     */
    $data.series.map((item, index) => {
        const data = [];

        item.data.map((x, i) => {
            data.push({
                value: x,
                symbol: `image://${icons[i]}`,
            });
        });

        option.series.push({
            name: item.name,
            data: data,
            type: 'bar',
            type: 'pictorialBar',
            barWidth: 25,
            symbolRepeat: true,
            symbolSize: [37, 29],
            z: 1,
        });

        // 背景处理
        option.series.push({
            name: item.name,
            data: item.data.map((x) => Math.max(...item.data)),
            type: 'bar',
            type: 'pictorialBar',
            barWidth: 25,
            symbolRepeat: true,
            symbolSize: [37, 29],
            symbol: `image://${icons[icons.length - 1]}`,
            z: 0,
            label: {
                show: true,
                color: 'rgba(255, 255, 255, 1)',
                position: 'right',
                align: 'right',
                offset: [90, 0],
                formatter: (param) => {
                    const value = $data.series[0].data[param.dataIndex];
                    const unit = $data.series[0].unit;

                    return `${value} ${unit}`;
                },
            },
        });
    });

    /**
     * 渲染
     */
    const extensOption = $echarts.extens(opt, option);

    const echarts = $echarts.render(dom, extensOption);

    return echarts;
};
