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 PowerQualityImpactTrendGadget = (function (_super) {
            __extends(PowerQualityImpactTrendGadget, _super);
            function PowerQualityImpactTrendGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityImpactTrendRender();
                _super.call(this, gadgetOptions);
            }
            PowerQualityImpactTrendGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityImpactTrendGadget(gadgetOptions);
                return instance;
            };
            PowerQualityImpactTrendGadget.prototype.canExportData = function () {
                // Even though the base class can, this gadget really needs to tweak the data to be usable first.
                return false;
            };
            PowerQualityImpactTrendGadget.prototype.hasData = function () {
                var hasData = _super.prototype.hasData.call(this);
                // the default "no data" message.
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEvents_NoEvents');
                // in a special case, override the "no data" message
                if (this.dataSet && this.dataSet.OtherData === "ReferenceArchitectureNotConfigured") {
                    this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'ReferenceArchitectureNotConfigured');
                    hasData = false;
                }
                if (hasData) {
                    // This gadget is only valid if any of the numbers are non-zero.
                    hasData = _.any(this.dataSet.ChartingData.DataPoints.ExternalImpactDuration, function (dataItem) {
                        return dataItem[Models.ChartingDataGadget.ValueIdx] !== 0;
                    }) ||
                        _.any(this.dataSet.ChartingData.DataPoints.InternalImpactDuration, function (dataItem) {
                            return dataItem[Models.ChartingDataGadget.ValueIdx] !== 0;
                        }) ||
                        _.any(this.dataSet.ChartingData.DataPoints.UnknownImpactDuration, function (dataItem) {
                            return dataItem[Models.ChartingDataGadget.ValueIdx] !== 0;
                        });
                }
                return hasData;
            };
            PowerQualityImpactTrendGadget.TypeId = "6cafaf3b-524f-4374-9cf2-73bde5b200a2";
            PowerQualityImpactTrendGadget.DurationIndex = 4;
            PowerQualityImpactTrendGadget.CountIndex = 1;
            return PowerQualityImpactTrendGadget;
        }(Models.ChartingDataGadget));
        Models.PowerQualityImpactTrendGadget = PowerQualityImpactTrendGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityImpactTrendRender = (function (_super) {
            __extends(PowerQualityImpactTrendRender, _super);
            function PowerQualityImpactTrendRender() {
                _super.call(this);
            }
            /* Render Overrides
            ******************************************************************************/
            PowerQualityImpactTrendRender.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
                this.dataPoints = null;
            };
            PowerQualityImpactTrendRender.prototype.updateGadget = function (handler) {
                var _this = this;
                var chartOptions = Views.ChartHelper.GetDefaultChartOptions(this);
                this.dataPoints = $.extend(true, {}, this.gadget.dataSet.ChartingData.DataPoints); // Deep copy of data
                chartOptions.axes = [];
                var bottomAxis = Views.ChartHelper.GetStandardBottomXAxis(this);
                bottomAxis.categories = _.map(this.dataPoints['UnknownImpactCost'], function (dataPoint) {
                    return dataPoint[Dashboards.Models.ChartingDataGadget.AxisLabelIdx];
                }),
                    chartOptions.axes.push(bottomAxis);
                this.leftAxisType = 'stackedColumn';
                // Bind the left axis
                var leftAxis = Views.ChartHelper.GetStandardLeftYAxis(this);
                leftAxis.title = StringUtil.format(i18n.loc.s('PowerQualityGadgets', 'ImpactTrend_Title_LeftAxis'), i18n.currencyFormat.currencySymbol);
                chartOptions.axes.push(leftAxis);
                // Bind Series
                var chartSeries = [];
                // Append the duration (hours) to the cost data point, as we
                // need it in the tooltip.
                $.each(this.dataPoints.InternalImpactCost, function (idx, elem) {
                    _this.dataPoints.InternalImpactCost[idx].push(_this.dataPoints.InternalImpactDuration[idx][Dashboards.Models.ChartingDataGadget.ValueIdx]);
                });
                $.each(this.dataPoints.ExternalImpactCost, function (idx, elem) {
                    _this.dataPoints.ExternalImpactCost[idx].push(_this.dataPoints.ExternalImpactDuration[idx][Dashboards.Models.ChartingDataGadget.ValueIdx]);
                });
                $.each(this.dataPoints.UnknownImpactCost, function (idx, elem) {
                    _this.dataPoints.UnknownImpactCost[idx].push(_this.dataPoints.UnknownImpactDuration[idx][Dashboards.Models.ChartingDataGadget.ValueIdx]);
                });
                var externalCostSeries = {
                    title: i18n.loc.unencoded('PowerQualityGadgets', 'ImpactTrend_Label_ExternalCost'),
                    internalName: 'ExternalImpactCost',
                    type: 'stackedColumn',
                    strokeStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.External),
                    fillStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.External),
                    data: _.map(this.dataPoints['ExternalImpactCost'], function (dataPoint) {
                        return dataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx];
                    }),
                    axisY: 'y1',
                    stackedGroup: 'y1'
                };
                var internalCostSeries = {
                    title: i18n.loc.unencoded('PowerQualityGadgets', 'ImpactTrend_Label_InternalCost'),
                    internalName: 'InternalImpactCost',
                    type: 'stackedColumn',
                    strokeStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Internal),
                    fillStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Internal),
                    data: _.map(this.dataPoints['InternalImpactCost'], function (dataPoint) {
                        return dataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx];
                    }),
                    axisY: 'y1',
                    stackedGroup: 'y1'
                };
                var unknownCostSeries = {
                    title: i18n.loc.unencoded('PowerQualityGadgets', 'ImpactTrend_Label_UnknownCost'),
                    internalName: 'UnknownImpactCost',
                    type: 'stackedColumn',
                    strokeStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Undetermined),
                    fillStyle: Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Undetermined),
                    data: _.map(this.dataPoints['UnknownImpactCost'], function (dataPoint) {
                        return dataPoint[Dashboards.Models.ChartingDataGadget.ValueIdx];
                    }),
                    axisY: 'y1',
                    stackedGroup: 'y1'
                };
                chartOptions.series = [];
                chartOptions.series.push(externalCostSeries);
                chartOptions.series.push(internalCostSeries);
                chartOptions.series.push(unknownCostSeries);
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                // Bind custom axis formating, showing dollars.
                Views.ChartHelper.BindChartAxisLabelFormat(this, ['left']);
                this.$getChart().jqChart(chartOptions);
                // Bind custom tooltip format show hours and costs.
                this.bindTooltipFormat();
                Views.ChartHelper.ShowMarkerForNoDataPlotted(this);
            };
            PowerQualityImpactTrendRender.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;
                    if (!$.isArray(data)) {
                        // In the case where there is only one series, force an array
                        data = [data];
                    }
                    if (typeof data[Dashboards.Models.ChartingDataGadget.AxisLabelIdx].y === 'undefined')
                        return '<div style="display:none"></div>';
                    var fontSize = Views.ChartHelper.GetToolTipFontSize(_this.renderSize);
                    var tooltip = '<div class="chartTooltip" style="font-size:' + fontSize + '"><div class="clear"></div>';
                    var chartData = data[0];
                    var seriesId = chartData.series.internalName;
                    var dataItem = _this.dataPoints[seriesId][chartData.index];
                    var impactCost = i18n.formatCurrency(dataItem[Dashboards.Models.ChartingDataGadget.ValueIdx], 2);
                    // Create an 'empty' date then add the hours in Duration so we can format them correctly
                    var impactDurationInHours = dataItem[Dashboards.Models.PowerQualityImpactTrendGadget.DurationIndex];
                    var impactDuration = DateFormatUtil.secondsToHMS(impactDurationInHours * 60 * 60);
                    var countSeriesName = seriesId + 'Count';
                    var impactCount = i18n.formatNumber(_this.dataPoints[countSeriesName][chartData.index][Dashboards.Models.PowerQualityImpactTrendGadget.CountIndex]);
                    // If the format string has the time, we want to use the 
                    // axis label value since the date of the value is the 
                    // interval time, but we want to output the aggregated hour
                    var timeLabel = (_dateFormatString.toLowerCase().indexOf('h') > -1)
                        ? i18n.formatDate(dataItem[Dashboards.Models.ChartingDataGadget.TimeIdx], i18n.dateFormat.shortDate) + ' ' + dataItem[Dashboards.Models.ChartingDataGadget.AxisLabelIdx]
                        : i18n.formatDate(dataItem[Dashboards.Models.ChartingDataGadget.TimeIdx], _dateFormatString);
                    tooltip += chartData.series.title.encodeHtml();
                    tooltip += '<div class="clear"></div>';
                    tooltip += timeLabel;
                    tooltip += '<div class="clear"></div>';
                    tooltip += impactCost;
                    tooltip += '<div class="clear"></div>';
                    tooltip += StringUtil.format('{0}: {1}', i18n.loc.s('PowerQualityGadgets', 'Incidents'), impactCount);
                    tooltip += '<div class="clear"></div>';
                    tooltip += StringUtil.format(i18n.loc.s('PowerQualityGadgets', 'ImpactTrend_SeriesLabel_Duration'), impactDuration);
                    tooltip += '<div class="clear"></div>';
                    return tooltip;
                });
            };
            PowerQualityImpactTrendRender.prototype.BindAxisLabelFormat = function (axisLocations) {
                this.$gadgetRender.bind('axisLabelCreating', function (eventObject, data) {
                    if (axisLocations.indexOf(data.context.axis.location) >= 0) {
                        data.text = i18n.formatNumber(data.text, 0);
                    }
                });
            };
            return PowerQualityImpactTrendRender;
        }(Views.ViewPeriodChartRender));
        Views.PowerQualityImpactTrendRender = PowerQualityImpactTrendRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityImpactTrendGadget.TypeId,
    create: Dashboards.Models.PowerQualityImpactTrendGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityImpactTrendGadget.js.map