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 __());
};
/************************************************
Power Factor Impact Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PowerQualityImpactGadget = (function (_super) {
            __extends(PowerQualityImpactGadget, _super);
            function PowerQualityImpactGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityImpactRender();
                _super.call(this, gadgetOptions);
            }
            PowerQualityImpactGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityImpactGadget(gadgetOptions);
                return instance;
            };
            /* Gadget Data Overrides
            ******************************************************************************/
            PowerQualityImpactGadget.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.ErrorCode === "ReferenceArchitectureNotConfigured") {
                    this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'ReferenceArchitectureNotConfigured');
                    hasData = false;
                }
                if (hasData) {
                    // This gadget is only valid if one of the costs is not zero.
                    hasData = this.dataSet.ExternalCost ||
                        this.dataSet.InternalCost ||
                        this.dataSet.UnknownCost;
                }
                return hasData;
            };
            PowerQualityImpactGadget.TypeId = "458cbb94-15a4-4400-9311-261da504d1e8";
            return PowerQualityImpactGadget;
        }(Models.ViewPeriodGadget));
        Models.PowerQualityImpactGadget = PowerQualityImpactGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityImpactRender = (function (_super) {
            __extends(PowerQualityImpactRender, _super);
            function PowerQualityImpactRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PowerQualityImpactRender.prototype.updateGadget = function (handler) {
                var chartOptions = Views.ChartHelper.GetDefaultChartOptions(this);
                chartOptions.legend.visible = false;
                chartOptions.axes = [];
                chartOptions.title = Views.ChartHelper.GetChartTitle(i18n.loc.s('PowerQualityGadgets', 'PowerQualityImpact_Chart_Label'), this);
                var bottomAxis = Views.ChartHelper.GetStandardBottomXAxis(this);
                bottomAxis.majorTickMarks.visible = false;
                // Bind the data point name, then the number of hours to appear in the bottom axis.
                bottomAxis.categories = [i18n.loc.unencoded('PowerQualityGadgets', 'External') + '\n' + StringUtil.format('{0}: {1}', i18n.loc.unencoded('PowerQualityGadgets', 'Incidents'), i18n.formatNumber(this.gadget.dataSet.ExternalCount, 0)),
                    i18n.loc.unencoded('PowerQualityGadgets', 'Internal') + '\n' + StringUtil.format('{0}: {1}', i18n.loc.unencoded('PowerQualityGadgets', 'Incidents'), i18n.formatNumber(this.gadget.dataSet.InternalCount, 0)),
                    i18n.loc.unencoded('PowerQualityGadgets', 'Undetermined') + '\n' + StringUtil.format('{0}: {1}', i18n.loc.unencoded('PowerQualityGadgets', 'Incidents'), i18n.formatNumber(this.gadget.dataSet.UnknownCount, 0))];
                chartOptions.axes.push(bottomAxis);
                // Bind the left axis
                var leftAxis = Views.ChartHelper.GetStandardLeftYAxis(this);
                leftAxis.visible = false;
                leftAxis.majorGridLines.visible = false;
                leftAxis.majorTickMarks.visible = false;
                leftAxis.labels.visible = false;
                leftAxis.lineWidth = 0;
                var maxValue = _.max([this.gadget.dataSet.ExternalCost, this.gadget.dataSet.InternalCost, this.gadget.dataSet.UnknownCost]);
                leftAxis.maximum = maxValue * 1.05;
                chartOptions.axes.push(leftAxis);
                var chartSeries = [
                    {
                        type: 'column',
                        labels: Views.ChartHelper.GetDataSeriesLabelFormat(this.renderSize),
                        data: [this.gadget.dataSet.ExternalCost,
                            this.gadget.dataSet.InternalCost,
                            this.gadget.dataSet.UnknownCost],
                        axisY: 'y1'
                    }];
                chartOptions.series = chartSeries;
                // Create the chart
                this.destroyChart(); // Destroy any previous chart instance
                // Style the data point (column) with their corresponding colors.
                this.bindShapeRenderingEvent();
                // Style the data point label (above the column).
                this.bindDataPointLabelCreatingEvent();
                this.$getChart().jqChart(chartOptions);
                this.bindTooltipFormat();
            };
            PowerQualityImpactRender.prototype.bindShapeRenderingEvent = function () {
                this.$gadgetRender.bind('shapeRendering', function (e, shape) {
                    var color = PowerQualityImpactRender.getSeriesColor(shape.context.index);
                    shape.fillStyle = color;
                    shape.strokeStyle = color;
                });
            };
            PowerQualityImpactRender.prototype.bindDataPointLabelCreatingEvent = function () {
                this.$gadgetRender.bind('dataPointLabelCreating', function (e, data) {
                    var amount = data.text;
                    var numAmount = i18n.formatCurrency(amount, 2);
                    data.text = numAmount;
                    // The data.context.index happens to correspond with the
                    // palette colors. Add 6 to get to the last group of colors,
                    // which is the text color.
                    var textColorIndex = data.context.index + 6;
                    var color = PowerQualityImpactRender.getSeriesColor(textColorIndex);
                    data.fillStyle = color;
                });
            };
            PowerQualityImpactRender.prototype.bindTooltipFormat = function () {
                var _this = this;
                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 key = '';
                    switch (chartData.index) {
                        case (PowerQualityImpactRender.ExternalSeriesIdx): {
                            key = 'External';
                            break;
                        }
                        case (PowerQualityImpactRender.InternalSeriesIdx): {
                            key = 'Internal';
                            break;
                        }
                        default:
                        case (PowerQualityImpactRender.UnknownSeriesIdx): {
                            key = 'Unknown';
                            break;
                        }
                    }
                    // Create an 'empty' date then add the hours in Duration so we can format them correctly
                    var impactDurationInHours = _this.gadget.dataSet[key + 'Duration'];
                    var impactDuration = DateFormatUtil.secondsToHMS(impactDurationInHours * 60 * 60);
                    var impactCount = i18n.formatNumber(_this.gadget.dataSet[key + 'Count'], 0);
                    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;
                });
            };
            PowerQualityImpactRender.RenderTemplateUri = 'Content/Gadgets/PowerQualityImpact/PowerQualityImpactTemplate.html';
            PowerQualityImpactRender.ExternalSeriesIdx = 0;
            PowerQualityImpactRender.InternalSeriesIdx = 1;
            PowerQualityImpactRender.UnknownSeriesIdx = 2;
            PowerQualityImpactRender.ExternalTextIdx = 6;
            PowerQualityImpactRender.InternalTextIdx = 7;
            PowerQualityImpactRender.UnknownTextIdx = 8;
            PowerQualityImpactRender.getSeriesColor = function (seriesIdx) {
                switch (seriesIdx) {
                    case PowerQualityImpactRender.ExternalSeriesIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.External);
                    case PowerQualityImpactRender.InternalSeriesIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Internal);
                    case PowerQualityImpactRender.UnknownSeriesIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.Undetermined);
                    case PowerQualityImpactRender.ExternalTextIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.ExternalText);
                    case PowerQualityImpactRender.InternalTextIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.InternalText);
                    case PowerQualityImpactRender.UnknownTextIdx:
                        return Dashboards.PowerQuality.PowerQualityHelper.GetDataSeriesColor(Dashboards.PowerQuality.DataSeriesColor.UndeterminedText);
                }
            };
            return PowerQualityImpactRender;
        }(Views.ViewPeriodChartRender));
        Views.PowerQualityImpactRender = PowerQualityImpactRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityImpactGadget.TypeId,
    create: Dashboards.Models.PowerQualityImpactGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityImpactGadget.js.map