var AjaxMethod;
(function (AjaxMethod) {
    AjaxMethod["POST"] = "POST";
    AjaxMethod["GET"] = "GET";
})(AjaxMethod || (AjaxMethod = {}));
var AjaxOptions = (function () {
    function AjaxOptions(fileds) {
        if (fileds) {
            Object.assign(this, fileds);
        }
    }
    return AjaxOptions;
}());
var AjaxService = (function () {
    function AjaxService() {
    }
    AjaxService.prototype.send = function (options) {
        var that = this;
        var call = $.ajax({
            url: options.url,
            type: options.method,
            data: options.data,
            success: function (d) {
                if (options.callback) {
                    options.callback(d);
                }
                if (options.successMsg) {
                    DevExpress.ui.notify(options.successMsg, "success", 5000);
                }
            },
            error: function (d) {
                if (options.errorCallback) {
                    options.errorCallback(d);
                }
                if (options.errorMsg) {
                    DevExpress.ui.notify(options.errorMsg, "error", 5000);
                }
            }
        });
        return call;
    };
    return AjaxService;
}());

var BaseView = (function () {
    function BaseView() {
        this.BindLayoutMenu();
        this.LogDbCallsErrors();
    }
    BaseView.prototype.BindLayoutMenu = function () {
        if (document.location.href.indexOf('Scheduler') <= -1 && (document.location.href.indexOf('Reports') > -1 || document.location.href.indexOf('Visualize') > -1)) {
            $('#reportsMenuDx').addClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Management') > -1 ||
            document.location.href.indexOf('ConnectionProfiles') > -1 ||
            document.location.href.indexOf('Settings') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').addClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Logs') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').addClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').removeClass('active');
            }
        }
        else if (document.location.href.indexOf('Scheduler') > -1) {
            $('#reportsMenuDx').removeClass('active');
            $('#usersMenuDx').removeClass('active');
            $('#logsMenuDx').removeClass('active');
            if (this.IsDefined($('#scheduleMenuDx'))) {
                $('#scheduleMenuDx').addClass('active');
            }
        }
        $('#reportsMenuDx').on("click", function (e) {
            document.location.href = "/Reports/Index";
        });
        $('#usersMenuDx').on("click", function (e) {
            document.location.href = "/Management/Index";
        });
        $('#logsMenuDx').on("click", function (e) {
            document.location.href = "/Logs/Index";
        });
        if (this.IsDefined($('#scheduleMenuDx'))) {
            $('#scheduleMenuDx').on("click", function (e) {
                document.location.href = "/Reports/Scheduler";
            });
        }
    };
    BaseView.prototype.IsNotEmpty = function (obj) {
        return this.IsDefined(obj) && obj !== "" && obj !== " ";
    };
    BaseView.prototype.IsDefined = function (obj) {
        return typeof obj !== 'undefined' && obj !== null;
    };
    BaseView.prototype.LogDbCallsErrors = function () {
        var that = this;
        DevExpress.data.setErrorHandler(function (error) {
            that.StatusCheck().done(function () {
                DevExpress.ui.notify("Error occured: " + error.message, "error", 5500);
            });
        });
    };
    BaseView.prototype.StatusCheck = function () {
        return $.ajax({
            url: '/Visualize/StatusCheck',
            type: "GET",
            statusCode: {
                401: function () {
                    window.location.reload();
                }
            }
        });
    };
    BaseView.prototype.LogError = function (msg, time, height) {
        DevExpress.ui.notify({ message: msg, height: height }, "error", time != null ? time : 5500);
    };
    BaseView.prototype.Info = function (msg, time, height) {
        DevExpress.ui.notify({ message: msg, height: height }, "info", this.IsDefined(time) ? time : 5500);
    };
    BaseView.prototype.Loading = function (text) {
        var loadingPanel = $('#wrLoadingPanel').dxLoadPanel("instance");
        return loadingPanel;
    };
    BaseView.prototype.IsMobile = function () {
        var currentWidth = $(window).width();
        return currentWidth < 992;
    };
    return BaseView;
}());
document.addEventListener("DOMContentLoaded", function documentReady() {
    this.removeEventListener("DOMContentLoaded", documentReady);
    var view = new BaseView();
});

