function UrlExists(url, cb) {
    //debugger;
    console.log('Called UrlExists');
    //debugger;
    $.ajax({
        url: url,
        dataType: 'text',
        type: 'GET',
        error: function (textStatus, errorThrown) {
            //console.error(textStatus.responseText);
            //console.error(errorThrown);
            console.log('Url not connected: ' + url);
        },
        complete: function (xhr) {
            if (typeof cb === 'function')
                cb.apply(this, [xhr.status]);
        }
    });
}
function ShowLoading() {    
    $("#divLoading").html('<image src="../img/vgidentityserver/select2-spinner1.gif" alt="Loading, please wait" />');
}
function HideLoading() {    
    $("#divLoading").html("");
}




var tryCount = 0;
var RestartApp = function () {
    //debugger;
    console.log('TryCount: ' + tryCount);
    //debugger;    
    //let redirectUrl = '@Url.Content("~/Home")';
    var base_url = window.location.origin;
    console.log('base_url: ' + base_url);
    UrlExists(base_url, function (status) {
        if (status === 200) {
            //HideLoading();
            waitingDialog.hide();
            window.location.href = base_url;
        }
        else {
            if (tryCount < 11) {
                setTimeout(function () {
                    //Wait for 3 seconds
                    tryCount++;
                    RestartApp();
                }, 3000);
            }
        }
    });
};

function RestartServerClick() {
    //debugger;
    var test = window.location;
    var base_url = window.location.origin + '/RestartServer';
    //console.log('calling base_url: ' + base_url);
    waitingDialog.show();
    //debugger;
    $.ajax({
        type: "Post",
        //url: "RestartServer",//base_url
        url: base_url,
        async: false,
        cache: false,
        success: function (result) {
            //debugger;
            if (result.success) {
                //debugger;
                setTimeout(function () {
                    //debugger;
                    //Wait for 7 seconds
                    RestartApp();
                }, 7000);
            }
        },
        error: function (textStatus, errorThrown) {
            waitingDialog.hide();
            //console.error(textStatus.responseText);
            console.log('error while restarting server. Need to do it manually');
        }
    });
}

$('#btnRestartServer').on('click', function () {
    //ShowLoading();
    /*debugger;*/
    RestartServerClick();
});



var waitingDialog = waitingDialog || (function ($) {
    'use strict';
    //debugger;
    // Creating modal dialog's DOM
    //var $dialogold = $(
    //    '<div id="ModalWaitingDialog1" class="modal " data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-hidden="true" style="padding-top:15%; overflow-y:visible;">' +
    //    '<div class="modal-waiting-dialog modal-dialog modal-m">' +
    //    '<div class="modal-content">' +
    //    '<div class="modal-header"><h3 style="margin:0;"></h3></div>' +
    //    '<div class="modal-body">' +
    //    '<div class="progress progress-striped active"  role="progressbar" style="margin-bottom:0;"><div class="progress-bar progress-bar-striped progress-bar-animated" style="width: 100%"></div></div>' +
    //    '</div>' +
    //    '</div></div></div>');

    var $dialog = $(
        '<div class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-hidden="true" style="padding-top:15%; overflow-y:visible;">' +
        '<div class="modal-dialog modal-m">' +
        '<div class="modal-content">' +
        '<div class="modal-header"><h3 style="margin:0;"></h3></div>' +
        '<div class="modal-body">' +
        '<div class="progress progress-striped bg-info active" style="margin-bottom:0;"><div class="progress-bar progress-bar-striped bg-info active" style="width: 100%;background-color:#007bff"></div></div>' +
        '</div>' +
        '</div></div></div>');

    return {
        /**
         * Opens our dialog
         * @param message Custom message
         * @param options Custom options:
         * 				  options.dialogSize - bootstrap postfix for dialog size, e.g. "sm", "m";
         * 				  options.progressType - bootstrap postfix for progress bar type, e.g. "success", "warning".
         */
        //show: function (message, options) {
        //    // Assigning defaults
        //    if (typeof options === 'undefined') {
        //        options = {};
        //    }
        //    if (typeof message === 'undefined') {
        //        message = 'Please Wait...';
        //    }
        //    var settings = $.extend({
        //        dialogSize: 'm',
        //        progressType: '',
        //        onHide: null // This callback runs after the dialog was hidden
        //    }, options);
        //   /* debugger;*/
        //    // Configuring dialog
        //    $dialog.find('.modal-waiting-dialog').attr('class', 'modal-waiting-dialog modal-dialog').addClass('modal-' + settings.dialogSize);
        //    $dialog.find('.progress-bar').attr('class', 'progress-bar progress-bar-striped progress-bar-animated');
        //    if (settings.progressType) {
        //        $dialog.find('.progress-bar').addClass('progress-bar-' + settings.progressType);
        //    }
        //    $dialog.find('h3').text(message);
        //    // Adding callbacks
        //    if (typeof settings.onHide === 'function') {
        //        $dialog.off('hidden.bs.modal').on('hidden.bs.modal', function (e) {
        //            settings.onHide.call($dialog);
        //        });
        //    }
        //    // Opening dialog
        //    $dialog.modal();
        //},
        ///**
        // * Closes dialog
        // */
        //hide: function () {
        //    $dialog.modal('hide');
        //}

        show: function (message, options) {
            // Assigning defaults
            if (typeof options === 'undefined') {
                options = {};
            }
            if (typeof message === 'undefined') {
                message = 'Restarting Please Wait.....';
            }
            var settings = $.extend({
                dialogSize: 'm',
                progressType: '',
                onHide: null // This callback runs after the dialog was hidden
            }, options);

            // Configuring dialog
            $dialog.find('.modal-dialog').attr('class', 'modal-dialog').addClass('modal-' + settings.dialogSize);
            $dialog.find('.progress-bar').attr('class', 'progress-bar progress-bar-striped progress-bar-animated');
            if (settings.progressType) {
                $dialog.find('.progress-bar').addClass('progress-bar-striped progress-bar-' + settings.progressType);
            }
            $dialog.find('h3').text(message);
            // Adding callbacks
            if (typeof settings.onHide === 'function') {
                $dialog.off('hidden.bs.modal').on('hidden.bs.modal', function (e) {
                    settings.onHide.call($dialog);
                });
            }
            // Opening dialog
            $dialog.modal();
        },
        /**
         * Closes dialog
         */
        hide: function () {
            $dialog.modal('hide');
        }
    };

})(jQuery);