/************************************************
PQ Events Breakdown Gadget
************************************************/
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PowerQualityEventsBreakdownGadget = (function (_super) {
            __extends(PowerQualityEventsBreakdownGadget, _super);
            function PowerQualityEventsBreakdownGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityEventsBreakdownRender();
                _super.call(this, gadgetOptions);
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEvents_NoEvents');
            }
            PowerQualityEventsBreakdownGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityEventsBreakdownGadget(gadgetOptions);
                return instance;
            };
            PowerQualityEventsBreakdownGadget.prototype.canExportData = function () {
                // Even though the base class can, this gadget really needs to tweak the data to be usable first.
                return false;
            };
            PowerQualityEventsBreakdownGadget.prototype.hasData = function () {
                var _this = this;
                var superHasData = _super.prototype.hasData.call(this);
                // Looking for all zeroes
                if (superHasData) {
                    superHasData = _.any(Object.keys(this.dataSet.ChartingData.DataPoints), function (key) {
                        var points = _this.dataSet.ChartingData.DataPoints[key];
                        return points.length == 1 && points[0][Models.ChartingDataGadget.ValueIdx];
                    });
                }
                return superHasData;
            };
            PowerQualityEventsBreakdownGadget.TypeId = "4f265af4-afd6-4cdd-aa6d-230e3a2a656f";
            return PowerQualityEventsBreakdownGadget;
        }(Models.ChartingDataGadget));
        Models.PowerQualityEventsBreakdownGadget = PowerQualityEventsBreakdownGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityEventsBreakdownRender = (function (_super) {
            __extends(PowerQualityEventsBreakdownRender, _super);
            function PowerQualityEventsBreakdownRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PowerQualityEventsBreakdownRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
            };
            PowerQualityEventsBreakdownRender.prototype.updateGadget = function (handler) {
                var chartOptions = Dashboards.Views.ChartHelper.GetDefaultChartOptions(this);
                var fillStyleColors = [];
                var seriesData = [];
                var legendData = [];
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                var dataPointNames = Object.getOwnPropertyNames(dataPoints);
                _.each(dataPointNames, function (eventType) {
                    var dataPoint = dataPoints[eventType][0];
                    // Localize the EventType, and add back into Data Point for tooltips use correct value.
                    var localizedEventType = i18n.loc.unencoded('PowerQualityGadgets', eventType);
                    dataPoint[Dashboards.Models.ChartingDataGadget.AxisLabelIdx] = localizedEventType;
                    // Add the data point
                    seriesData.push(dataPoint);
                    // Set the color, based on custom rules for the Event Type
                    var color = Dashboards.PowerQuality.PowerQualityHelper.GetEventTypeColor(Dashboards.PowerQuality.EventTypeColor[eventType]) || Dashboards.PowerQuality.PowerQualityHelper.GetEventTypeColor(Dashboards.PowerQuality.EventTypeColor.Default);
                    fillStyleColors.push(color);
                    var dataPointValue = i18n.formatNumber(dataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx], 0);
                    legendData.push({
                        'text': {
                            'text': StringUtil.format('{0} ({1})', localizedEventType, dataPointValue)
                        },
                        'marker': {
                            type: 'circle',
                            fillStyle: color
                        }
                    });
                });
                // Set Legend
                chartOptions.legend.customItems = legendData;
                // Set series
                var options = {
                    data: seriesData,
                    fillStyles: fillStyleColors,
                    strokeStyles: fillStyleColors
                };
                if (this.renderSize > 1) {
                    options = {
                        data: seriesData,
                        fillStyles: fillStyleColors,
                        strokeStyles: fillStyleColors,
                        labels: {
                            valueType: 'percentage',
                            font: Views.ChartHelper.GetAxisLabelFontSize(this.renderSize, false) + ' Arial',
                            fillStyle: '#9fa0a4'
                        },
                        labelsPosition: 'outside',
                        labelsAlign: 'circle',
                        labelsExtend: 20,
                        leaderLineWidth: 1,
                        leaderLineStrokeStyle: '#9fa0a4'
                    };
                }
                else {
                    options = {
                        data: seriesData,
                        fillStyles: fillStyleColors,
                        strokeStyles: fillStyleColors
                    };
                }
                var chartSeries = Views.ChartHelper.GetPieChartSeriesOptions(options);
                chartOptions.series = chartSeries;
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                Views.ChartHelper.BindPieChartLabelFormat(this);
                this.$getChart().jqChart(chartOptions);
                // Bind custom tooltip format show hours and costs.
                this.bindTooltipFormat();
            };
            PowerQualityEventsBreakdownRender.prototype.bindTooltipFormat = function () {
                var _this = this;
                var handler = this.getHandler();
                var _viewPeriodKey = this.gadget.getViewPeriodId(handler);
                var _viewPeriodGroup = this.gadget.getViewPeriodGroup(handler);
                var _aggregationKey = this.gadget.getViewPeriodAggregationId(handler);
                var _dateFormatString = Settings.ViewingPeriod.GetSimpleDateFormatStringForViewingPeriod(_viewPeriodGroup.Name, _viewPeriodKey, _aggregationKey);
                this.$gadgetRender.bind('tooltipFormat', function (eventObject, data) {
                    // Is the gadget gone?
                    if (!_this.gadget || !_this.gadget.dataSet)
                        return;
                    var dataItem = data.dataItem[Dashboards.Models.ChartingDataGadget.AxisLabelIdx];
                    var fontSize = Views.ChartHelper.GetToolTipFontSize(_this.renderSize);
                    var tooltip = '<div class="chartTooltip" style="font-size:' + fontSize + '"><div class="clear"></div>';
                    tooltip += dataItem.encodeHtml();
                    tooltip += '<div class="clear"></div>';
                    tooltip += i18n.formatPercent(data.percentage, 1);
                    tooltip += '<div class="clear"></div>';
                    tooltip += StringUtil.format('{0}: {1}', i18n.loc.s('PowerQualityGadgets', 'Incidents'), i18n.formatNumber(data.value, 0));
                    return tooltip;
                });
            };
            PowerQualityEventsBreakdownRender.prototype.getColorForEventType = function (eventType) {
                switch (eventType) {
                    case "Undervoltage":
                        return '#5785af';
                    case "Overvoltage":
                        return '#b05171';
                    case "Swell":
                        return '#d5a8bc';
                    case "Transient":
                        return '#f1e0c2';
                    case "Sag":
                        return '#c4d6e4';
                    case "Interruption":
                    default:
                        return '#333333';
                }
            };
            return PowerQualityEventsBreakdownRender;
        }(Views.ViewPeriodChartRender));
        Views.PowerQualityEventsBreakdownRender = PowerQualityEventsBreakdownRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityEventsBreakdownGadget.TypeId,
    create: Dashboards.Models.PowerQualityEventsBreakdownGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityEventsBreakdown.js.map