/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
import {Composite} from "widget/Composite";
import {Widget} from "widget/Widget";
import {EChart} from "widget/echart/EChart";

export class BarChart extends EChart {
    //抽象数据列表
    private data: Array<DataEntry> = new Array<DataEntry>();
    private title: string = "unset";
    private subTitle: string = "";
    //默认为横向坐标轴保存category
    private align: "vertical" | "horizontal" = "horizontal";
    private axisLabel: { categoryAxisLabel: string, dataAxisLabel: string } = { categoryAxisLabel: "", dataAxisLabel: "" };
    private categories: Array<string> = [];

    setTitle(args: { titles: { title: string, subTitle: string } }) {
        this.title = args.titles.title;
        this.subTitle = args.titles.subTitle;
    }

    setData(args: { data: Array<DataEntry> }) {
        this.data = args.data;
    }

    setAxisLabel(args: { axisLabelInfo: { categoryAxisLabel: string, dataAxisLabel: string } }) {
        this.axisLabel = { categoryAxisLabel: args.axisLabelInfo.categoryAxisLabel, dataAxisLabel: args.axisLabelInfo.dataAxisLabel };
    }

    setAlign(args: { align: "vertical" | "horizontal" }) {
        this.align = args.align;
    }

    setCategories(args: { categories: Array<string> }) {
        this.categories = args.categories;
    }

    postLayout() {
        if (this.data) {
            this.redrawChart();
        }
        super.postLayout();
    }

    redrawChart() {
        /*-- 设置坐标轴属性的逻辑:label,categories,align start --*/
        let xAxisContent = {};
        let yAxisContent = {};
        //x轴作为category轴
        if (this.align === "horizontal") {
            xAxisContent = {
                data: this.categories,
                name: this.axisLabel.categoryAxisLabel,
                silent: false,
                splitLine: {
                    show: false
                }
            };
            yAxisContent = {
                name: this.axisLabel.dataAxisLabel
            };
        }
        //y轴作为category轴
        else if (this.align === "vertical") {
            yAxisContent = {
                data: this.categories,
                name: this.axisLabel.categoryAxisLabel,
                silent: false,
                splitLine: {
                    show: false
                }
            };
            xAxisContent = {
                name: this.axisLabel.dataAxisLabel
            };
        };
        /*-- 设置坐标轴属性的逻辑:label,categories,align end --*/

        /*-- 设置数据总名称的逻辑 start --*/
        let dataName: Array<string> = [];
        for (let dataEntry of this.data) {
            dataName.push(dataEntry.entryName);
        }
        /*-- 设置数据总名称的逻辑 end --*/

        /*-- 设置数据的逻辑 start --*/
        let seriesContent: Array<any> = [];

        let i = 0;
        for (let dataEntry of this.data) {
            let dataTemplate = {
                "name": dataEntry.entryName,
                "type": "bar",
                "data": dataEntry.entryData
                // 暂时不添加动画，对resize有影响
                /*,
                "animationDelay": function (idx: number) {
                    return idx * 10 + 100 * i;
                }*/
            };
            seriesContent.push(dataTemplate);
            i++;
        }
        /*-- 设置数据的逻辑 end --*/
        let barOption = {
            title: {
                text: this.title,
                subtext: this.subTitle,
                left: "center"
            },
            legend: {
                data: dataName,
                align: "left",
                left: "left"
            },
            //设置能够在不同数据重叠形式之间切换的按钮
            toolbox: {
                feature: {
                    magicType: {
                        type: ["stack", "tiled"]
                    }
                }
            },
            tooltip: {},
            xAxis: xAxisContent,
            yAxis: yAxisContent,
            series: seriesContent
            // 暂时不添加动画，对resize有影响
            /*,
            animationEasing: "elasticOut",
            animationDelayUpdate: function (idx: number) {
                return idx * 5;
            }*/
        };
        this.option = barOption;
        //如果已经进行了layout则直接进行写入
        if (this.echart) {
            this.echart.setOption(this.option);
            this.echart.resize();
        }
    }
}

interface DataEntry {
    entryName: string;
    entryData: [{ dataValue: number }];
}