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) {
        // Settings.Gadget.SettingGroup.Summary
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            Summary: "78e4698e-daf8-11df-8977-b2f3dfd72085"
        });
        Gadget.Title = { id: Settings.Gadget.SettingGroup.Summary, name: "Title" };
        Gadget.GadgetOpacity = { id: Settings.Gadget.SettingGroup.Summary, name: "GadgetOpacity" };
        Gadget.UseDefaultOpacity = { id: Settings.Gadget.SettingGroup.Summary, name: "UseDefaultOpacity" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var Summary = (function (_super) {
            __extends(Summary, _super);
            function Summary() {
                _super.call(this, Settings.Gadget.SettingGroup.Summary);
                this.title = i18n.loc.s('Gadgets', 'GeneralSettingsControl_Title');
            }
            Summary.CreateInstance = function () {
                var instance = new Summary();
                return instance;
            };
            Summary.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                var templateOptions = {
                    model: {
                        opactity_0: i18n.formatPercent(0, 0),
                        opactity_10: i18n.formatPercent(10, 0),
                        opactity_20: i18n.formatPercent(20, 0),
                        opactity_30: i18n.formatPercent(30, 0),
                        opactity_40: i18n.formatPercent(40, 0),
                        opactity_50: i18n.formatPercent(50, 0),
                        opactity_60: i18n.formatPercent(60, 0),
                        opactity_70: i18n.formatPercent(70, 0),
                        opactity_80: i18n.formatPercent(80, 0),
                        opactity_90: i18n.formatPercent(90, 0),
                        opactity_100: i18n.formatPercent(100, 0)
                    }
                };
                TemplateUtil.LocAndLoad(Summary.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this), templateOptions);
            };
            Summary.prototype.isConfigured = function () {
                return true;
            };
            Summary.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.Title.name:
                    case Settings.Gadget.GadgetOpacity.name:
                    case Settings.Gadget.UseDefaultOpacity.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            Summary.prototype.isValid = function () {
                var $titleInput = this.$container.find('#setup-control-title-input');
                var result = Validation.Text.validateUserText($titleInput, { allowEmpty: true });
                if (!result.valid) {
                    this.addValidationMessage($titleInput, result.message);
                    return false;
                }
                // OK to update setting value
                this.setting(Settings.Gadget.Title.name, $titleInput.val());
                return true;
            };
            Summary.prototype.render = function () {
                var _this = this;
                var $titleInput = this.$container.find('#setup-control-title-input');
                $titleInput.val(this.setting(Settings.Gadget.Title.name));
                // Update opacity values to include regionalized percent symbols.
                _.each(this.$container.find('.opacity-values .opactity_value_label'), function (labelValue) {
                    var opacityLabelValue = $(labelValue).text();
                    $(labelValue).text(i18n.formatPercent(opacityLabelValue, 0));
                });
                var $globalOpacity = this.$container.find('.useDefaultOpacity');
                var globalOpacityValue = this.setting(Settings.Gadget.UseDefaultOpacity.name);
                var opacityValue = this.setting(Settings.Gadget.GadgetOpacity.name);
                if (!opacityValue) {
                    // If there is no opacity setting, set it to 100% and set the
                    // UseDefaultOpacity value to true.
                    opacityValue = '100';
                    globalOpacityValue = 'true';
                }
                this.$container.find('.gadgetOpacity').val(opacityValue);
                this.$container.find('.gadgetOpacity').change(function (event) {
                    _this.opacityChanged(event);
                });
                if (_.isUndefined(globalOpacityValue) || _.isNull(globalOpacityValue)) {
                    globalOpacityValue = 'false';
                }
                if (globalOpacityValue.toString().toLowerCase() === 'true') {
                    $globalOpacity.prop('checked', true);
                }
                $globalOpacity.change(function (event) {
                    _this.globalOpacityChanged(event);
                });
                $globalOpacity.change();
            };
            Summary.prototype.globalOpacityChanged = function (event) {
                var useDefault = $(event.delegateTarget).prop('checked');
                this.setting(Settings.Gadget.UseDefaultOpacity.name, useDefault.toString());
                // If user checks "use default", the radio button that matches the
                // dashboard's setting should be checked.
                if (useDefault) {
                    var currentDashboard = Dashboards.Models.Application.GetCurrentDashboard();
                    var settingHandler = new Settings.SettingHandler(currentDashboard.Properties);
                    var gadgetOpacitySetting = settingHandler.byName(Settings.Dashboard.GadgetOpacity);
                    if (gadgetOpacitySetting) {
                        this.$container.find('.gadgetOpacity').val(gadgetOpacitySetting);
                        this.$container.find('.gadgetOpacity').change();
                    }
                }
                this.$container.find('.opacity-values select').prop('disabled', useDefault);
            };
            Summary.prototype.opacityChanged = function (event) {
                this.setting(Settings.Gadget.GadgetOpacity.name, $(event.delegateTarget).val());
            };
            Summary.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/GeneralSettingsSetupControlTemplate.html';
            return Summary;
        }(Dashboards.Models.SetupControl));
        SetupControl.Summary = Summary;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.Summary.CreateInstance,
    id: Settings.Gadget.SettingGroup.Summary
});
//# sourceMappingURL=GeneralSettingsSetupControl.js.map