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.MultiMeasurement
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            MultiMeasurement: "73b86362-505f-4eb9-92fe-6e5dfc8e2d23"
        });
        Gadget.MultiSelectMeasurements = { id: Settings.Gadget.SettingGroup.MultiMeasurement, name: "MultiSelectMeasurements" };
        Gadget.MultiSelectShowRealtimeMeasurementTemplates = { id: Settings.Gadget.SettingGroup.MultiMeasurement, name: "ShowRealtimeMeasurementTemplates" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        // This module self-registers the following localization Resource Sets:
        i18n.loc.add('MultiMeasurementRealtimeTemplates');
        var MultiMeasurement = (function (_super) {
            __extends(MultiMeasurement, _super);
            function MultiMeasurement() {
                _super.call(this, Settings.Gadget.SettingGroup.MultiMeasurement);
                this.multiMeasurementSelector = null;
                this.title = i18n.loc.s('Gadgets', 'MultiMeasurementSetupControl_Title');
            }
            MultiMeasurement.CreateInstance = function () {
                var instance = new MultiMeasurement();
                return instance;
            };
            MultiMeasurement.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(MultiMeasurement.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            MultiMeasurement.prototype.isConfigured = function () {
                return this.selectedMeasurements.length > 0;
            };
            MultiMeasurement.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.MultiSelectMeasurements.name:
                    case Settings.Gadget.MultiSelectShowRealtimeMeasurementTemplates.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            MultiMeasurement.prototype.onFocus = function () {
                if (!this.multiMeasurementSelector) {
                    this.multiMeasurementSelector = new Controls.Smp.MultiMeasurementSelect(this.$container.find('.measurementSelection'));
                    this.multiMeasurementSelector.initialize({
                        isSmallRender: this.isSmallRenderWindow()
                    }, this.selectedMeasurements);
                }
            };
            MultiMeasurement.prototype.onBlur = function () {
            };
            MultiMeasurement.prototype.destroy = function () {
                if (this.multiMeasurementSelector) {
                    this.multiMeasurementSelector.unloadControl();
                    this.multiMeasurementSelector = null;
                }
                _super.prototype.destroy.call(this);
            };
            MultiMeasurement.prototype.isValid = function () {
                if (this.multiMeasurementSelector) {
                    // If we have a selector, use these values.
                    this.selectedMeasurements = this.multiMeasurementSelector.getSelection();
                    this.setting(Settings.Gadget.MultiSelectMeasurements.name, JSON.stringify(this.selectedMeasurements));
                }
                return true;
            };
            MultiMeasurement.prototype.render = function () {
                // We must call this AFTER we are visible to ensure our loading block is centered...
                var _this = this;
                // Bind Measurements
                this.selectedMeasurements = [];
                var measurementsJsonString = this.setting(Settings.Gadget.MultiSelectMeasurements.name);
                if (measurementsJsonString) {
                    this.selectedMeasurements = JSON.parse(measurementsJsonString);
                }
                if (this.setting(Settings.Gadget.MultiSelectShowRealtimeMeasurementTemplates.name) === 'true') {
                    var $templateButton = this.$container.find('.measurementTemplateSelect');
                    $templateButton.show();
                    $templateButton.click(function () { _this.showTemplateDialog(); });
                }
            };
            MultiMeasurement.prototype.prepareToShowTemplateDialog = function () {
                var _this = this;
                if (this.multiMeasurementSelector.getSelection().length > 0) {
                    // If we are not empty.  User must confirm that their selection will be purged.
                    Controls.ConfirmationDialog.Show(i18n.loc.unencoded('Gadgets', 'MultiMeasurementSetupControl_ConfirmClearForTemplate_Title'), i18n.loc.s('Gadgets', 'MultiMeasurementSetupControl_ConfirmClearForTemplate_Message'), {
                        confirmText: i18n.loc.s('General', 'Yes'),
                        cancelText: i18n.loc.s('General', 'No'),
                        showCancel: true,
                        size: Controls.ConfirmationDialog.Size.Small
                    }, new Core.CallBackSuccess(function (status) {
                        // Ok clicked?
                        if (status.success) {
                            _this.multiMeasurementSelector.removeAll();
                        }
                        // Ready to show
                        _this.showTemplateDialog();
                    }));
                }
                else {
                    // Ready to show
                    this.showTemplateDialog();
                }
            };
            MultiMeasurement.prototype.showTemplateDialog = function () {
                var _this = this;
                SetupControl.MultiMeasurementSelectRealtimeTemplateDialog.ShowDialog(new Core.CallBackPayload(function (status, topicIds) {
                    if (!status.success || !topicIds || topicIds.length == 0)
                        return;
                    var applyTemplateMeasurements = function () {
                        if (topicIds && topicIds.length > 0) {
                            var mmSelector = Controls.Smp.SmpSelector.MultiMeasurementSelector();
                            for (var topicIdx = 0; topicIdx < topicIds.length; topicIdx++) {
                                mmSelector.selectItem(topicIds[topicIdx], false, true);
                            }
                        }
                    };
                    // If we are not empty.  User must confirm that their selection will be purged.
                    if (_this.multiMeasurementSelector.getSelection().length > 0) {
                        Controls.ConfirmationDialog.Show(i18n.loc.s('Gadgets', 'MultiMeasurementSetupControl_ConfirmClearForTemplate_Title'), i18n.loc.s('Gadgets', 'MultiMeasurementSetupControl_ConfirmClearForTemplate_Message'), {
                            confirmText: i18n.loc.s('General', 'Yes'),
                            cancelText: i18n.loc.s('General', 'No'),
                            showCancel: true,
                            size: Controls.ConfirmationDialog.Size.Small
                        }, new Core.CallBackSuccess(function (status) {
                            // Ok clicked?
                            if (status.success) {
                                _this.multiMeasurementSelector.removeAll();
                            }
                            // Ready to apply.  Delay call by 100ms, as the removeAll is on a setTimeout
                            setTimeout(function () {
                                applyTemplateMeasurements();
                            }, 100);
                        }));
                    }
                    else {
                        // Ready to apply
                        applyTemplateMeasurements();
                    }
                }));
            };
            MultiMeasurement.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/MultiMeasurementSelectSetupControlTemplate.html';
            return MultiMeasurement;
        }(Dashboards.Models.SetupControl));
        SetupControl.MultiMeasurement = MultiMeasurement;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.MultiMeasurement.CreateInstance,
    id: Settings.Gadget.SettingGroup.MultiMeasurement
});
//# sourceMappingURL=MultiMeasurementSelectSetupControl.js.map