/******************************************************************************
This class is responsible for selecting a single Source
*****************************************************************************/
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var SingleSourceSelectSetupControlDialog = (function () {
            function SingleSourceSelectSetupControlDialog(selectedSourceId, selectionType, okCallback) {
                if (okCallback === void 0) { okCallback = Core.CallBackPayload.None; }
                // State variables
                this.sourceSelection = null;
                this.okWasClicked = false;
                this.$dialog = null;
                this.treeSelectType = Controls.Smp.SourceSelectorMode[selectionType];
                if (!this.treeSelectType) {
                    this.treeSelectType = Controls.Smp.SourceSelectorMode.SourceNode;
                }
                this.okCallback = okCallback;
                this.defaultSourceId = selectedSourceId;
            }
            SingleSourceSelectSetupControlDialog.ShowDialog = function (selectedSourceId, selectionType, okCallback) {
                var dialog = new SingleSourceSelectSetupControlDialog(selectedSourceId, selectionType, okCallback);
                dialog.showDialog();
            };
            SingleSourceSelectSetupControlDialog.prototype.showDialog = function () {
                var _this = this;
                TemplateUtil.LocAndLoad(SingleSourceSelectSetupControlDialog.dialogTemplateUri, new Core.CallBack(function (template) {
                    _this.$dialog = $(template);
                    _this.renderDialog();
                }));
            };
            // Called after the Template has been loaded
            SingleSourceSelectSetupControlDialog.prototype.renderDialog = function () {
                var _this = this;
                this.bindEventHandlers();
                // Set initial State
                this.clearValidation();
                this.$dialog.dialog({
                    modal: true,
                    width: 435,
                    height: 460,
                    autoResize: false,
                    resizable: false,
                    closeText: i18n.loc.s('General', 'Close'),
                    buttons: [],
                    open: function () {
                        _this.$dialog.parent().find('.ui-dialog-title').html(i18n.loc.s('Gadgets', 'SingleSourceControl_Title'));
                        // Populate Series dialog with current series
                        _this.smpInformationLoading();
                        Controls.Smp.SmpSelector.SingleSourceSelector().load({
                            $sourceContainer: _this.$dialog.find('.singleSourceSelectContainer'),
                            sourceSelectedCallback: new Core.CallBackPayload(_this.sourceSelectionChange, _this),
                            loadedCallback: new Core.CallBackSuccess(_this.smpInformationLoaded, _this),
                            selectorMode: _this.treeSelectType,
                            selectedSource: _this.defaultSourceId,
                            showTreeTitle: false,
                            treeHeight: Controls.SelectTree.DefaultHeightTallNoTitle,
                            showTypeFilter: false
                        });
                    },
                    beforeClose: function (event, ui) {
                        // Callback with current options
                        if (_this.okWasClicked) {
                            if (!_this.onCloseDialog()) {
                                _this.okWasClicked = false;
                                return false;
                            }
                            _this.okCallback.call(true, _this.sourceSelection);
                        }
                        // Move SMP Selectors back to template
                        Controls.Smp.SmpSelector.SingleSourceSelector().detachSourceSelectorFromContainer();
                        return true;
                    },
                    close: function (event, ui) {
                        _this.$dialog.remove();
                    }
                });
            };
            // Bind all static event handlers.  Should only ever be called once.
            SingleSourceSelectSetupControlDialog.prototype.bindEventHandlers = function () {
                var _this = this;
                this.$dialog.find('.dialog-okbutton').click(function () {
                    _this.okWasClicked = true;
                    _this.$dialog.dialog('close');
                });
                this.$dialog.find('.CancelLink').click(function () {
                    _this.okWasClicked = false;
                    _this.$dialog.dialog('close');
                });
            };
            /************************ State Management *******************************/
            SingleSourceSelectSetupControlDialog.prototype.clearValidation = function () {
                // Reset Validation on Data Series Setup
                this.$dialog.find('.dialogValidationErrorMessage').html('');
                this.$dialog.find('.validationError').removeClass('validationError');
            };
            // Blocks while loading
            SingleSourceSelectSetupControlDialog.prototype.smpInformationLoading = function () {
                if (this.$dialog) {
                    this.$dialog.find('.singleSourceSelectContainer').block({
                        message: i18n.loc.s('SourceMeasurementSelector', 'LoadingSourceMeasurements')
                    });
                }
            };
            // A call back from the SmpSelector, triggered when the Smp Info is loaded
            SingleSourceSelectSetupControlDialog.prototype.smpInformationLoaded = function () {
                if (this.$dialog) {
                    if (!this.sourceSelection) {
                        // If we have no selection, lets use what the tree has
                        var treeSelection = Controls.Smp.SmpSelector.SingleSourceSelector().getSelectedSource();
                        if (treeSelection && treeSelection.DeviceId) {
                            this.sourceSelection = treeSelection;
                        }
                    }
                    this.$dialog.find('.singleSourceSelectContainer').unblock();
                    this.$dialog.find('.dialog-okbutton').prop('disabled', false);
                }
            };
            // A call back from the SmpSelector, triggered when a source selection changes
            SingleSourceSelectSetupControlDialog.prototype.sourceSelectionChange = function (valid, device) {
                if (this.$dialog && device) {
                    this.sourceSelection = device;
                    this.okWasClicked = false;
                }
            };
            ///************************ Settings Management ****************************/
            // Perform validation and save the series from UI settings.
            // - If validation fails, then the dialog will not close and return false
            SingleSourceSelectSetupControlDialog.prototype.onCloseDialog = function () {
                var validationMsg;
                ///****************  Reset Validation and start validating
                this.clearValidation();
                /* Series settings */
                // Validate Source Selection
                if (!this.sourceSelection || this.sourceSelection.DeviceId == '0') {
                    this.$dialog.find('.sourceTreeWrapper').addClass('validationError');
                    validationMsg = validationMsg || i18n.loc.s('SourceMeasurementSelector', 'Validation_SelectDevice');
                }
                if (validationMsg) {
                    validationMsg = validationMsg || i18n.loc.s('General', 'Validation_Failed');
                    this.$dialog.find('.dialogValidationErrorMessage').html(validationMsg);
                    this.$dialog.find('.validationError').first().focus();
                    return false;
                }
                return true;
            };
            SingleSourceSelectSetupControlDialog.dialogTemplateUri = 'Content/Gadgets/_baseSetupControls/SingleSourceSelectSetupControlDialogTemplate.html';
            return SingleSourceSelectSetupControlDialog;
        }());
        SetupControl.SingleSourceSelectSetupControlDialog = SingleSourceSelectSetupControlDialog;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
//# sourceMappingURL=SingleSourceSelectSetupControlDialog.js.map