var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var MultiMeasurementSelectRealtimeTemplateDialog = (function () {
            function MultiMeasurementSelectRealtimeTemplateDialog(okCallback) {
                if (okCallback === void 0) { okCallback = Core.CallBackPayload.None; }
                this.okWasClicked = false;
                this.okCallback = okCallback;
            }
            MultiMeasurementSelectRealtimeTemplateDialog.ShowDialog = function (okCallback) {
                var dialog = new MultiMeasurementSelectRealtimeTemplateDialog(okCallback);
                dialog.showDialog();
            };
            MultiMeasurementSelectRealtimeTemplateDialog.prototype.showDialog = function () {
                var _this = this;
                TemplateUtil.LocAndLoad(MultiMeasurementSelectRealtimeTemplateDialog.DialogTemplateUri, new Core.CallBack(function (template) {
                    _this.$dialog = $(template);
                    _this.renderDialog();
                }));
            };
            // Called after the Template has been loaded
            MultiMeasurementSelectRealtimeTemplateDialog.prototype.renderDialog = function () {
                var _this = this;
                this.bindEventHandlers();
                this.$dialog.dialog({
                    modal: true,
                    width: 750,
                    height: 500,
                    title: i18n.loc.unencoded('Gadgets', 'MultiMeasurementSetupControl_TemplateDialog_Title'),
                    autoResize: false,
                    resizable: false,
                    closeText: i18n.loc.s('General', 'Close'),
                    buttons: [],
                    open: function () {
                        _this.$dialog.block();
                        if (MultiMeasurementSelectRealtimeTemplateDialog.Templates.length == 0) {
                            _this.fetchTemplateList(new Core.CallBackPayload(function (status, templates) {
                                MultiMeasurementSelectRealtimeTemplateDialog.Templates = templates;
                                _this.renderTemplates();
                            }));
                        }
                        else {
                            _this.renderTemplates();
                        }
                    },
                    beforeClose: function (event, ui) {
                        // Callback with current options
                        if (_this.okWasClicked) {
                            _this.okCallback.call(_this.okWasClicked, _this.getSelectedTopicsFromTemplate());
                        }
                        return true;
                    },
                    close: function (event, ui) {
                        _this.$dialog.remove();
                    }
                });
            };
            MultiMeasurementSelectRealtimeTemplateDialog.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');
                });
            };
            MultiMeasurementSelectRealtimeTemplateDialog.prototype.renderTemplates = function () {
                var _this = this;
                var $templateList = this.$dialog.find('.templateList');
                _.each(MultiMeasurementSelectRealtimeTemplateDialog.Templates, function (template) {
                    var $aggEntry = _this.$dialog.find('#measurementTemplatesTemplate').clone();
                    var populatedTemplate = TemplateUtil.ApplyLocalModel('measurementTemplatesTemplate', $aggEntry.html(), template);
                    var $populatedTemplate = $(populatedTemplate);
                    $populatedTemplate.appendTo($templateList);
                    $populatedTemplate.find('input').change(function (event) {
                        // Any selection enabled the ok button
                        _this.$dialog.find('.dialog-okbutton').prop('disabled', false);
                    });
                });
                var $templates = this.$dialog.find('.templateList label');
                $templates.sort(function (a, b) {
                    var aText = $(a).find('span').text();
                    var bText = $(b).find('span').text();
                    return aText.localeCompare(bText);
                });
                $templates.detach().appendTo($templateList);
                this.$dialog.unblock();
            };
            MultiMeasurementSelectRealtimeTemplateDialog.prototype.getSelectedTopicsFromTemplate = function () {
                var topicIds = [];
                var selectedOption = $('.templateList input:checked').val();
                if (selectedOption) {
                    var selectedTemplate = _.findWhere(MultiMeasurementSelectRealtimeTemplateDialog.Templates, { key: selectedOption });
                    if (selectedTemplate) {
                        topicIds = selectedTemplate.topicIds;
                    }
                }
                return topicIds;
            };
            MultiMeasurementSelectRealtimeTemplateDialog.prototype.fetchTemplateList = function (callBack) {
                var url = fwk.url.get("RealTimeGridMeasurementTemplates");
                $.ajax({
                    url: url,
                    type: "GET",
                    data: [],
                    timeout: fwk.webRequestTimeoutMs,
                    success: function (templates) {
                        var parsedTemplates = [];
                        _.each(templates, function (template) {
                            var $xmlTemplate = $($.parseXML(template.Value));
                            var $topicIds = $xmlTemplate.find('Id');
                            var topicIds = [];
                            _.each($topicIds, function (topicNode) {
                                topicIds.push(parseInt($(topicNode).text()));
                            });
                            var displayName = StringUtil.format('{0} ({1})', i18n.loc.s('MultiMeasurementRealtimeTemplates', 'TableLibrary_' + template.Key), topicIds.length);
                            var readyTemplate = {
                                displayName: displayName,
                                key: $xmlTemplate.find('Name:first').text(),
                                topicIds: topicIds
                            };
                            parsedTemplates.push(readyTemplate);
                        });
                        callBack.call(true, parsedTemplates);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        fwk.logAjaxError(jqXHR, textStatus, errorThrown);
                        fwk.displayError(i18n.loc.s('General', 'Error_LoadFrameworkFailTitle', false), url);
                    }
                });
            };
            MultiMeasurementSelectRealtimeTemplateDialog.DialogTemplateUri = 'Content/Gadgets/_baseSetupControls/MultiMeasurementSelectRealtimeTemplateDialogTemplate.html';
            MultiMeasurementSelectRealtimeTemplateDialog.Templates = [];
            return MultiMeasurementSelectRealtimeTemplateDialog;
        }());
        SetupControl.MultiMeasurementSelectRealtimeTemplateDialog = MultiMeasurementSelectRealtimeTemplateDialog;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
//# sourceMappingURL=MultiMeasurementSelectRealtimeTemplateDialog.js.map