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 Rating Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var PowerQualityRatingGadget = (function (_super) {
            __extends(PowerQualityRatingGadget, _super);
            function PowerQualityRatingGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityRatingRender();
                _super.call(this, gadgetOptions);
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityEvents_NoEvents');
            }
            PowerQualityRatingGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityRatingGadget(gadgetOptions);
                return instance;
            };
            /* Gadget Data Overrides
            ******************************************************************************/
            PowerQualityRatingGadget.prototype.hasData = function () {
                var hasData = this.dataSet &&
                    !_.isUndefined(this.dataSet.Rating) &&
                    !_.isUndefined(this.dataSet.Contributors);
                return hasData;
            };
            PowerQualityRatingGadget.TypeId = "aa5885e6-4852-49ca-a170-c88435ebdc38";
            return PowerQualityRatingGadget;
        }(Models.ViewPeriodGadget));
        Models.PowerQualityRatingGadget = PowerQualityRatingGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityRatingRender = (function (_super) {
            __extends(PowerQualityRatingRender, _super);
            function PowerQualityRatingRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            PowerQualityRatingRender.prototype.updateGadget = function (handler) {
                var callBack = new Core.CallBack(this.renderContent, this);
                TemplateUtil.LocAndLoad(PowerQualityRatingRender.RenderTemplateUri, callBack);
            };
            /* Private Methods
            ******************************************************************************/
            PowerQualityRatingRender.prototype.renderContent = function (template) {
                var _this = this;
                if (!this.$gadgetRender)
                    return; // We have been destroyed!
                this.$gadgetRender.html(template);
                this.modifyVisiblityByRenderSize();
                var data = this.gadget.dataSet;
                // Update thisthod...
                var $boxRating = this.$gadgetProper.find('.box' + data.Rating.toUpperCase());
                PowerQualityRatingRender.resetGradeElementColors(this.$gadgetProper.find('.gaugeBoxes'));
                this.setGradeElementColor($boxRating, data.Rating);
                var contributorList = this.$gadgetProper.find('.powerQualityRatingContributors');
                contributorList.empty();
                var contributorHtml = '';
                _.each(data.Contributors, function (value, contributor) {
                    if (value < 2) {
                        contributorHtml += '<br/>';
                        contributorHtml += _this.loc(contributor);
                    }
                });
                if (contributorHtml == '') {
                    contributorHtml = i18n.loc.s('General', 'None');
                }
                contributorList.html(contributorHtml);
                // Update the pointer and tooltip.
                var pointerPos = PowerQualityRatingRender.getPointerPosition(data.Pointer);
                var tooltip = i18n.formatPercent(data.Index, 0);
                this.$gadgetProper.find('.pointer')
                    .css("top", pointerPos + "%")
                    .prop("title", tooltip);
                this.$gadgetProper.find('.pointerLabel').text(tooltip);
                if (this.renderSize != Views.Application.ViewRenderSize.Compact) {
                    this.resizeContributorsText();
                    this.setGaugeZoom();
                }
                else {
                    // Update the compact view
                    this.populateCompactView(data);
                }
            };
            PowerQualityRatingRender.prototype.setGaugeZoom = function () {
                if (window.devicePixelRatio >= 1.5) {
                    return;
                }
                var zoomValue = 1;
                // Calculate the available width we can grow in, this means half the size of
                // gadgetRender minus the left margin amount and an extra 20px just to make sure 
                // it can never run all the way to the right.
                var widthAvailable = Math.floor((this.$gadgetRender.width() * 0.5) - 80);
                // Substract arbitrary value of 20px to make sure it can never go past that threshold.
                var heightAvailable = Math.floor((this.$gadgetRender.height()) - 30);
                // Standard rendering is 130px wide.
                var widthRatio = widthAvailable / 130;
                // Standard rendering is 330px tall.
                var heightRatio = heightAvailable / 330;
                zoomValue = Math.min(widthRatio, heightRatio);
                zoomValue = +((zoomValue).toFixed(3));
                this.$gadgetProper.find('.gadgetWrapper').css('transform', 'scale(' + zoomValue + ')');
            };
            PowerQualityRatingRender.prototype.resizeContributorsText = function () {
                // Resize contributors list label
                var $contributorsWrapper = $(this.$gadgetProper.find('.contributors'));
                var $labelWrapper = this.$gadgetProper.find('.labelWrapper');
                $labelWrapper.height($contributorsWrapper.height() * 0.1);
                $labelWrapper.width($contributorsWrapper.width());
                UIUtil.FitTextToElement({
                    $element: $labelWrapper,
                    maxFontPx: 55,
                    minFontPx: 10
                });
                // Resize contributors list
                var $listWrapper = this.$gadgetProper.find('.listWrapper');
                $listWrapper.height($contributorsWrapper.height() * 0.7);
                $listWrapper.width($contributorsWrapper.width() * 0.75);
                UIUtil.FitTextToElement({
                    $element: $listWrapper,
                    maxFontPx: 50,
                    minFontPx: 10
                });
            };
            PowerQualityRatingRender.prototype.populateCompactView = function (data) {
                this.$gadgetProper.find('.letter').text(data.Rating);
                var contributorsCount = this.getContributorsCount(data.Contributors);
                if (contributorsCount > 0) {
                    this.$gadgetProper.find('.details').html(contributorsCount +
                        ' ' + this.loc('ContributorsLabel'));
                }
                else {
                    this.$gadgetProper.find('.details').text(this.loc('NoContributorsLabel'));
                }
                this.setGradeElementColor(this.$gadgetProper.find('.compactRatingLetterContainer'), data.Rating);
                // Resize rating letter text
                var $ratingLetterWrapper = $(this.$gadgetProper.find('.compactRatingLetterContainer'));
                UIUtil.FitTextToElement({
                    $element: $ratingLetterWrapper,
                    maxFontPx: 200,
                    minFontPx: 10,
                    changeLineHeight: true
                });
                // Resize contributing issues text
                var $detailsWrapper = $(this.$gadgetProper.find('.compactContributorsContainer'));
                var $detailsContent = this.$gadgetProper.find('.compactDetails');
                $detailsContent.height($detailsWrapper.height());
                $detailsContent.width($detailsWrapper.width());
                var detailsContentMaxFontSize = Math.round($detailsContent.width());
                UIUtil.FitTextToElement({
                    $element: $detailsContent,
                    maxFontPx: detailsContentMaxFontSize,
                    minFontPx: 10
                });
                // Tooltip
                this.$gadgetProper.find('.details').prop('title', this.getContributorsTooltip(data.Contributors));
            };
            PowerQualityRatingRender.prototype.getContributorsTooltip = function (contributors) {
                var tooltip = '';
                for (var contributorName in contributors) {
                    if (contributors[contributorName] === PowerQualityRatingRender.SeverityHigh ||
                        contributors[contributorName] === PowerQualityRatingRender.SeverityMedium) {
                        tooltip += this.loc(contributorName) + '\n';
                    }
                }
                tooltip = tooltip.slice(0, -1);
                return tooltip;
            };
            PowerQualityRatingRender.prototype.getContributorsCount = function (contributors) {
                var count = 0;
                for (var contributorName in contributors) {
                    if (contributors[contributorName] === PowerQualityRatingRender.SeverityHigh ||
                        contributors[contributorName] === PowerQualityRatingRender.SeverityMedium) {
                        ++count;
                    }
                }
                return count;
            };
            PowerQualityRatingRender.prototype.loc = function (key) {
                return i18n.loc.s("PowerQualityGadgets", key);
            };
            PowerQualityRatingRender.prototype.setGradeElementColor = function ($jElement, grade) {
                var background = "#ffffff";
                switch (grade) {
                    case "A":
                        background = "#6bde00";
                        break;
                    case "B":
                        background = "#a8de00";
                        break;
                    case "C":
                        background = "#efdd00";
                        break;
                    case "D":
                        background = "#ffba01";
                        break;
                    case "E":
                        background = "#ff8c03";
                        break;
                    case "F":
                        background = "#ff3908";
                        break;
                }
                $jElement.css("color", "#ffffff")
                    .css("background-color", background)
                    .addClass('selected');
                return $jElement;
            };
            PowerQualityRatingRender.resetGradeElementColors = function ($boxes) {
                $boxes.find('.selected').removeAttr('style').removeClass('selected');
            };
            PowerQualityRatingRender.getPointerPosition = function (index) {
                // We get in a value of 0% for the bottom to 100% for the top
                // but the element position needs 0% for the top and 100% for 
                // the bottom.
                var pointerPosition = 100 - index;
                if (pointerPosition > 100)
                    pointerPosition = 100;
                if (pointerPosition < 0)
                    pointerPosition = 0;
                return pointerPosition;
            };
            PowerQualityRatingRender.RenderTemplateUri = 'Content/Gadgets/PowerQualityRating/PowerQualityRatingGadgetTemplate.html';
            PowerQualityRatingRender.SeverityHigh = 0;
            PowerQualityRatingRender.SeverityMedium = 1;
            PowerQualityRatingRender.SeverityLow = 2;
            return PowerQualityRatingRender;
        }(Views.ViewPeriodRender));
        Views.PowerQualityRatingRender = PowerQualityRatingRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityRatingGadget.TypeId,
    create: Dashboards.Models.PowerQualityRatingGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityRatingGadget.js.map