import defaultsDeep from 'lodash/defaultsDeep';
export default function getBar(
    charsData,
    xAxisData,
    color,
    tooltipFormatter,
    barOpt,
    changeOpt,
    type
) {
    let opt = {
        color,
        legend: {
            icon: "circle",
            textStyle: {
                color: type == "dark" ? "#fff" : "#000"
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            tooltipFormatter
        },
        xAxis: {
            type: 'category',
            data: xAxisData,
            axisLine: {
                lineStyle: {
                    color: type == "dark" ? "#2f309c" : "#000",
                    type: "dotted"
                }
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: type == "dark" ? "#bac4e8" : "#000",
                interval: 0,
                formatter: (params) => {
                    var newParamsName = "";
                    var paramsNameNumber = params.length;
                    var provideNumber = 5;
                    var rowNumber = Math.ceil(paramsNameNumber / provideNumber);
                    if (paramsNameNumber > provideNumber) {
                        for (var p = 0; p < rowNumber; p++) {
                            var tempStr = "";
                            var start = p * provideNumber;
                            var end = start + provideNumber;
                            if (p == rowNumber - 1) {
                                tempStr = params.substring(start, paramsNameNumber);
                            } else {
                                if (p > 2) {
                                    tempStr = "...";
                                    newParamsName += tempStr;
                                    break;
                                } else {
                                    tempStr = params.substring(start, end);
                                    if (p < 2) {
                                        tempStr += "\n";
                                    }
                                }
                            }
                            newParamsName += tempStr;
                        }

                    } else {
                        newParamsName = params;
                    }
                    return newParamsName
                }
            }
        },
        yAxis: {
            axisLabel: {
                color: type == "dark" ? "#bac4e8" : "#000"
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: type == "dark" ? "#2f309c" : "#000"
                }
            },
            axisTick: {
                show: false
            },
            splitLine: {
                lineStyle: {
                    color: type == "dark" ? "#2f309c" : "#000",
                    type: "dotted"
                }
            }
        },
        series: formatter(charsData, barOpt)
    }
    if (changeOpt) {
        opt = defaultsDeep({}, changeOpt, opt)
    }
    return opt
}

function formatter(data, barOpt) {
    let arr = [];
    data.forEach(item => {
        let opt = {
            name: item.name,
            type: 'bar',
            barWidth: "20%",
            barGap: '0',
            data: item.data
        }
        if (barOpt) {
            opt = defaultsDeep({}, barOpt, opt)
        }
        arr.push(opt)
    })
    return arr
}