export default (config) => {
    const { viewType, option } = config;
    let o;
    switch(viewType) {
        case 'bar': {
            o = barConfig(option);
            break;
        }
        case 'pie': {
            o = pieConfig(option);
            break;
        }
        case 'line': {
            o = lineConfig(option);
            break;
        }
        case 'scatter': {
            o = scatterConfig(option);
            break;
        }
        case 'aggregateTable': {
            o = tableConfig(option);
            break;
        }case 'dataView' : {
            o = tableConfig(option);
            break;
        }
        default:{
            o = {};
            break;
        }
    }
    return o;
}

function barConfig(option) {
    const { xAxis, serieses, xTitle, yTitle } = option;

    let o = {
        tooltip : {
            trigger: "axis",
            axisPointer: {
                type: "cross",
                label: {
                    backgroundColor: "#6a7985"
                }
            }
        },
        legend: {
            show: true
        },
        xAxis: [{
            type: 'category',
            data: xAxis,
            name: xTitle || '横轴',
        }],
        yAxis: [{
            name: yTitle || '纵轴',
            type: 'value'
        }],
        series: serieses.map(s => {
            return {
                name: s.name,
                type: 'bar',
                data: s.value,
                // stack: s.stack
            }
        }) 
    }
    return o;
}

function pieConfig(option) {

    const { xAxis, columnName, serieses } = option;

    let o = {
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            data: xAxis
        },
        series : [
            {
                name: columnName,
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data: serieses[0].value,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    return o;
}

function lineConfig(option) {
    const { serieses, xTitle, yTitle } = option;

    let o = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            show: true
        },
        xAxis:  {
            name: xTitle,
            type: 'time'
        },
        yAxis: {
            name: yTitle,
            type: 'value'
        },
        
        series: serieses.map(s => {
            return {
                name: s.name,
                type: 'line',
                data: s.mdata.map(m => {
                    return [m.date, m.value]
                })
            }
        })
    };

    return o;
}

function scatterConfig(option) {
    const { serieses, xTitle, yTitle } = option;
    let o = {
        tooltip : {
            showDelay : 0,
            axisPointer:{
                show: true,
                type : 'cross',
                lineStyle: {
                    type : 'dashed',
                    width : 1
                }
            }
        },
        legend: {
            show: true
        },
        xAxis : [
            {
                type : 'value',
                name: xTitle,
                scale:true,
                splitLine: {
                    show: false
                }
            }
        ],
        yAxis : [
            {
                type : 'value',
                name: yTitle,
                scale:true,
                splitLine: {
                    show: false
                }
            }
        ],
        series : serieses.map(s => {
            return {
                name: s.name,
                type: 'scatter',
                data: s.mdata.map(m => {
                    return [m.date, m.value]
                })
            }
        })
    };
    return o;
}

function tableConfig(option) {
    const { columns, data } = option;
    let o = {
        columns: columns.map(c => {
            c.width = 100;
            if(c.dataIndex === 'percent') { // 这里只在总体统计图下生效
                return { ...c, render: (value, record, index) => {console.log(record);return ((+value*100).toFixed(2)) + '%'} };
            }else {
                return c;
            }
        }),
        data: data.map((d, i) => {
            return { ...d, key: i}
        })
    };
    return o;
}