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 Settings;
(function (Settings) {
    var Gadget;
    (function (Gadget) {
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            EnergyEquivalencySetup: "87654321-daf8-11df-944e-f6f3dfd72085"
        });
        Gadget.EquivalentUnitDescription = { id: Gadget.SettingGroup.EnergyEquivalencySetup, name: "EquivalentUnitDescription" };
        Gadget.ValueMultiplier = { id: Gadget.SettingGroup.EnergyEquivalencySetup, name: "ValueMultiplier" };
        Gadget.ValueSymbol = { id: Gadget.SettingGroup.EnergyEquivalencySetup, name: "ValueSymbol" };
        Gadget.SymbolBeforeValue = { id: Gadget.SettingGroup.EnergyEquivalencySetup, name: "SymbolBeforeValue" };
        Gadget.DecimalPlaces = { id: Gadget.SettingGroup.EnergyEquivalencySetup, name: "DecimalPlaces" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var EnergyEquivalencySetup = (function (_super) {
            __extends(EnergyEquivalencySetup, _super);
            function EnergyEquivalencySetup() {
                _super.call(this, Settings.Gadget.SettingGroup.EnergyEquivalencySetup);
                this.title = i18n.loc.s(EnergyEquivalencySetup.ResxName, 'EquivalencyTab_Title');
            }
            EnergyEquivalencySetup.CreateInstance = function () {
                var instance = new EnergyEquivalencySetup();
                return instance;
            };
            EnergyEquivalencySetup.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(EnergyEquivalencySetup.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            EnergyEquivalencySetup.prototype.isValid = function () {
                var validationMsg = '';
                var valueMultiplier = this.$container.find('.value-multiplier').val();
                valueMultiplier = NumberUtil.parseUserInput(valueMultiplier, '');
                if (valueMultiplier === '') {
                    this.$container.find('.value-multiplier').addClass('validationError');
                    validationMsg = i18n.loc.s('EnergyGadgets', 'InvalidMultiplierFromWh');
                }
                else {
                    this.$container.find('.value-multiplier').removeClass('validationError');
                }
                if (!validationMsg) {
                    var $description = this.$container.find('.description');
                    var validationResult = Validation.Text.validateUserText($description, { allowEmpty: true });
                    if (!validationResult.valid) {
                        $description.addClass('validationError');
                        validationMsg = validationResult.message;
                    }
                }
                if (!validationMsg) {
                    var $valueSymbol = this.$container.find('.value-symbol');
                    var validationResult = Validation.Text.validateUserText($valueSymbol, { allowEmpty: true });
                    if (!validationResult.valid) {
                        $valueSymbol.addClass('validationError');
                        validationMsg = validationResult.message;
                    }
                }
                if (validationMsg) {
                    this.$container.find('.dialogValidationErrorMessage').html(validationMsg);
                    this.$container.find('.validationError').first().focus();
                    return false;
                }
                else {
                    this.$container.find('.dialogValidationErrorMessage').html('');
                }
                return true;
            };
            EnergyEquivalencySetup.prototype.beforeCommit = function () {
                var description = this.$container.find('.description').val();
                this.setting(Settings.Gadget.EquivalentUnitDescription.name, description);
                var valueMultiplier = this.$container.find('.value-multiplier').val();
                valueMultiplier = NumberUtil.parseUserInput(valueMultiplier, '');
                this.setting(Settings.Gadget.ValueMultiplier.name, valueMultiplier);
                var valueSymbol = this.$container.find('.value-symbol').val();
                this.setting(Settings.Gadget.ValueSymbol.name, valueSymbol);
                var symbolPlacement = this.$container.find('.symbol-placement').val();
                this.setting(Settings.Gadget.SymbolBeforeValue.name, symbolPlacement);
                var decimalPlaces = this.$container.find('.decimal-places').val();
                this.setting(Settings.Gadget.DecimalPlaces.name, decimalPlaces);
            };
            EnergyEquivalencySetup.prototype.isConfigured = function () {
                return true;
            };
            EnergyEquivalencySetup.prototype.isManagedSetting = function (name) {
                var managed = false;
                if (name.startsWith('Conversion_') || name.startsWith('WhConversion__')) {
                    managed = true;
                }
                else {
                    switch (name) {
                        case Settings.Gadget.EquivalentUnitDescription.name:
                        case Settings.Gadget.ValueMultiplier.name:
                        case Settings.Gadget.ValueSymbol.name:
                        case Settings.Gadget.SymbolBeforeValue.name:
                        case Settings.Gadget.DecimalPlaces.name:
                            managed = true;
                            break;
                    }
                    ;
                }
                return managed;
            };
            EnergyEquivalencySetup.prototype.render = function () {
                var _this = this;
                this.Equivalencies = new Dashboards.EnergyGadgets.PredefinedEquivalencies(this.settings);
                this.addConversionSettingsToEquivalenciesList();
                // set up initial values
                this.$container.find('.description').val(this.setting(Settings.Gadget.EquivalentUnitDescription.name));
                var valueMultiplier = this.setting(Settings.Gadget.ValueMultiplier.name) || '1';
                this.$container.find('.value-multiplier').val(i18n.formatNumber(valueMultiplier));
                this.$container.find('.value-symbol').val(this.setting(Settings.Gadget.ValueSymbol.name));
                if ((this.setting(Settings.Gadget.SymbolBeforeValue.name) || 'false').toLowerCase() === 'true') {
                    this.$container.find('.symbol-placement').val('true');
                }
                else {
                    this.$container.find('.symbol-placement').val('false');
                }
                // Decimal places. In the previous version, up to 9 decimal places were allowed. This is
                // now limited to 6, so handle the case where it was > 6 by replacing it with a 6.
                var decimalPlaces = this.setting(Settings.Gadget.DecimalPlaces.name) || '0';
                try {
                    if (parseInt(decimalPlaces) > 6) {
                        decimalPlaces = "6";
                    }
                }
                catch (exception) {
                    decimalPlaces = "0";
                }
                this.$container.find('.decimal-places').val(decimalPlaces);
                // wire up event listeners
                this.$container.find('.predefined-equivalency').click(function () {
                    SetupControl.PredefinedEquivalencySetupControlDialog.ShowDialog(_this.Equivalencies, new Core.CallBackPayload(function (ok, selection) {
                        if (ok.success) {
                            _this.setPredefinedValues(selection);
                        }
                    }));
                });
            };
            // add the list of predefined equivalencies to the drop-down list
            EnergyEquivalencySetup.prototype.addConversionSettingsToEquivalenciesList = function () {
                var _this = this;
                _.each(this.Equivalencies.getEquivalencies(), function (equivalency) {
                    var newItem = $('<option/>', { value: equivalency.key, text: equivalency.name });
                    var $list = _this.$container.find('#predefined-equivalency');
                    $list.append(newItem);
                });
            };
            EnergyEquivalencySetup.prototype.setPredefinedValues = function (equivalency) {
                if (equivalency) {
                    this.$container.find('#energy-equivalency-setup-control-description').val(equivalency.description);
                    this.$container.find('.value-multiplier').val(equivalency.multiplier);
                    this.$container.find('#energy-equivalency-setup-control-decimal-places').val(equivalency.decimalPlaces);
                    this.$container.find('.value-symbol').val(equivalency.symbol);
                    this.$container.find('.symbol-placement').val(equivalency.symbolBeforeValue ? 'true' : 'false');
                }
            };
            EnergyEquivalencySetup.prototype.getPredefinedSetting = function (name, setting) {
                var _this = this;
                this.settings.forEach(function (genericSetting) {
                    var genericName = String(genericSetting.Name);
                    var value = i18n.loc.s(EnergyEquivalencySetup.ResxName, genericSetting.Value);
                    if (!genericName.startsWith('Conversion_'))
                        return;
                    if (genericName.substring(genericName.length - 5, genericName.length) !== '_Name')
                        return;
                    var newItem = $('<option/>', { value: genericName, text: value });
                    var $list = _this.$container.find('#predefined-equivalency');
                    $list.append(newItem);
                });
            };
            EnergyEquivalencySetup.SetupControlTemplateUri = 'Content/Gadgets/EnergyEquivalency/EnergyEquivalencySetupControlTemplate.html';
            EnergyEquivalencySetup.ResxName = 'EnergyGadgets';
            return EnergyEquivalencySetup;
        }(Dashboards.Models.SetupControl));
        SetupControl.EnergyEquivalencySetup = EnergyEquivalencySetup;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.EnergyEquivalencySetup.CreateInstance,
    id: Settings.Gadget.SettingGroup.EnergyEquivalencySetup
});
//# sourceMappingURL=EnergyEquivalencySetupControl.js.map