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

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

    let o = {
        animation: !thumbnail,
        tooltip : {
            show: !silent && !thumbnail,
            trigger: "axis",
            axisPointer: {
                type: "cross",
                label: {
                    backgroundColor: "#6a7985"
                }
            }
        },
        legend: {
            show: !thumbnail,
            selectedMode: !silent
        },
        grid: {
            left: thumbnail ? 10 : '10%',
            right: thumbnail ? 10 : '10%',
            top: thumbnail ? 10 : 60,
            bottom: thumbnail ? 10 : 60,
            containLabel: !thumbnail
        },
        xAxis: [{
            show: !thumbnail,
            type: 'category',
            data: xAxis,
            name: xTitle || '横轴',
        }],
        yAxis: [{
            show: !thumbnail,
            name: yTitle || '纵轴',
            type: 'value'
        }],
        series: serieses.map(s => {
            return {
                name: s.name,
                type: 'bar',
                data: s.value,
                showSymbol: !thumbnail,
                silent,
            }
        }) 
    }
    return o;
}

function pieConfig(option, silent, thumbnail) {

    const { xAxis, columnName, serieses } = option;

    let o = {
        animation: !thumbnail,
        grid: {
            left: thumbnail ? 10 : '10%',
            right: thumbnail ? 10 : '10%',
            top: thumbnail ? 10 : 60,
            bottom: thumbnail ? 10 : 60,
            containLabel: !thumbnail
        },
        tooltip : {
            show: !silent && !thumbnail,
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            show: !thumbnail,
            data: xAxis,
            selectedMode: !silent
        },
        series : [
            {
                name: columnName,
                type: 'pie',
                // radius : '55%',
                // center: ['50%', '60%'],
                data: serieses[0].value,
                label: { show: !silent },
                labelLine: { show: !silent },
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
                silent
            }
        ]
    };
    return o;
}

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

    let o = {
        animation: !thumbnail,
        grid: {
            left: thumbnail ? 10 : '10%',
            right: thumbnail ? 10 : '10%',
            top: thumbnail ? 10 : 60,
            bottom: thumbnail ? 10 : 60,
            containLabel: !thumbnail
        },
        tooltip: {
            show: !silent && !thumbnail,
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            show: !thumbnail,
            selectedMode: !silent
        },
        xAxis:  {
            show: !thumbnail,
            name: xTitle,
            type: 'time'
        },
        yAxis: {
            show: !thumbnail,
            name: yTitle,
            type: 'value'
        },
        
        series: serieses.map(s => {
            return {
                name: s.name,
                type: 'line',
                data: s.mdata.map(m => {
                    return [m.date, m.value]
                }),
                showSymbol: !thumbnail,
                silent
            }
        })
    };

    return o;
}

function scatterConfig(option, silent, thumbnail) {
    const { serieses, xTitle, yTitle } = option;
    let o = {
        animation: !thumbnail,
        grid: {
            left: thumbnail ? 10 : '10%',
            right: thumbnail ? 10 : '10%',
            top: thumbnail ? 10 : 60,
            bottom: thumbnail ? 10 : 60,
            containLabel: !thumbnail
        },
        tooltip : {
            show: !silent && !thumbnail,
            showDelay : 0,
            axisPointer:{
                show: true,
                type : 'cross',
                lineStyle: {
                    type : 'dashed',
                    width : 1
                }
            }
        },
        legend: {
            show: !thumbnail,
            selectedMode: !silent
        },
        xAxis : [
            {
                show: !thumbnail,
                type : 'value',
                name: xTitle,
                scale:true,
                splitLine: {
                    show: false
                }
            }
        ],
        yAxis : [
            {
                show: !thumbnail,
                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]
                }),
                silent
            }
        })
    };
    return o;
}

function tableConfig(option, silent, thumbnail) {
    const { columns, data } = option;
    let o = {
        columns: columns.map(c => {
            if(c.dataIndex === 'percent') {
                return { ...c, render: (value, record, index) => ((+value*100).toFixed(2)) + '%'};
            }else {
                return { ...c, width: 100 };
            }
        }),
        data: data.map((d, i) => {
            return { ...d, key: i}
        })
    };
    return o;
}