/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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.
 */
/**
 * Created by huangdeyun on 2015/8/27 16:31.
 */
define(["adoreapp/widget/BaseWidget",
        "adoreapp/widget/EventHub",
        "adoreapp/widget/echarts/Radar_",
        "adoreapp/widget/echarts/Pie_",
        "adoreapp/widget/echarts/Bar_",
        "adoreapp/widget/echarts/BranchRadar",
        "adoreapp/widget/constants/ADORE",
        "adoreapp/widget/echarts/TYPE",
        "adoreapp/util/Map",
        "adoreapp/util/VisibilityChangeUtil"],
    function (BaseWidget, EventHub, Radar_, Pie_, Bar_, BranchRadar, Adore, EChartsType, Map, VisibilityChangeUtil) {

        return BaseWidget.extend(function (base) {
            return {
                _initWidget: function (attrs) {
                    this.jqueryNode = $(attrs.template).attr("id", this.id);
                    this.isLayout = false;
                    var own = this;
                    this.hadVisibilityListener = false;
                    $(window).bind("resize." + this.id, function (e) {
                        if ($("#" + own.id).length === 0) {
                            $(window).unbind("resize." + own.id);
                            return;
                        }
                        if (own.jqueryNode.is(":visible")) {
                            //可见状态下直接刷新
                            if (own.myChart) {
                                own.myChart.resize();
                            }
                        } else {
                            //不可见状态下添加visibleListener，可见时再刷新
                            if (!own.hadVisibilityListener) {
                                own.hadVisibilityListener = true;
                                VisibilityChangeUtil.addVisibilityListener(own.jqueryNode, function (visible) {
                                    if (visible) {
                                        if (own.myChart) {
                                            own.myChart.resize();
                                        }
                                        VisibilityChangeUtil.removeVisibilityListener(own.jqueryNode);
                                        own.hadVisibilityListener = false;
                                    }
                                });
                            }
                        }
                    });
                    this.resetAttrs();
                },

                _layout: function () {
                    base._layout.call(this);
                    this.isLayout = true;
                    this.handleSetAttrs();
                },

                resetAttrs: function () {
                    /**
                     * 各图表公共属性
                     * */
                    this.title = "";
                    this.subTitle = "";
                    this.names = [];//一维数组
                    this.values = [];//pie:一维数组。bar、radar:二维数组
                    /**
                     * bar
                     * */
                    this.barXAxis = [];//X轴坐标,一维数组
                    /**
                     * radar
                     */
                    this.radarPolarIndicator = [];//雷达极坐标指示器名,一维数组
                    this.radarPolarIndicatorMax = -1;//雷达极坐标指示器最大值
                    /**
                     * branchRadar
                     */
                    this.branchRadarOthers = new Map();
                },

                _setTheme: function (attrs) {
                    var themeName = attrs.themeName;
                    if (themeName) {
                        var own = this;
                        window.location.hash = themeName;
                        require(['echarts/theme/' + themeName], function (curTheme) {
                            setTimeout(function () {
                                own.myChart.setTheme(curTheme);
                            }, 500);
                        });
                    }
                },

                _setTitle: function (attrs) {
                    this.title = attrs.title !== null ? attrs.title : "";
                    this.subTitle = attrs.subTitle !== null ? attrs.subTitle : "";
                    this.handleSetAttrs();
                },

                _clear: function () {
                    this.resetAttrs();
                    if (this.myChart) {
                        this.myChart.clear();
                    }
                },

                /**
                 * bar提供的接口:
                 *   setBarXAxis(String[] barXAxis)
                 *   addBarData(String name,String[] values)
                 */
                _setBarXAxis: function (attrs) {
                    if (attrs.barXAxis) {
                        this.barXAxis = attrs.barXAxis;
                        this.setEChartsType(EChartsType.BAR);
                    }
                },
                _addBarData: function (attrs) {
                    if (attrs.name && attrs.values) {
                        this.names.push(attrs.name);
                        this.values.push(attrs.values);
                        this.setEChartsType(EChartsType.BAR);
                    }
                },

                /**
                 * pie提供的接口：
                 *   setPieNames(String[] pieNames)
                 *   setPieValues(String[] pieValues)
                 * */
                _setPieNames: function (attrs) {
                    if (attrs.pieNames) {
                        this.names = attrs.pieNames;
                        this.setEChartsType(EChartsType.PIE);
                    }
                },
                _setPieValues: function (attrs) {
                    if (attrs.pieValues) {
                        this.values = attrs.pieValues;
                        this.setEChartsType(EChartsType.PIE);
                    }
                },

                /**
                 * radar提供的接口：
                 *   setRadarPolarIndicator(String[] radarPolarIndicator)
                 *   setRadarPolarIndicatorMax(int radarPolarIndicatorMax)
                 *   addRadarData(String name,String[] values)
                 * */
                _setRadarPolarIndicator: function (attrs) {
                    if (attrs.radarPolarIndicator) {
                        this.radarPolarIndicator = attrs.radarPolarIndicator;
                        this.setEChartsType(EChartsType.RADAR);
                    }
                },
                _setRadarPolarIndicatorMax: function (attrs) {
                    if (attrs.radarPolarIndicatorMax !== null) {
                        this.radarPolarIndicatorMax = attrs.radarPolarIndicatorMax;
                        this.setEChartsType(EChartsType.RADAR);
                    }
                },
                _addRadarData: function (attrs) {
                    if (attrs.name && attrs.values) {
                        this.names.push(attrs.name);
                        this.values.push(attrs.values);
                        this.setEChartsType(EChartsType.RADAR);
                    }
                },

                /*
                 * branchRadar提供的接口
                 *   addBranchRadarData(String name,int distance,String walkingTime,String description)
                 *   setBranchRadarData(String name,int windowNum,int waitInLineNum,String others)
                 */
                _addBranchRadarData: function (attrs) {
                    if (attrs.name) {
                        this.names.push(attrs.name);
                        this.values.push(attrs.distance);
                        var other = this.branchRadarOthers.getValue(attrs.name);
                        if (other) {
                            other.walkingTime = attrs.walkingTime;
                            other.description = attrs.description;
                        } else {
                            other = {
                                walkingTime: attrs.walkingTime,
                                description: attrs.description
                            };
                            this.branchRadarOthers.put(attrs.name, other);
                        }
                        this.setEChartsType(EChartsType.BRANCH_RADAR);
                    }
                },
                _setBranchRadarData: function (attrs) {//String name,int windowNum,int waitInLineNum,String otherDesc
                    if (attrs.name) {
                        var other = this.branchRadarOthers.getValue(attrs.name);
                        if (other) {
                            other.windowNum = attrs.windowNum;
                            other.waitInLineNum = attrs.waitInLineNum;
                            other.otherDesc = attrs.otherDesc;
                        } else {
                            other = {
                                windowNum: attrs.windowNum,
                                waitInLineNum: attrs.waitInLineNum,
                                otherDesc: attrs.otherDesc
                            };
                            this.branchRadarOthers.put(attrs.name, other);
                        }
                        this.setEChartsType(EChartsType.BRANCH_RADAR);
                    }
                },

                setEChartsType: function (type) {
                    this.type = type;
                    this.handleSetAttrs();
                },

                handleSetAttrs: function () {
                    if (this.isLayout) {
                        switch (this.type) {
                            case EChartsType.BAR:
                                this.myChart = Bar_.init(this.myChart, this.jqueryNode[0], this.title, this.subTitle, this.barXAxis, this.names, this.values);
                                break;
                            case EChartsType.PIE:
                                this.myChart = Pie_.init(this.myChart, this.jqueryNode[0], this.title, this.subTitle, this.names, this.values);
                                break;
                            case EChartsType.RADAR:
                                this.myChart = Radar_.init(this.myChart, this.jqueryNode[0], this.title, this.subTitle, this.radarPolarIndicator, this.radarPolarIndicatorMax, this.names, this.values);
                                break;
                            case EChartsType.BRANCH_RADAR:
                                var others = [];
                                for (var i = 0; i < this.names.length; i++) {
                                    others.push(this.branchRadarOthers.getValue(this.names[i]));
                                }
                                this.myChart = BranchRadar.init(this.myChart, this.jqueryNode[0], this.title, this.subTitle, this.names, this.values, others);
                                break;
                        }
                    }
                },

                _dispose: function () {
                    $(window).unbind("resize." + this.id);
                    if (this.myChart) {
                        this.myChart.dispose();
                    }
                    base._dispose.call(this);
                }
            };
        });
    });
