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 Quality Events Process Impact Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PowerQualityEventsProcessImpactGadget = (function (_super) {
            __extends(PowerQualityEventsProcessImpactGadget, _super);
            function PowerQualityEventsProcessImpactGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityEventsProcessImpactRender();
                _super.call(this, gadgetOptions);
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEvents_NoEvents');
            }
            PowerQualityEventsProcessImpactGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityEventsProcessImpactGadget(gadgetOptions);
                return instance;
            };
            /* Gadget Data Overrides
            ******************************************************************************/
            PowerQualityEventsProcessImpactGadget.prototype.hasData = function () {
                var hasData = this.dataSet &&
                    _.isArray(this.dataSet) &&
                    this.dataSet.length > 0;
                if (hasData) {
                    var likelyImpactData = _.findWhere(this.dataSet, { Name: "LikelyImpact" });
                    var noImpactData = _.findWhere(this.dataSet, { Name: "NoImpact" });
                    hasData =
                        likelyImpactData &&
                            !_.isUndefined(likelyImpactData.Value) &&
                            noImpactData &&
                            !_.isUndefined(noImpactData.Value);
                }
                return hasData;
            };
            PowerQualityEventsProcessImpactGadget.prototype.formatData = function (data) {
                if (data && data.hasOwnProperty('Results') && _.isArray(data.Results)) {
                    data = data.Results;
                }
                return data;
            };
            PowerQualityEventsProcessImpactGadget.TypeId = "a2ce1c34-dedd-4cf5-8ee1-fd62231deef9";
            return PowerQualityEventsProcessImpactGadget;
        }(Models.ViewPeriodGadget));
        Models.PowerQualityEventsProcessImpactGadget = PowerQualityEventsProcessImpactGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityEventsProcessImpactRender = (function (_super) {
            __extends(PowerQualityEventsProcessImpactRender, _super);
            function PowerQualityEventsProcessImpactRender() {
                _super.call(this);
            }
            /* Chart Render Overrides
            ******************************************************************************/
            PowerQualityEventsProcessImpactRender.prototype.$getChart = function () {
                var $chart = _super.prototype.$getChart.call(this);
                if ($chart) {
                    $chart = $chart.find('.chartContainer');
                }
                return $chart;
            };
            /* Renderer Overrides
            ******************************************************************************/
            PowerQualityEventsProcessImpactRender.prototype.updateGadget = function (handler) {
                TemplateUtil.LocAndLoad(PowerQualityEventsProcessImpactRender.RenderTemplateUri, new Core.CallBack(this.renderContent, this));
            };
            /* Private Methods
            ******************************************************************************/
            PowerQualityEventsProcessImpactRender.prototype.renderContent = function (template) {
                if (!this.$gadgetRender)
                    return; // We have been destroyed!
                this.$gadgetRender.html(template);
                var likelyImpactData = _.findWhere(this.gadget.dataSet, { Name: "LikelyImpact" });
                var noImpactData = _.findWhere(this.gadget.dataSet, { Name: "NoImpact" });
                var results = {
                    noImpact: noImpactData.Value,
                    likelyImpact: likelyImpactData.Value,
                    total: noImpactData.Value + likelyImpactData.Value
                };
                var $eventCountText = this.$gadgetProper.find('.eventCountText');
                $eventCountText.text(StringUtil.format('{0} {1}', results.likelyImpact, i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEventsProcessImpactGadget_Label')));
                var $descriptionCell = this.$gadgetProper.find('.descriptionCell');
                var percent5Width = Math.round($descriptionCell.width() * 0.05);
                $descriptionCell.css('padding', StringUtil.format('0px {0}px 0px {0}px', percent5Width));
                UIUtil.FitTextToElement({
                    $element: $descriptionCell,
                    maxFontPx: 200,
                    minFontPx: 8
                });
                var $totalEventText = this.$gadgetProper.find('.totalEventText');
                var totalEventText = StringUtil.format(i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEventsProcessImpactGadget_Total_Label_1'), results.total);
                $totalEventText.text(totalEventText);
                var $totalEventCell = this.$gadgetProper.find('.totalEventCell');
                var percent20Width = Math.round($totalEventCell.height() * 0.4);
                $totalEventCell.css('padding', StringUtil.format('0px {0}px {0}px {0}px', percent20Width));
                UIUtil.FitTextToElement({
                    $element: $totalEventCell,
                    maxFontPx: 200,
                    minFontPx: 8
                });
                // Create the chart
                var fillStyleColors = [];
                var seriesData = [];
                seriesData.push([i18n.loc.unencoded('PowerQualityGadgets', 'LikelyProcessImpact'), results.likelyImpact]);
                fillStyleColors.push('#b10043');
                seriesData.push([i18n.loc.unencoded('PowerQualityGadgets', 'NoProcessImpact'), results.noImpact]);
                fillStyleColors.push('#9fa0a4');
                // 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 chartOptions = Dashboards.Views.ChartHelper.GetDefaultChartOptions(this);
                chartOptions.legend.visible = false;
                var chartSeries = Dashboards.Views.ChartHelper.GetPieChartSeriesOptions(options);
                chartOptions.series = chartSeries;
                this.destroyChart(); // Destroy any previous chart instance
                Views.ChartHelper.BindPieChartLabelFormat(this);
                // In IE (for some reason!) the chart on first load does not respect the height of the container chain.
                // As a resolution, we explicitly set the height on the chart container, so jqChart correctly
                // renders as the right size.
                this.$getChart().height(this.$getChart().height());
                this.$getChart().jqChart(chartOptions);
                // Bind custom tooltip format show hours and costs.
                this.bindTooltipFormat();
            };
            PowerQualityEventsProcessImpactRender.prototype.bindTooltipFormat = function () {
                var _this = this;
                this.$gadgetRender.find('.chartContainer').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;
                });
            };
            PowerQualityEventsProcessImpactRender.RenderTemplateUri = 'Content/Gadgets/PowerQualityEventsProcessImpact/PowerQualityEventsProcessImpactTemplate.html';
            PowerQualityEventsProcessImpactRender.GadgetIconUri = 'Content/Gadgets/PowerQualityEventsProcessImpact/PQ_Impact_500.png';
            return PowerQualityEventsProcessImpactRender;
        }(Views.ViewPeriodChartRender));
        Views.PowerQualityEventsProcessImpactRender = PowerQualityEventsProcessImpactRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityEventsProcessImpactGadget.TypeId,
    create: Dashboards.Models.PowerQualityEventsProcessImpactGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityEventsProcessImpact.js.map