import { reactive, watch } from 'vue'
import { ECOption } from '@/components/base/echarts/core'
import { timerInterceptor } from '@/utils/timer'
import { useThemeStore } from '@/stores'
import { EchartsDataset, EchartsOptions, Colors } from './interface'
import moment from 'moment'

const { appTheme } = useThemeStore();

function getColors() {
    // 默认主题色配置
    const defaultColors: Colors = {
        upColor: '#ff3333',
        downColor: '#0aab62',
        xAxisLineColor: 'rgba(128,128,128,.1)',
        yAxisLineColor: 'rgba(128,128,128,.1)',
    }

    const colors = {
        default: defaultColors,
        dark: defaultColors,
        light: {
            ...defaultColors,
            xAxisLineColor: '#dae5ec',
            yAxisLineColor: '#dae5ec',
        }
    }

    return colors[appTheme.value];
}

export function useOptions(dataset: EchartsDataset) {
    // 图表配置项
    const options = reactive<EchartsOptions>({
        colors: getColors(),
        candlestick: {},
        macd: {},
        vol: {},
        kdj: {},
        cci: {},
    })

    const getDefaultOption = (): ECOption => {
        const { source } = dataset.candlestick;
        const { xAxisLineColor } = options.colors;

        return {
            grid: {
                left: '10px',
                right: '10px',
                top: '15px',
                bottom: '10px',
                containLabel: true,
            },
            dataZoom: {
                type: 'inside',
                startValue: source.length - 120, // 起始显示K线条数(最新120条)
                endValue: source.length,
                minValueSpan: 50, // 限制窗口缩放显示最少数据条数
                maxValueSpan: 400, // 限制窗口缩放显示最大数据条数
            },
            axisPointer: {
                label: {
                    backgroundColor: 'rgba(128,128,128,.75)'
                }
            },
            xAxis: {
                type: 'category',
                axisLabel: {
                    formatter: (val: string) => moment(val).format('YYYY/MM/DD'),
                    margin: 12,
                },
                axisPointer: {
                    label: {
                        formatter: (params) => moment(params.value).format('YYYY/MM/DD HH:mm:ss'),
                    }
                },
                axisTick: {
                    show: false,
                }
            },
            yAxis: {
                scale: true,
                splitLine: {
                    lineStyle: {
                        // 坐标分隔线颜色
                        color: xAxisLineColor,
                    },
                },
            },
        }
    }

    // K线配置项
    const setCandlestickOption = () => {
        const { dimensions, source } = dataset.candlestick;
        const { upColor, downColor } = options.colors;

        options.candlestick = {
            ...getDefaultOption(),
            dataset: {
                dimensions,
                source,
            },
            series: [
                {
                    name: 'K线',
                    type: 'candlestick',
                    itemStyle: {
                        color: upColor,
                        color0: downColor,
                        borderColor: upColor,
                        borderColor0: downColor,
                    },
                    // 最新价标线
                    markLine: {
                        symbolSize: 6,
                        // 标线标签样式
                        label: {
                            color: '#fff',
                            backgroundColor: 'rgba(128,128,128,.75)',
                            padding: 5,
                            borderRadius: 3,
                            position: 'insideStartTop',
                        },
                        // 标线样式
                        lineStyle: {
                            type: 'dashed',
                            color: 'rgba(102,102,102,.6)'
                        },
                        data: [
                            {
                                // 收盘价
                                yAxis: source.length ? source[source.length - 1].close : 0,
                            },
                        ],
                    },
                },
                {
                    name: 'MA5',
                    type: 'line',
                    sampling: 'average', //折线图在数据量远大于像素点时候的降采样策略，开启后可以有效的优化图表的绘制效率
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
                {
                    name: 'MA10',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
                {
                    name: 'MA15',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
            ],
        }
    }

    // MACD配置项
    const setMacdOption = () => {
        const { upColor, downColor } = options.colors;
        const { dimensions, source } = dataset.macd;
        options.macd = {
            ...getDefaultOption(),
            dataset: {
                dimensions,
                source,
            },
            series: [
                {
                    name: 'MACD',
                    type: 'bar',
                    sampling: 'average',
                    barWidth: '20%',
                    itemStyle: {
                        color: ({ data }) => {
                            const { macd } = data as { macd: number };
                            if (macd > 0) {
                                return upColor;
                            } else {
                                return downColor;
                            }
                        },
                    }
                },
                {
                    name: 'DIF',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
                {
                    name: 'DEA',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
            ],
        }
    }

    // VOL配置项
    const setVolOption = () => {
        const { dimensions, source } = dataset.vol;
        options.vol = {
            ...getDefaultOption(),
            dataset: {
                dimensions,
                source,
            },
            series: [
                {
                    name: 'VOL',
                    type: 'bar',
                    sampling: 'average',
                    barWidth: '60%',
                },
            ],
        }
    }

    // KDJ配置项
    const setKdjOption = () => {
        const { dimensions, source } = dataset.kdj;
        options.kdj = {
            ...getDefaultOption(),
            dataset: {
                dimensions,
                source,
            },
            series: [
                {
                    name: 'K',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
                {
                    name: 'D',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
                {
                    name: 'J',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
            ],
        }
    }

    // CCI配置项
    const setCciOption = () => {
        const { dimensions, source } = dataset.cci;
        options.cci = {
            ...getDefaultOption(),
            dataset: {
                dimensions,
                source,
            },
            series: [
                {
                    name: 'CCI',
                    type: 'line',
                    sampling: 'average',
                    smooth: true,
                    symbol: 'none',
                    lineStyle: {
                        width: 1,
                        opacity: 0.8,
                    },
                },
            ],
        }
    }

    const initOptions = () => {
        setCandlestickOption();
        setMacdOption();
        setVolOption();
        setKdjOption();
        setCciOption();
    }

    // 动态更新数据
    const updateOptions = timerInterceptor.setThrottle(() => {
        const { candlestick, macd, vol, kdj, cci } = dataset;

        options.candlestick = {
            dataset: {
                source: candlestick.source,
            },
            series: [
                {
                    name: 'K线',
                    markLine: {
                        data: [
                            {
                                yAxis: candlestick.source[candlestick.source.length - 1].close,
                            },
                        ],
                    },
                },
            ],
        }
        options.macd = {
            dataset: {
                source: macd.source,
            },
        }
        options.vol = {
            dataset: {
                source: vol.source,
            },
        }
        options.kdj = {
            dataset: {
                source: kdj.source,
            },
        }
        options.cci = {
            dataset: {
                source: cci.source,
            },
        }
    }, 1000)

    // 监听主题变化
    watch(appTheme, () => {
        options.colors = getColors();
        initOptions();
    })

    return {
        options,
        initOptions,
        updateOptions,
    }
}