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 __());
};
/************************************************
PQ rating trend Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PowerQualityRatingTrendGadget = (function (_super) {
            __extends(PowerQualityRatingTrendGadget, _super);
            function PowerQualityRatingTrendGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityRatingTrendRender();
                _super.call(this, gadgetOptions);
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEvents_NoEvents');
            }
            PowerQualityRatingTrendGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityRatingTrendGadget(gadgetOptions);
                return instance;
            };
            PowerQualityRatingTrendGadget.prototype.canExportData = function () {
                // Even though the base class can, this gadget really needs to tweak the data to be usable first.
                return false;
            };
            PowerQualityRatingTrendGadget.TypeId = "e2639fdf-2d6b-4581-a91a-f7e48e2ae359";
            PowerQualityRatingTrendGadget.PercentageIndex = 3;
            return PowerQualityRatingTrendGadget;
        }(Models.ChartingDataGadget));
        Models.PowerQualityRatingTrendGadget = PowerQualityRatingTrendGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityRatingTrendRender = (function (_super) {
            __extends(PowerQualityRatingTrendRender, _super);
            function PowerQualityRatingTrendRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PowerQualityRatingTrendRender.prototype.updateGadget = function (handler) {
                var chartOptions = Dashboards.Views.ChartHelper.GetDefaultChartOptions(this);
                chartOptions.legend.visible = false;
                // Add bottom axis
                // Column types require a category axis, else column get drawn over each other.
                chartOptions.axes = [Dashboards.Views.ChartHelper.GetStandardBottomXAxis(this)];
                var axisY = Dashboards.Views.ChartHelper.GetStandardLeftYAxis(this);
                axisY.majorGridLines.interval = 1;
                axisY.majorTickMarks.interval = 1;
                axisY.minimum = 0;
                axisY.maximum = 6.5;
                chartOptions.axes.push(axisY);
                // Bind Series
                var chartSeries = [];
                var dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                var series = { title: i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityRating') };
                var jqSeries = Dashboards.Views.ChartHelper.SetupChartSeries(this, series, 'column', 'rgba(159, 160, 164, 0.4)');
                // The rating percentage is returned in the data series called Secondary. This is extra data 
                // for the primary series, so this copies the percentage value from the Secondary to the Primary 
                // series.
                if (dataPoints.Primary !== undefined) {
                    for (var i = 0; i < dataPoints.Primary.length; i++) {
                        dataPoints.Primary[i][Dashboards.Models.PowerQualityRatingTrendGadget.PercentageIndex] = dataPoints.Secondary[i][Dashboards.Models.ChartingDataGadget.ValueIdx];
                    }
                }
                jqSeries.data = dataPoints.Primary; // The PQ Rating engine returns the main data as a series called "Primary" 
                jqSeries.axisY = 'y1';
                chartSeries.push(jqSeries);
                chartOptions.series = chartSeries;
                var existingSeries = this.$getChart().jqChart('option', 'series');
                Dashboards.Views.ChartHelper.UpdateSeriesVisibility(chartSeries, existingSeries);
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                this.bindChartAxisLabelFormat();
                this.bindShapeRenderingEvent();
                this.$getChart().jqChart(chartOptions);
                this.bindTimeValueTooltipFormat();
            };
            /* Private Methods
            ******************************************************************************/
            PowerQualityRatingTrendRender.prototype.bindChartAxisLabelFormat = function () {
                this.$gadgetRender.bind('axisLabelCreating', function (e, data) {
                    if (data.context.axis.location === 'left') {
                        data.text = PowerQualityRatingTrendRender.RatingIndexToLetterGrade(data.text);
                    }
                });
            };
            PowerQualityRatingTrendRender.prototype.bindShapeRenderingEvent = function () {
                this.$gadgetRender.bind('shapeRendering', function (e, shape) {
                    var color = PowerQualityRatingTrendRender.GetLetterGradeColor(shape.context.y);
                    shape.fillStyle = color;
                    shape.strokeStyle = color;
                });
            };
            PowerQualityRatingTrendRender.prototype.bindTimeValueTooltipFormat = function () {
                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 (e, data) {
                    var fontSize = '10pt';
                    var letterGrade = PowerQualityRatingTrendRender.RatingIndexToLetterGrade(data.dataItem[Dashboards.Models.ChartingDataGadget.ValueIdx]);
                    var formattedPercentage = i18n.formatPercent(data.dataItem[Dashboards.Models.PowerQualityRatingTrendGadget.PercentageIndex], 0);
                    var timestamp = data.dataItem[Dashboards.Models.ChartingDataGadget.AxisLabelIdx];
                    var timeLabel = (_dateFormatString.toLowerCase().indexOf('h') > -1)
                        ? i18n.formatDate(data.dataItem[Dashboards.Models.ChartingDataGadget.TimeIdx], i18n.dateFormat.shortDate) + ' ' + data.dataItem[Dashboards.Models.ChartingDataGadget.AxisLabelIdx]
                        : i18n.formatDate(data.dataItem[Dashboards.Models.ChartingDataGadget.TimeIdx], _dateFormatString);
                    var tooltip = '<div class="chartTooltip" style="font-size:' + fontSize + ';">';
                    tooltip += '<div class="clear bold">' + letterGrade + ' (' + formattedPercentage + ')</div>';
                    tooltip += '<div class="clear">' + timeLabel + '</div>';
                    tooltip += '</div>';
                    return tooltip;
                });
            };
            // For the given rating index, return the associated letter grade, or an
            // empty string if no match is found.
            PowerQualityRatingTrendRender.RatingIndexToLetterGrade = function (ratingIndex) {
                var i = parseFloat(ratingIndex); // parseFloat, because parseInt will round any partial values (e.g., 1.5) to an integer.
                switch (i) {
                    case 1:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'F');
                    case 2:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'E');
                    case 3:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'D');
                    case 4:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'C');
                    case 5:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'B');
                    case 6:
                        return i18n.loc.unencoded('PowerQualityGadgets', 'A');
                    default:
                        return '';
                }
            };
            PowerQualityRatingTrendRender.GetLetterGradeColor = function (ratingIndex) {
                switch (ratingIndex) {
                    case 6:
                        return "#6bde00";
                    case 5:
                        return "#a8de00";
                    case 4:
                        return "#efdd00";
                    case 3:
                        return "#ffba01";
                    case 2:
                        return "#ff8c03";
                    case 1:
                        return "#ff3908";
                }
                return '#9FA0A4';
            };
            return PowerQualityRatingTrendRender;
        }(Views.ViewPeriodChartRender));
        Views.PowerQualityRatingTrendRender = PowerQualityRatingTrendRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityRatingTrendGadget.TypeId,
    create: Dashboards.Models.PowerQualityRatingTrendGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityRatingTrendGadget.js.map