/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 RadarChart extends EChart {
    private data: Array<DataEntry> = new Array<DataEntry>();
    private indicators: Array<Indicator> = new Array<Indicator>();
    private title: string = "unset";
    private subTitle: string = "";

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

    setIndicators(args: { indicators: Array<Indicator> }) {
        this.indicators = args.indicators;
    }

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


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

    redrawChart() {
        /*-- 组装indicators多个数据轴的逻辑 start --*/
        let indicatorsContent: Array<any> = new Array<any>();
        for (let indicator of this.indicators) {
            let template: any;
            if (indicator.max) {
                template = {
                    name: indicator.name, max: indicator.max
                };
            } else {
                template = {
                    name: indicator.name
                };

            }
            indicatorsContent.push(template);
        }
        /*-- 组装indicators多个数据轴的逻辑 end --*/

        /*-- 根据data设置legend以及series的逻辑 start --*/
        let legendData: Array<string> = new Array<string>();
        let dataContent: Array<any> = [];
        for (let dataEntry of this.data) {
            let subDataTemplate: any = {
                value: dataEntry.value,
                name: dataEntry.name
            };
            legendData.push(dataEntry.name);
            dataContent.push(subDataTemplate);
        }
        /*-- 根据data设置legend以及series的逻辑 end --*/


        let radarOption = {
            title: {
                text: this.title,
                subtext: this.subTitle,
                x: "center"
            },
            tooltip: {},
            legend: {
                data: legendData,
                x: "left",
                orient: "vertical"
            },
            radar: {
                // shape: "circle",
                radius: "70%",
                center: ["50%", "50%"],
                indicator: indicatorsContent
            },
            series: [{
                name: "",
                type: "radar",
                areaStyle: { normal: {} },
                data: dataContent
            }]
        };

        this.option = radarOption;
        if (this.echart) {
            this.echart.setOption(this.option);
            this.echart.resize();
        }
    }
}

interface DataEntry {
    name: string;
    value: Array<number>;
}

interface Indicator {
    name: string;
    max?: number;
}