var ConnectionProfileViewModel = (function () {
    function ConnectionProfileViewModel() {
    }
    return ConnectionProfileViewModel;
}());

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var ConnectionProfilesView = (function (_super) {
    __extends(ConnectionProfilesView, _super);
    function ConnectionProfilesView() {
        var _this = _super.call(this) || this;
        _this.callController = "/ConnectionProfiles";
        _this.backBtnId = "#backPageDx";
        _this.nextBtnId = "#nextPageDx";
        _this.submitBtnId = "#submitParamsDx";
        _this.wizardDxId = "#wizardDx";
        _this.testBtnId = "#testBtnDx";
        _this.connectionTypeDxId = "#connectionTypeDx";
        _this.connectionTypeContainerId = "#connectionTypeContainer";
        _this.ajaxService = new AjaxService();
        var that = _this;
        $('#refreshDx').dxButton('instance').on('click', function () {
            $('#connectionGridDx').dxDataGrid('instance').refresh();
        });
        $('#addNewDx').dxButton('instance').on('click', function () {
            that.AddNewConnection();
        });
        return _this;
    }
    ConnectionProfilesView.prototype.SaveConnectionProfile = function () {
        var that = this;
        var profile = this.GetCurrentViewModel();
        if (profile) {
            var callUrl = this.callController + '/Save';
            var data = {
                __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                viewModel: profile
            };
            var callOptions = new AjaxOptions({
                url: callUrl,
                method: AjaxMethod.POST,
                data: data,
                callback: function (response) {
                    that.GetPopup().hide();
                    $('#connectionGridDx').dxDataGrid('instance').refresh();
                },
                errorMsg: "Error occurs during saving connection profile",
                successMsg: "Connection profile has been saved"
            });
            this.ajaxService.send(callOptions);
        }
    };
    ConnectionProfilesView.prototype.TestConnectionProfile = function () {
        var that = this;
        var profile = this.GetCurrentViewModel();
        if (profile) {
            var callUrl = this.callController + '/TestConnection';
            var data = {
                __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                viewModel: profile
            };
            var callOptions = new AjaxOptions({
                url: callUrl,
                method: AjaxMethod.POST,
                data: data,
                callback: function (response) {
                },
                errorMsg: "Error occurs during testing connection parameters",
                successMsg: "Connection established successfully"
            });
            this.ajaxService.send(callOptions);
        }
    };
    ConnectionProfilesView.prototype.AddNewConnection = function () {
        var that = this;
        var callUrl = this.callController + '/AddNewConnection';
        var data = {
            __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
        };
        var callOptions = new AjaxOptions({
            url: callUrl,
            method: AjaxMethod.POST,
            data: data,
            callback: function (response) { that.PopupForm.call(that, response); },
            errorMsg: "Error occurs during operation"
        });
        this.ajaxService.send(callOptions);
    };
    ConnectionProfilesView.prototype.EditConnection = function (id) {
        var that = this;
        if (this.IsNotEmpty(id)) {
            var callUrl = this.callController + '/EditConnection';
            var data = {
                __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                id: id
            };
            var callOptions = new AjaxOptions({
                url: callUrl,
                method: AjaxMethod.POST,
                data: data,
                callback: function (response) { that.PopupForm.call(that, response); },
                errorMsg: "Error occurs during operation"
            });
            this.ajaxService.send(callOptions);
        }
    };
    ConnectionProfilesView.prototype.SendConnectionProfile = function (o, a) {
        if (o === "load") {
            a.data.viewModel = this.GetCurrentViewModel();
        }
    };
    ConnectionProfilesView.prototype.DataServiceConnectorClick = function (e) {
        if (e) {
            if (this.IsDefined($("#serverDx").dxTextBox('instance'))) {
                $("#serverDx").dxTextBox('instance').option('disabled', e.value);
            }
            if (this.IsDefined($("#databaseDx").dxTextBox('instance'))) {
                $("#databaseDx").dxTextBox('instance').option('disabled', e.value);
            }
            if (this.IsDefined($("#instanceDx").dxTextBox('instance'))) {
                $("#instanceDx").dxTextBox('instance').option('disabled', e.value);
            }
            if (this.IsDefined($("#portDx").dxTextBox('instance'))) {
                $("#portDx").dxTextBox('instance').option('disabled', e.value);
            }
            if (this.IsDefined($("#tlsslDx").dxCheckBox('instance'))) {
                $("#tlsslDx").dxCheckBox('instance').option('disabled', e.value);
            }
            if (this.IsDefined($("#testBtnDx").dxButton('instance'))) {
                e.value ? $("#testBtnDx").dxButton('instance').option('text', 'Resolve Host') : $("#testBtnDx").dxButton('instance').option('text', 'Test Connection');
            }
        }
    };
    ConnectionProfilesView.prototype.PopupForm = function (content, show) {
        if (show === void 0) { show = true; }
        var popup = this.GetPopup();
        if (this.IsMobile()) {
            popup.option('width', '700px');
            popup.option('height', '370px');
        }
        popup.option('contentTemplate', null);
        popup.option('contentTemplate', $(content));
        popup.show();
        this.SetupPaging();
        this.SetupWizardActions();
    };
    ConnectionProfilesView.prototype.GetPopup = function () {
        var popup = $('#conProfilePopup').dxPopup("instance");
        return popup;
    };
    ConnectionProfilesView.prototype.SetupWizardActions = function () {
        var _this = this;
        var that = this;
        $(this.connectionTypeDxId).dxSelectBox('instance').on('selectionChanged', function (e) {
            if (e.selectedItem && e.selectedItem.Id) {
                if (e.selectedItem.Id === "InterSystem DeepSee" || e.selectedItem.Id === "InterSystem IRIS BI"
                    || e.selectedItem.Id === "Select Platform") {
                    $("#connectionPrefixDx").dxCheckBox('instance').option("disabled", true);
                }
                else {
                    $("#connectionPrefixDx").dxCheckBox('instance').option("disabled", false);
                }
                var callUrl = _this.callController + '/ChangeConnectionProfileType';
                var data = {
                    __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val(),
                    connectionProfileId: $('#ConnectionProfileId').val(),
                    connectionType: e.selectedItem.Id,
                    isPrivate: $("#isPrivateDx").dxCheckBox('instance').option("value")
                };
                var callOptions = new AjaxOptions({
                    url: callUrl,
                    method: AjaxMethod.POST,
                    data: data,
                    callback: function (response) { $(_this.connectionTypeContainerId).html(response); },
                    errorMsg: "Error occurs during operation"
                });
                _this.ajaxService.send(callOptions);
            }
            that.CheckPageStatus();
        });
    };
    ConnectionProfilesView.prototype.SetupPaging = function () {
        var _this = this;
        var that = this;
        this.wizardDx = $(this.wizardDxId).dxTabPanel("instance");
        this.wizardDx.on('selectionChanged', function (e) {
            if (that.wizardDx.option("selectedIndex") === 1) {
                if (_this.IsDefined($(_this.testBtnId).dxButton("instance"))) {
                    _this.testBtn = $('#testBtnDx').dxButton("instance");
                    _this.testBtn.option("onClick", function (e) {
                        that.TestConnectionProfile();
                    });
                }
            }
        });
        this.backBtn = $(this.backBtnId).dxButton("instance");
        this.backBtn.option("onClick", function (e) {
            var index = that.wizardDx.option("selectedIndex");
            index--;
            that.wizardDx.option("selectedIndex", index);
            that.CheckPageStatus();
        });
        this.nextBtn = $(this.nextBtnId).dxButton("instance");
        this.nextBtn.option("onClick", function (e) {
            var index = that.wizardDx.option("selectedIndex");
            index++;
            that.wizardDx.option("selectedIndex", index);
            that.CheckPageStatus();
        });
        this.submitBtn = $(this.submitBtnId).dxButton("instance");
        this.submitBtn.option("onClick", function (e) {
            that.SaveConnectionProfile();
        });
        this.CheckPageStatus();
    };
    ConnectionProfilesView.prototype.CheckPageStatus = function () {
        var disabled = $(this.connectionTypeDxId).dxSelectBox('instance').option("value") === null;
        this.backBtn.option('disabled', this.wizardDx.option("selectedIndex") === 0);
        this.nextBtn.option('disabled', this.wizardDx.option("selectedIndex") === 1 || disabled);
        this.submitBtn.option('disabled', this.wizardDx.option("selectedIndex") != 1);
    };
    ConnectionProfilesView.prototype.GetCurrentViewModel = function () {
        var viewModel = new ConnectionProfileViewModel();
        viewModel.ConnectionProfileId = this.IsNotEmpty($('#ConnectionProfileId').val()) ? parseInt($('#ConnectionProfileId').val().toString()) : null;
        viewModel.ConnectionType = $(this.connectionTypeDxId).dxSelectBox('instance').option("value");
        viewModel.IsPrivate = $("#isPrivateDx").dxCheckBox('instance').option("value");
        viewModel.ProfileName = $('#connectionNameDx').dxTextBox('instance').option("value");
        if (viewModel.ConnectionType == 'Oracle') {
            viewModel.Server = this.IsDefined($("#serverDx").dxTextArea('instance')) ? $("#serverDx").dxTextArea('instance').option("value") : null;
        }
        else {
            viewModel.Server = this.IsDefined($("#serverDx").dxTextBox('instance')) ? $("#serverDx").dxTextBox('instance').option("value") : null;
        }
        if (this.IsDefined($("#portDx").dxTextBox('instance'))) {
            var port = $("#portDx").dxTextBox('instance').option('value');
            if (port) {
                viewModel.Server += ":" + port;
            }
        }
        if (this.IsDefined($("#instanceDx").dxTextBox('instance'))) {
            var instance = $("#instanceDx").dxTextBox('instance').option('value');
            if (instance) {
                viewModel.Server += "\\" + instance;
            }
        }
        viewModel.Database = this.IsDefined($("#databaseDx").dxTextBox('instance')) ? $("#databaseDx").dxTextBox('instance').option('value') : null;
        viewModel.UserName = this.IsDefined($("#usernameDx").dxTextBox('instance')) ? $("#usernameDx").dxTextBox('instance').option('value') : null;
        viewModel.Password = this.IsDefined($("#passwordDx").dxTextBox('instance')) ? $("#passwordDx").dxTextBox('instance').option('value') : null;
        viewModel.DSN = this.IsDefined($("#dnsDx").dxTextBox('instance')) ? $("#dnsDx").dxTextBox('instance').option('value') : null;
        viewModel.TLSSL = this.IsDefined($("#tlsslDx").dxCheckBox('instance')) ? $("#tlsslDx").dxCheckBox('instance').option('value') : null;
        viewModel.Schema = this.IsDefined($("#connectionPrefixDx").dxCheckBox('instance')) ? $("#connectionPrefixDx").dxCheckBox('instance').option('value') : null;
        viewModel.TrustedConnection = this.IsDefined($("#trustedDx").dxCheckBox('instance')) ? $("#trustedDx").dxCheckBox('instance').option('value') : null;
        viewModel.Encrypted = this.IsDefined($("#encryptedDx").dxCheckBox('instance')) ? $("#encryptedDx").dxCheckBox('instance').option('value') : null;
        viewModel.TrustServerCertificate = this.IsDefined($("#trustservercertificateDx").dxCheckBox('instance')) ? $("#trustservercertificateDx").dxCheckBox('instance').option('value') : null;
        viewModel.APIKey = this.IsDefined($("#apiKeyDx").dxTextBox('instance')) ? $("#apiKeyDx").dxTextBox('instance').option('value') : null;
        viewModel.ReportName = this.IsDefined($("#reprotNameDx").dxTextBox('instance')) ? $("#reprotNameDx").dxTextBox('instance').option('value') : null;
        viewModel.ClientId = this.IsDefined($("#customerIdDx").dxTextBox('instance')) ? $("#customerIdDx").dxTextBox('instance').option('value') : null;
        viewModel.Parameters = this.IsDefined($("#parametersCDx").dxTextBox('instance')) ? $("#parametersCDx").dxTextBox('instance').option('value') : null;
        viewModel.UseDataServiceConnector = this.IsDefined($("#useDataServiceConnectorDx").dxCheckBox('instance')) ? $("#useDataServiceConnectorDx").dxCheckBox('instance').option('value') : null;
        viewModel.DataServiceConnector = this.IsDefined($("#dataServiceConnectorDx").dxTextBox('instance')) ? $("#dataServiceConnectorDx").dxTextBox('instance').option('value') : null;
        viewModel.DataServiceValues = this.IsDefined($("#dataServiceValuesDx").dxTextBox('instance')) ? $("#dataServiceValuesDx").dxTextBox('instance').option('value') : null;
        if (viewModel.UseDataServiceConnector) {
            viewModel.Server = null;
            viewModel.Database = null;
        }
        viewModel.Organization = this.IsDefined($("#orgNameDx").dxTextBox('instance')) ? $("#orgNameDx").dxTextBox('instance').option('value') : null;
        viewModel.SelectProduct = this.IsDefined($("#productsDx").dxSelectBox('instance')) ? $("#productsDx").dxSelectBox('instance').option('value') : null;
        return viewModel;
    };
    return ConnectionProfilesView;
}(BaseView));
var View = window.View || {};
document.addEventListener("DOMContentLoaded", function documentReady() {
    this.removeEventListener("DOMContentLoaded", documentReady);
    View = window.View = new ConnectionProfilesView();
});
