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 PowerQualityPowerFactorImpactGadget = (function (_super) {
            __extends(PowerQualityPowerFactorImpactGadget, _super);
            function PowerQualityPowerFactorImpactGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.PowerQualityPowerFactorImpactRenderer();
                _super.call(this, gadgetOptions);
                this.noDataMessage = i18n.loc.unencoded('PowerQualityGadgets', 'PowerQualityPowerFactor_NoData');
            }
            PowerQualityPowerFactorImpactGadget.CreateInstance = function (gadgetOptions) {
                var instance = new PowerQualityPowerFactorImpactGadget(gadgetOptions);
                return instance;
            };
            /* Gadget Overrides
            ******************************************************************************/
            PowerQualityPowerFactorImpactGadget.prototype.isConfigured = function () {
                return _super.prototype.isConfigured.call(this) && !StringUtil.isNullOrEmpty(this.getHandler().byName(Settings.Gadget.SourceId));
            };
            PowerQualityPowerFactorImpactGadget.prototype.hasData = function () {
                return this.dataSet &&
                    ((!_.isUndefined(this.dataSet.effectivePowerFactor) &&
                        !_.isUndefined(this.dataSet.powerFactorImpact) &&
                        !_.isUndefined(this.dataSet.targetPowerFactor)) &&
                        (this.dataSet.effectivePowerFactor != 0 &&
                            this.dataSet.targetPowerFactor != 0));
            };
            PowerQualityPowerFactorImpactGadget.TypeId = '30f72a63-fe57-4acc-b951-4d4cb50f3c44';
            return PowerQualityPowerFactorImpactGadget;
        }(Models.ViewPeriodGadget));
        Models.PowerQualityPowerFactorImpactGadget = PowerQualityPowerFactorImpactGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var PowerQualityPowerFactorImpactRenderer = (function (_super) {
            __extends(PowerQualityPowerFactorImpactRenderer, _super);
            function PowerQualityPowerFactorImpactRenderer() {
                _super.call(this);
                // Override the compact size, as this gadget has issues with small renderings.
                this.compactSizeMaxWidth = 340;
                this.compactSizeMaxHeight = 280;
            }
            /* Render Overrides
            ******************************************************************************/
            PowerQualityPowerFactorImpactRenderer.prototype.destroy = function () {
                _super.prototype.destroy.call(this);
                this.gadget = null;
                this.dataSet = null;
            };
            PowerQualityPowerFactorImpactRenderer.prototype.updateGadget = function (handler) {
                this.dataSet = this.gadget.dataSet;
                if (!this.dataSet)
                    return;
                var callBack = new Core.CallBack(this.renderContent, this);
                // Format model
                this.dataSet.formattedPowerFactorImpact = i18n.formatCurrency(this.dataSet.powerFactorImpact, 0);
                this.dataSet.formattedEffectivePowerFactor = i18n.formatPercent(Math.abs(this.dataSet.effectivePowerFactor), 0);
                this.dataSet.formattedTargetPowerFactor = i18n.formatPercent(Math.abs(this.dataSet.targetPowerFactor), 0);
                // Init method
                TemplateUtil.LocAndLoad(PowerQualityPowerFactorImpactRenderer.GadgetTemplateUri, callBack, { model: this.dataSet });
            };
            /* Private Methods
            ******************************************************************************/
            PowerQualityPowerFactorImpactRenderer.prototype.renderContent = function (template) {
                if (!this.$gadgetRender)
                    return; // We have been destroyed!
                this.$gadgetRender.html(template);
                this.modifyVisiblityByRenderSize();
                // set an absolute size for the wrapper, so the resizer can do
                // it's job
                var $gadgetWrapper = $(this.$gadgetProper.find('.bottomSection'));
                var $upperContent = this.$gadgetProper.find('.costSection');
                $upperContent.height($gadgetWrapper.height() * 0.6);
                $upperContent.width($gadgetWrapper.width());
                var $lowerContent = this.$gadgetProper.find('.costLabelSection');
                $lowerContent.height($gadgetWrapper.height() * 0.3);
                $lowerContent.width($gadgetWrapper.width());
                $gadgetWrapper = $(this.$gadgetProper.find('.topSection'));
                var $gaugeContent = this.$gadgetProper.find('.gaugeLabel');
                $gaugeContent.height($gadgetWrapper.height() * 0.1);
                $gaugeContent.width($gadgetWrapper.width());
                var pointerLabel = this.$gadgetProper.find('.pointerLabel');
                pointerLabel.height($gadgetWrapper.height() * 0.1);
                pointerLabel.width($gadgetWrapper.width());
                // Update the pointer.
                var pointerDirection = PowerQualityPowerFactorImpactRenderer.GetPointerDirection(this.dataSet.effectivePowerFactor);
                var pointerPos = PowerQualityPowerFactorImpactRenderer.GetPointerPosition(this.dataSet.effectivePowerFactor);
                this.$gadgetProper.find('.pointer').css(pointerDirection, pointerPos + '%');
                this.$gadgetProper.find('.pointer').css('transform', PowerQualityPowerFactorImpactRenderer.GetPointerOffset(this.dataSet.effectivePowerFactor));
                this.$gadgetProper.find('.pointerLabel').css(pointerDirection, '50%');
                this.$gadgetProper.find('.pointerLabel').css('transform', 'translateX(-50%)');
                // Pointer's tooltip
                this.$gadgetProper.prop('title', this.getTooltip());
                UIUtil.FitTextToElement({
                    $element: $upperContent,
                    maxFontPx: 800,
                    minFontPx: 10,
                    innerTag: 'div'
                });
                UIUtil.FitTextToElement({
                    $element: $lowerContent,
                    maxFontPx: 400,
                    minFontPx: 10
                });
                UIUtil.FitTextToElement({
                    $element: $gaugeContent,
                    maxFontPx: 200,
                    minFontPx: 10
                });
                UIUtil.FitTextToElement({
                    $element: pointerLabel,
                    maxFontPx: 200,
                    minFontPx: 10
                });
                this.$gadgetProper.find('.linearGaugeContainer').css('padding-top', $gaugeContent.height() + 15);
                // Work the top margin of the color scale 
                if (this.renderSize == Views.Application.ViewRenderSize.Compact) {
                    this.$gadgetProper.find('.linearGauge').removeAttr('style');
                    this.$gadgetProper.find('.linearGauge').css('top', '20%');
                }
                else {
                    this.$gadgetProper.find('.linearGauge').removeAttr('style');
                    this.$gadgetProper.find('.linearGauge').css('margin-top', '35px');
                }
            };
            PowerQualityPowerFactorImpactRenderer.prototype.getTooltip = function () {
                return StringUtil.format('{0}\n{1}\n{2}', i18n.loc.unencoded("PowerQualityGadgets", 'PowerQualityPowerFactorImpactGadget_PowerFactorLabel') + ' ' + this.dataSet.formattedEffectivePowerFactor, i18n.loc.unencoded("PowerQualityGadgets", 'PowerQualityPowerFactorImpactGadget_TargetLabel') + ' ' + this.dataSet.formattedTargetPowerFactor, i18n.loc.unencoded("PowerQualityGadgets", 'PowerQualityPowerFactorImpactGadget_SurchargeLabel') + ' ' + this.dataSet.formattedPowerFactorImpact);
            };
            PowerQualityPowerFactorImpactRenderer.GetPointerDirection = function (value) {
                return (value >= 0) ? 'left' : 'right';
            };
            PowerQualityPowerFactorImpactRenderer.GetPointerOffset = function (value) {
                return (value >= 0) ? 'translate(-50%, 0)' : 'translate(50%, 0)';
            };
            PowerQualityPowerFactorImpactRenderer.GetPointerLabelOffset = function (value) {
                return (value >= 0) ? 'translate(-25%, 0)' : 'translate(50%, 0)';
            };
            PowerQualityPowerFactorImpactRenderer.GetPointerPosition = function (value) {
                var propValue = 0;
                var absoluteValue = Math.abs(value);
                if (absoluteValue === 0) {
                    propValue = 0;
                }
                else if (absoluteValue === 100) {
                    propValue = 50;
                }
                else if (absoluteValue === 80) {
                    propValue = 25;
                }
                else if (absoluteValue > 80) {
                    propValue = (absoluteValue * 37.5) / 90;
                }
                else if (absoluteValue < 80) {
                    propValue = (absoluteValue * 12.5) / 40;
                }
                return propValue.toString();
            };
            PowerQualityPowerFactorImpactRenderer.GadgetTemplateUri = 'Content/Gadgets/PowerQualityPowerFactorImpact/PowerQualityPowerFactorImpactTemplate.html';
            return PowerQualityPowerFactorImpactRenderer;
        }(Views.ViewPeriodRender));
        Views.PowerQualityPowerFactorImpactRenderer = PowerQualityPowerFactorImpactRenderer;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.PowerQualityPowerFactorImpactGadget.TypeId,
    create: Dashboards.Models.PowerQualityPowerFactorImpactGadget.CreateInstance,
    resourceSet: 'PowerQualityGadgets'
});
//# sourceMappingURL=PowerQualityPowerFactorImpactGadget.js.map