
import * as echarts from "echarts";
import { chartsWidth,emptyData, handelZoom,dataLength, handelProfitrate, handelLineType, handelSwitchs, handelSmaData, getDateArray, handelWatermark, fetchBtcPrice, fetchEthPrice } from "../../util/processingData"
export default {
    data() {
        return {
            lengthTimer:null,
            ETHvolumeLoading: false,
            option: {
                legend: {
                    right: 100,
                    formatter: function (name) {
                        return name;
                    },
                },
                tooltip: {
                    trigger: "axis",
                    textStyle: {
                        align: "left",
                    },
                    animation: false,
                    // formatter: function (params) {
                    //     var res = params[0].axisValueLabel;

                    //     for (let i = 0; i <= params.length - 1; i++) {
                    //         res += `</br>${params[i].marker} <span style="font-weight: bold">${params[i].value[1]}</span> ${params[i].seriesName}`;
                    //     }
                    //     return res;
                    // },
                    axisPointer: {
                        animation: false,
                        snap: true,
                        label: {
                            precision: 2, //坐标轴保留的位数
                        },
                        type: "cross", //cross shadow
                        crossStyle: {
                            //十字轴横线
                            // opacity: "0",
                            width: 0.5,
                        },
                        lineStyle: {
                            // opacity: 0,
                        },
                    },
                },

                xAxis: {
                    type: "time",
                    boundaryGap: false,
                    data: [],
                    axisTick: {
                        //去除刻度
                        show: false,
                    },
                    axisLine: {
                        //去除轴线
                        show: false,
                    },
                },
                yAxis: [
                    {
                        position: "left",
                        type: "value",
                        axisLabel: {
                            formatter: function (value) {

                                return `${value / 10000} W`
                            }
                        },
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        position: "right",
                        // type: "log",
                        // splitNumber: "5",
                        show: true,
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        position: "right",
                        // type: "log",
                        // splitNumber: "5",
                        show: false,
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        position: "right",
                        // type: "log",
                        // splitNumber: "5",
                        show: false,
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        position: "right",
                        // type: "log",
                        // splitNumber: "5",
                        show: false,
                        min: "dataMin",
                        max: "dataMax",
                    },
                    {
                        position: "right",
                        // type: "log",
                        // splitNumber: "5",
                        show: false,
                        min: "dataMin",
                        max: "dataMax",
                    },
                ],
                dataZoom: [
                    {
                        type: "inside",
                        start: 10,
                        end: 100,
                        maxSpan: 100,
                        minSpan: 2,
                        animation: false,
                    },
                    {
                        start: 0,
                        end: 20,
                        showDetail: false,
                    },
                ],
                series: [
                    {
                        name: "Transaction Volume",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#f7931a",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#f7931a",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "Transaction Fee",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#e73f32",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#e73f32",
                            width: "1",
                        },
                        yAxisIndex: 1,
                        data: [],
                    },
                    {
                        name: "EIP1559 burnt fees",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#9dd3e8",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#9dd3e8",
                            width: "1",
                        },
                        yAxisIndex: 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: 3,
                    //     data: [],
                    //     //绘制右侧Y轴
                    //     markLine: {
                    //         symbol: ["none", "none"], //去掉箭头
                    //         itemStyle: {
                    //             normal: {
                    //                 lineStyle: { type: "solid", color: "rgba(0,0,0,0.01)" },
                    //             },
                    //         },
                    //         // lineStyle:{
                    //         //   color:"red"
                    //         // },

                    //         label: {
                    //             formatter: "{b}",
                    //             fontSize: 10,
                    //             color: "rgba(0,0,0,0.8)",
                    //             align: "left",
                    //         },
                    //         data: [
                    //             // {
                    //             //     name: "0",
                    //             //     yAxis: 0,
                    //             // },
                    //             //  {
                    //             //   name: "$40W",
                    //             //   yAxis: 400000,
                    //             // },
                    //             {
                    //                 name: "$70W",
                    //                 yAxis: 700000,
                    //             },
                    //             {
                    //                 name: "$40W",
                    //                 yAxis: 400000,
                    //             },

                    //             {
                    //                 name: "$10W",
                    //                 yAxis: 100000,
                    //             },
                    //             {
                    //                 name: "$100W",
                    //                 yAxis: 1000000,
                    //                 // position: ['100%', '50'],
                    //             },
                    //             {
                    //                 name: "$120W",
                    //                 yAxis: 1200000,
                    //             },

                    //             {
                    //                 name: "$140W",
                    //                 yAxis: 1400000,
                    //             },
                    //         ],
                    //     },
                    // },
                    {
                        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: 4,
                        data: [],
                    },


                ],
            },
            params: {
                req: "ETHVolume",
                radius: "eth"
            },
            feesParams: {
                req: "ETHFees",
                radius: "eth"
            },
            volumeData: [],
            feesData: [],
            times: {
                start: "",
                end: ""

            },
            switch3: "",
            btcFlag: null,
            ethFlag: null,
            fatherSmaData: [],
            start: "",
            end: "",
            sma: "",
            zoomStart: 0,
            zoomEnd: 0,
        }
    },
    mounted() {

        this.fetchVolume(this.params.req, this.params.radius)
        this.fetchFees(this.feesParams.req, this.feesParams.radius)
        this.switch3 = handelSwitchs(this.option, this.switch3)

        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: {
        inChart() {
            if (this.myChart == null) {
                this.myChart = echarts.init(document.getElementById("ETHvolumeBox"));
            }
            this.option = handelZoom(this.myChart, this.option, this, this.zoomStart, this.zoomEnd)

            this.myChart.setOption(this.option);
            chartsWidth("ETHvolumeBox", this);
            window.addEventListener("resize", () => {
                if (this.myChart) this.myChart.resize();
            });
        },
        //请求交易量数据渲染
        async fetchVolume(req, radius, start, end, sma) {
            this.ETHvolumeLoading = true
            const { data } = await this.$API.getVolume(req, radius, start, end, sma)
            this.volumeData = data.data
            //空数据处理
            let noData = emptyData(data.data, this.option.series, this.inChart, this)
            if (noData) {
                this.ETHvolumeLoading = false
                return
            }



            this.start = this.volumeData[0].date.split("T")[0]
            this.end = this.volumeData[this.volumeData.length - 1].date.split("T")[0]
            //根据渲染的数据时间渲染对应的价格线和移动平均线
            //   this.$nextTick(() => {
            //     //移动平均线没值 才渲染正常价格数据 
            //     if (this.start ) {                 
            //         this.handelSonEthPrice("ETH", false);                 

            //     }
            //     //点击时间 移动平均线有值 就渲染查询移动平均线
            //     if (this.fatherSmaData[0] && this.start) {
            //         this.handelSonSma(this.fatherSmaData, this.start, this.end)

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

            // })

            // this.handelSonEthPrice("ETH", false); 
            //添加水印
            this.option = handelWatermark(this.option)

            let volumeValueList = []
            let priceList = []
            this.volumeData.forEach(item => {
                let time = item.date.split("T")[0]
                this.option.xAxis.data.push(time)
                volumeValueList.push(Number(item.volume.toFixed(2)))
                priceList.push(Number(item.price.toFixed(2)))

            })
            this.option.series[0].data = handelProfitrate(this.volumeData, volumeValueList)
            this.option.series[3].data = handelProfitrate(this.volumeData, priceList)
            this.inChart()
            this.ETHvolumeLoading = false
        },
        //请求交易费数据渲染
        async fetchFees(req, radius, start, end, sma) {
            const { data } = await this.$API.getVolume(req, radius, start, end, sma)
            this.feesData = data.data

            let feesValueList = []
            let burntsValueList = []

            this.feesData.forEach(item => {
                feesValueList.push(Number(item.fees.toFixed(2)))
                burntsValueList.push(Number(item.burnts.toFixed(2)))

            })
            this.option.series[1].data = handelProfitrate(this.feesData, feesValueList)
            this.option.series[2].data = handelProfitrate(this.feesData, burntsValueList)

            this.inChart()

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

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

            }, 10000)


        },
        //父级时间
        handelSonTime(start, end) {
            this.times.start = start;
            this.times.end = end;
            this.fetchVolume(this.params.req, this.params.radius, this.times.start, this.times.end, this.sma)
            this.fetchFees(this.feesParams.req, this.feesParams.radius, this.times.start, this.times.end, this.sma)
            // this.handelSonBtcPrice("BTC",true)
        },
        //点击对数/线性按钮切换
        handelLineSwitch() {
            this.option = handelLineType(this.option)
            this.switch3 = handelSwitchs(this.option, this.switch3)
            this.myChart.setOption(this.option);

        },
        //父级点击BTC价格按钮
        handelSonBtcPrice(radius, flag) {
            //父级BTC默认是true 在ETH页面btcFlag 取反才能正常点击父级就渲染
            // if (this.params.radius == "eth") {
            //     console.log("简历啊");
            //     this.btcFlag = !flag;
            // } else {
            //     this.btcFlag = flag;
            // } 
            this.btcFlag = flag;
            fetchBtcPrice("BTC", this.option.series[3], this.start, this.end, this.inChart, this, this.btcFlag)


        },
        //父级点击ETH价格按钮
        handelSonEthPrice(radius, flag) {

            this.ethFlag = !flag

            fetchEthPrice("ETH", this.option.series[3], this.start, this.end, this.inChart, this, this.ethFlag, true)


        },
        //父级选择Sma
        handelSonSma(smaDataList) {


            this.sma = smaDataList
            this.fetchVolume(this.params.req, this.params.radius, this.times.start, this.times.end, this.sma)
            this.fetchFees(this.feesParams.req, this.feesParams.radius, this.times.start, this.times.end, this.sma)


            this.$addStorageEvent(1, "sma", this.sma);




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

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

}