import * as echarts from "echarts";
import { takegoldenRatioMultEth } from "../../api/pages"
import { chartsWidth,dataLength,  emptyData,handelProfitrate, handelLineType, handelZoom, handelWatermark, handelSwitchs, fetchBtcPrice, fetchEthPrice, handelSmaData, getDateArray } from "../../util/processingData"

export default {
    data() {
        return {
            lengthTimer:null,
            goldenRatioMultEthLoading: false,
            option: {
                tooltip: {
                    trigger: "axis",
                    //解决tooltip显示不全问题1 
                    confine: true,
                },
                legend: {
                    right: "100",
                    //处理legengd过长
                    // formatter: function (name) {

                    //     return echarts.format.truncateText(name, 100, '14px Microsoft Yahei', '…');
                    // },
                    tooltip: {
                        show: true
                    }
                },
                xAxis: {
                    type: "time",
                    boundaryGap: false,
                    //   axisLabel: {
                    //     formatter: `{yyyy}-{MM}-{dd}`,
                    //   },
                    data: []
                },
                yAxis: [
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        axisLabel: {
                            formatter: function (e) {

                                let value;
                                if (e > 100000000) {
                                    value = `${e / 100000000}E`;
                                } else if (e > 1000000) {//百万
                                    value = `${e / 1000000}M`;
                                } else if (e >= 10000) {//万
                                    value = `${e / 10000}W`;
                                } else if (e < -1000000) {//百万
                                    value = `${e / 1000000}M`;
                                } else if (e < -10000) {//万
                                    value = `${e / 10000}W`;
                                }
                                else {
                                    value = e;
                                }
                                return value;
                            },
                        },
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        show: true,
                        min: "dataMin",
                        max: "dataMax",
                        position: "right",
                        axisLabel: {
                            formatter: function (e) {

                                let value;
                                if (e > 100000000) {
                                    value = `${e / 100000000}E`;
                                } else if (e > 1000000) {//百万
                                    value = `${e / 1000000}M`;
                                } else if (e >= 10000) {//万
                                    value = `${e / 10000}W`;
                                } else if (e < -1000000) {//百万
                                    value = `${e / 1000000}M`;
                                } else if (e < -10000) {//万
                                    value = `${e / 10000}W`;
                                }
                                else {
                                    value = e;
                                }
                                return value;
                            },
                        },
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        min: "dataMin",
                        max: "dataMax",
                        show: false,
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        min: "dataMin",
                        max: "dataMax",
                        show: false,
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        min: "dataMin",
                        max: "dataMax",
                        show: false,
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        min: "dataMin",
                        max: "dataMax",
                        show: false,
                    },
                    {
                        type: "value",
                        // boundaryGap: [0, "100%"],
                        min: "dataMin",
                        max: "dataMax",
                        show: false,
                    },
                ],
                dataZoom: [
                    {
                        type: "inside",
                        start: 0,
                        end: 100,
                    },
                    {
                        start: 0,
                        end: 100,
                    },
                ],
                series: [
                    {
                        name: "350DMA",
                        type: "line",
                        smooth: false,
                        symbol: "circle",
                        showSymbol: false,
                        symbolSize: 5,
                        // areaStyle: {},
                        itemStyle: {
                            color: "#f7864c",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        yAxisIndex: 0,
                        lineStyle: {
                            //线条样式
                            color: "#f7864c",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "1.6 Accumulation High",
                        type: "line",
                        smooth: false,
                        symbol: "circle",
                        showSymbol: false,
                        symbolSize: 5,
                        // areaStyle: {},
                        itemStyle: {
                            color: "#2ca02c",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        yAxisIndex: 0,
                        lineStyle: {
                            //线条样式
                            color: "#2ca02c",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "2 Low Bull High",
                        type: "line",
                        smooth: false,
                        symbol: "circle",
                        showSymbol: false,
                        symbolSize: 5,
                        // areaStyle: {},
                        itemStyle: {
                            color: "#d62728",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        yAxisIndex: 0,
                        lineStyle: {
                            //线条样式
                            color: "#d62728",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "3 Upper Bull High",
                        type: "line",
                        smooth: false,
                        symbol: "circle",
                        showSymbol: false,
                        symbolSize: 5,
                        // areaStyle: {},
                        itemStyle: {
                            color: "#9467bd",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        yAxisIndex: 0,
                        lineStyle: {
                            //线条样式
                            color: "#9467bd",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "350DMA x5",
                        type: "line",
                        smooth: false,
                        symbol: "circle",
                        showSymbol: false,
                        symbolSize: 5,
                        // areaStyle: {},
                        itemStyle: {
                            color: "#75c1df",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        yAxisIndex: 0,
                        lineStyle: {
                            //线条样式
                            color: "#75c1df",
                            width: "1",
                        },
                        data: [],
                    },

                    // {
                    //     name: "Price[BTC]",
                    //     type: "line",
                    //     smooth: false,
                    //     symbol: "circle",
                    //     symbolSize: 5,
                    //     showSymbol: false,
                    //     // areaStyle: {},
                    //     itemStyle: {
                    //         color: "#888888",
                    //         borderColor: "rgba(221,220,107,0.1)",
                    //         borderWidth: 12,
                    //     },
                    //     lineStyle: {
                    //         //线条样式
                    //         color: "#888888",
                    //         width: "1",
                    //     },
                    //     yAxisIndex: 0,
                    //     data: [],


                    // },
                    {
                        name: "Price [ETH]",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#627eea",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#627eea",
                            width: "1",
                        },
                        yAxisIndex: 0,
                        data: [],
                    },

                ],
            },
            switch3: "",
            oldY: null,
            btcFlag: null,
            ethFlag: null,
            fatherSmaData: [],
            start: "",
            end: "",
            params: {
                start: "",
                end: "",
                sma: ""
            },
            zoomStart: 0,
            zoomEnd: 0,


        }
    },
    mounted() {
        this.fetchGoldenRatioMultEth({})
        //禁用时间框
        this.$addStorageEvent(1, "timeDisable", true);
        this.switch3 = handelSwitchs(this.option, this.switch3)
        this.oldY = this.option.yAxis
        this.ethFlag = JSON.parse(localStorage.getItem("ethFlag"));
        this.btcFlag = JSON.parse(localStorage.getItem("btcFlag"));
        this.$addStorageEvent(1, "zoomStart", this.zoomStart);
        this.$addStorageEvent(1, "zoomEnd", this.zoomEnd);

        window.addEventListener("setItem", () => {
            this.ethFlag = JSON.parse(localStorage.getItem("ethFlag"));
            this.btcFlag = JSON.parse(localStorage.getItem("btcFlag"));
            this.zoomStart = localStorage.getItem("zoomStart")
            this.zoomEnd = localStorage.getItem("zoomEnd")


        })
    },
    methods: {
        //初始化图表
        inCharts() {
            if (this.myChart == null) {
                this.myChart = echarts.init(document.getElementById("goldenRatioMultEthBox"));
            }
            this.option = handelZoom(this.myChart, this.option, this, this.zoomStart, this.zoomEnd)

            this.myChart.setOption(this.option);
            chartsWidth("goldenRatioMultEthBox", this);
            window.addEventListener("resize", () => {
                if (this.myChart) this.myChart.resize();
            });
        },
        async fetchGoldenRatioMultEth(params) {
            this.goldenRatioMultEthLoading = true
            const { data } = await takegoldenRatioMultEth(params)
            //空数据处理
            let noData = emptyData(data.data, this.option.series, this.inCharts, this)
            if (noData) {
                this.goldenRatioMultEthLoading = false
                return
            }


            let goldenRatioMultEthData = data.data
            this.start = goldenRatioMultEthData[0].date.split("T")[0]
            this.end = goldenRatioMultEthData[goldenRatioMultEthData.length - 1].date.split("T")[0]
            //  //添加水印
            this.option = handelWatermark(this.option)

            //根据渲染的数据时间渲染对应的价格线和移动平均线
            //   this.$nextTick(() => {

            //       //渲染ETH
            //       if (this.ethFlag) {
            //         this.$addStorageEvent(1, "ethFlag", JSON.stringify(this.ethFlag));
            //         this.handelSonEthPrice("ETH", this.ethFlag)
            //     }
            //     //渲染BTC价格
            //     if (this.btcFlag) {
            //         this.$addStorageEvent(1, "btcFlag", JSON.stringify(this.btcFlag));
            //         this.handelSonBtcPrice("BTC", this.btcFlag)
            //     }
            // })

            // this.handelSonEthPrice("ETH", true)
            let ma350x1ValueList = []
            let ma3501r6ValueList = []
            let ma350x2ValueList = []
            let ma350x3ValueList = []
            let ma350x5ValueList = []
            let priceValueList = []
            goldenRatioMultEthData.forEach(item => {
                if (item.ma3501r6 == null) {
                    item.ma3501r6 = 0
                }
                ma350x1ValueList.push(Number(item.ma350x1.toFixed(2)))
                ma3501r6ValueList.push(Number(item.ma350x1r6.toFixed(2)))
                ma350x2ValueList.push(Number(item.ma350x2.toFixed(2)))
                ma350x3ValueList.push(Number(item.ma350x3.toFixed(2)))
                ma350x5ValueList.push(Number(item.ma350x5.toFixed(2)))
                priceValueList.push(Number(item.price.toFixed(2)))

            })
            this.option.series[0].data = handelProfitrate(goldenRatioMultEthData, ma350x1ValueList)
            this.option.series[1].data = handelProfitrate(goldenRatioMultEthData, ma3501r6ValueList)
            this.option.series[2].data = handelProfitrate(goldenRatioMultEthData, ma350x2ValueList)
            this.option.series[3].data = handelProfitrate(goldenRatioMultEthData, ma350x3ValueList)
            this.option.series[4].data = handelProfitrate(goldenRatioMultEthData, ma350x5ValueList)
            this.option.series[5].data = handelProfitrate(goldenRatioMultEthData, priceValueList)



            this.inCharts()

            this.goldenRatioMultEthLoading = false

             //判定页面条数是否一致
             if (this.lengthTimer) {
                clearTimeout(this.lengthTimer)
            }
            this.lengthTimer = setTimeout(() => {

                this.$addStorageEvent(1, "lengthFlag", dataLength(this.option, this.option.series[0].data.length), );

            }, 10000)

        },

        //接收Home传的时间handelParmesTime
        handelSonTime(start, end) {
            this.params.start = start;
            this.params.end = end;
            this.fetchGoldenRatioMultEth(this.params)
        },
        //点击对数/线性按钮切换
        handelLineSwitch() {
            this.option = handelLineType(this.option, this.oldY)
            this.switch3 = handelSwitchs(this.option, this.switch3)
            this.myChart.setOption(this.option);

        },
        //父级点击BTC价格按钮
        handelSonBtcPrice(radius, flag) {
            this.btcFlag = flag
            fetchBtcPrice("BTC", this.option.series[5], this.start, this.end, this.inCharts, this, this.btcFlag)

        },
        //父级点击ETH价格按钮
        handelSonEthPrice(radius, flag) {
            this.ethFlag = flag
            fetchEthPrice("ETH", this.option.series[5], this.start, this.end, this.inCharts, this, this.ethFlag)
        },
        //父级选择Sma
        handelSonSma(smaDataList) {


            this.params.sma = smaDataList
            this.fetchGoldenRatioMultEth(this.params)
            this.$addStorageEvent(1, "sma", smaDataList);

            // this.fatherSmaData = smaDataList
            // handelSmaData(smaDataList, this.option.series[5], this.inCharts,this.start, this.end,)

        },
    },
    beforeDestroy() {
        if (this.lengthTimer) {
            clearTimeout(this.lengthTimer)
        }
    }
}