import _ from 'lodash'
import * as echarts from 'echarts'

const mainOption = function (data) {
    const dateList = _.groupBy(data, p => {
        return p.date
    })
    const keys = Object.keys(dateList)
    const timeLineData = []
    keys.map(p1 => {
        timeLineData.push(dateList[p1][0].lineNum)
    })

    function getDayOptions () {
        const result = []
        for (var i = 0; i < keys.length; i++) {
            result.push({
                dataset: [
                    {
                        id: 'sumA',
                        fromDatasetId: 'raw',
                        transform: [
                            {
                                type: 'filter',
                                config: { dimension: 'lineNum', '<=': dateList[keys[i]][0].lineNum }
                            }
                        ]
                    },
                    {
                        id: 'sum2',
                        fromDatasetId: 'raw',
                        transform: [
                            {
                                type: 'filter',
                                config: { dimension: 'lineNum', '>=': dateList[keys[i]][0].lineNum }
                            }
                        ]
                    }
                ]
            })
        }

        return result
    }


    return {
        title: {
            show: false,
            text: '井号'
        },
        timeline: {
            bottom: -6,
            axisType: 'category',
            autoPlay: true,
            playInterval: 2000,
            left: 0,
            right: 0,
            data: timeLineData,
            label: {
                formatter: function (v, i) {
                    return data[i * 24].date
                }
            }
        },
        backgroundColor: 'transparent',
        grid: {
            bottom: 50,
            show: true,
            backgroundColor: 'transparent',
            borderWidth: 0
        },
        xAxis: {
            type: 'value',
            splitLine: { show: false },
            axisLabel: {
                show: false
            },
            boundaryGap: false
        },
        yAxis: {
            type: 'value',
            name: '数量'
        },
        dataset: [
            {
                id: 'root',
                dimensions: ['lineNum', 'date', 'hour', 'total', 'fzqk', 'gcyj', 'zjy'],
                source: data
            },
            {
                id: 'raw',
                fromDatasetId: 'root',
                transform: [{
                    type: 'ecSimpleTransform:aggregate',
                    config: {
                        groupBy: 'date',
                        resultDimensions: [
                            { name: 'lineNum', from: 'lineNum', method: 'min' },
                            { from: 'date' },
                            { from: 'hour' },
                            { name: 'total', from: 'total', method: 'sum' },
                            { name: 'fzqk', from: 'fzqk', method: 'sum' },
                            { name: 'gcyj', from: 'gcyj', method: 'sum' },
                            { name: 'zjy', from: 'zjy', method: 'sum' }
                        ],
                        print: true
                    }
                }]
            },
            {
                id: 'sumA',
                fromDatasetId: 'raw',
                transform: [
                    {
                        type: 'filter',
                        config: { dimension: 'lineNum', '<=': 0 }
                    }
                ]
            },
            {
                id: 'sum2',
                fromDatasetId: 'raw',
                transform: [
                    {
                        type: 'filter',
                        config: { dimension: 'lineNum', '>=': 0 }
                    }
                ]
            }
        ],
        series: [
            {
                id: 'sumA',
                type: 'line',
                name: '合计',
                datasetIndex: 2,
                encode: {
                    x: 'lineNum',
                    y: 'total'
                }
            },
            {
                id: 'fzqkA',
                type: 'line',
                name: '邻井复杂',
                datasetIndex: 2,
                encode: {
                    x: 'lineNum',
                    y: 'fzqk',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'gcyjA',
                type: 'line',
                name: '工程预警',
                datasetIndex: 2,
                encode: {
                    x: 'lineNum',
                    y: 'gcyj',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'zjyA',
                type: 'line',
                name: '钻井液',
                datasetIndex: 2,
                encode: {
                    x: 'lineNum',
                    y: 'zjy',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'sum2',
                type: 'line',
                name: '合计',
                datasetIndex: 3,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'total',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'fzqk2',
                type: 'line',
                name: '邻井复杂',
                datasetIndex: 3,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'fzqk',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'gcyj2',
                type: 'line',
                name: '工程预警',
                datasetIndex: 3,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'gcyj',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'zjy2',
                type: 'line',
                name: '钻井液',
                datasetIndex: 3,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'zjy',
                    itemId: 'lineNum'
                }
            }
        ],
        options: getDayOptions(),
        timeLineData,
        keys
    }
}

const subOption = function (yyyyMMdd, data) {
    return {
        legend: {
            show: false,
            top: 'bottom'
        },
        grid: {
            bottom: 50,
            show: true,
            backgroundColor: 'transparent',
            borderWidth: 0
        },
        backgroundColor: 'transparent',
        dataset: [
            {
                id: 'raw',
                dimensions: ['lineNum', 'date', 'hour', 'total', 'fzqk', 'gcyj', 'zjy'],
                source: data
            },
            {
                id: 'hours',
                fromDatasetId: 'raw',
                transform: [{
                    type: 'filter',
                    config: { dimension: 'date', '=': yyyyMMdd }
                }],
                print: true
            }
        ],
        xAxis: {
            type: 'category'
        },
        yAxis: {
            type: 'value'
        },
        toolbox: {
            show: false,
            feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        series: [
            {
                name: '报警合计（小时）',
                type: 'line',
                itemStyle: {
                    color: 'rgb(255, 70, 131)'
                },
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                            offset: 0,
                            color: 'rgb(255, 158, 68)'
                        },
                        {
                            offset: 1,
                            color: 'rgb(255, 70, 131)'
                        }
                    ])
                },
                datasetIndex: 1,
                encode: {
                    x: 'hour',
                    y: 'total'
                }
            }
        ]
    }
}

export default {
    mainOption,
    subOption
}
