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 __());
};
/************************************************
Energy Equivalency Gadget
************************************************/
var Dashboards;
(function (Dashboards) {
    var Models;
    (function (Models) {
        var EnergyEquivalencyGadget = (function (_super) {
            __extends(EnergyEquivalencyGadget, _super);
            function EnergyEquivalencyGadget(gadgetOptions) {
                gadgetOptions.renderer = new Dashboards.Views.EnergyEquivalencyGadgetRender();
                _super.call(this, gadgetOptions);
                // Special case where Comparison/Pie settings have the wrong aggregation settings.
                Models.ViewPeriodGadget.EnsureViewingPeriodCorrectForType(this, Settings.ViewingPeriod.SettingGroup.SingleDateWithoutAggregation);
            }
            EnergyEquivalencyGadget.CreateInstance = function (gadgetOptions) {
                var instance = new EnergyEquivalencyGadget(gadgetOptions);
                return instance;
            };
            /* Gadget Data Overrides
            ******************************************************************************/
            EnergyEquivalencyGadget.prototype.hasData = function () {
                // check for flag
                return _super.prototype.hasData.call(this) && this.dataSet.hasOwnProperty('Value');
            };
            EnergyEquivalencyGadget.TypeId = "79f51f8b-671f-4526-8092-d6db2e7132b4"; // actual gadget id from silverlight
            return EnergyEquivalencyGadget;
        }(Models.ViewPeriodGadget));
        Models.EnergyEquivalencyGadget = EnergyEquivalencyGadget;
    })(Models = Dashboards.Models || (Dashboards.Models = {}));
})(Dashboards || (Dashboards = {}));
var Dashboards;
(function (Dashboards) {
    var Views;
    (function (Views) {
        var EnergyEquivalencyGadgetRender = (function (_super) {
            __extends(EnergyEquivalencyGadgetRender, _super);
            function EnergyEquivalencyGadgetRender() {
                _super.call(this);
            }
            /* Renderer Overrides
            ******************************************************************************/
            EnergyEquivalencyGadgetRender.prototype.updateGadget = function (handler) {
                TemplateUtil.LocAndLoad(EnergyEquivalencyGadgetRender.RenderTemplateUri, new Core.CallBack(this.renderData, this));
            };
            /* Private Methods
            ******************************************************************************/
            EnergyEquivalencyGadgetRender.prototype.renderData = function (template) {
                if (!this.$gadgetRender)
                    return; // We have been destroyed!
                // the data from the server
                var data = this.gadget.dataSet;
                // the settings from the settings pages
                var imageName = this.getHandler().byName(Settings.Gadget.FileName.name);
                var units = this.getHandler().byName(Settings.Gadget.ValueSymbol.name);
                var description = this.getHandler().byName(Settings.Gadget.EquivalentUnitDescription.name);
                var symbolBeforeValue = (this.getHandler().byName(Settings.Gadget.SymbolBeforeValue.name) === 'true');
                var decimalPlacesToDisplay = parseInt(this.getHandler().byName(Settings.Gadget.DecimalPlaces.name));
                if (decimalPlacesToDisplay > 6) {
                    decimalPlacesToDisplay = 6; // Silverlight version allowed 9. Now max is 6;
                }
                this.$gadgetRender.html(template);
                // show the image if it is present
                if (imageName) {
                    this.$gadgetRender.find('.iconCell').show();
                    var baseUrl = fwk.url.format('ImageManager', 'EnergyGadgetImages');
                    var url = UrlStore.Combine(baseUrl, imageName);
                    this.$gadgetRender.find('.iconDiv').css('background-image', 'url("' + url + '")');
                    this.$gadgetRender.find('.iconCell').css('width', '50%');
                    this.$gadgetRender.find('.valueCell').css('width', '50%');
                }
                else {
                    this.$gadgetRender.find('.iconCell').hide();
                    this.$gadgetRender.find('.valueCell').css('width', '100%');
                }
                if (symbolBeforeValue) {
                    this.$gadgetRender.find('.value').text(StringUtil.format('{1} {0}', i18n.formatNumber(data.Value, decimalPlacesToDisplay), units));
                }
                else {
                    this.$gadgetRender.find('.value').text(StringUtil.format('{0} {1}', i18n.formatNumber(data.Value, decimalPlacesToDisplay), units));
                }
                // set the description
                this.$gadgetRender.find('.description').text(description);
                // resize the value to fit well in the cell
                UIUtil.FitTextToElement({
                    $element: this.$gadgetProper.find('.valueCell'),
                    maxFontPx: 1000,
                    minFontPx: 4
                });
                // resize the description to fit well in the cell
                UIUtil.FitTextToElement({
                    $element: this.$gadgetProper.find('.descriptionCell'),
                    maxFontPx: 1000,
                    minFontPx: 6
                });
            };
            EnergyEquivalencyGadgetRender.RenderTemplateUri = 'Content/Gadgets/EnergyEquivalency/EnergyEquivalencyTemplate.html';
            return EnergyEquivalencyGadgetRender;
        }(Views.ViewPeriodRender));
        Views.EnergyEquivalencyGadgetRender = EnergyEquivalencyGadgetRender;
    })(Views = Dashboards.Views || (Dashboards.Views = {}));
})(Dashboards || (Dashboards = {}));
// Register the gadget type
Dashboards.Controllers.GadgetType.RegisterDefinition({
    typeId: Dashboards.Models.EnergyEquivalencyGadget.TypeId,
    create: Dashboards.Models.EnergyEquivalencyGadget.CreateInstance,
    resourceSet: 'EnergyGadgets'
});
//# sourceMappingURL=EnergyEquivalencyGadget.js.map