$(function() {
	$('#progress').hide();
	window.setTimeout(function() {

		var updateText = function(txt, style) {
			$('#updateText').removeClass('text-muted text-success text-danger text-warning');
			$('#updateText').html(txt);
			$('#updateText').addClass(style);
		}

		var serverRestarting = function() {
			$('#detailText').hide();
			$('#messageText').hide();
			$('#progress').hide();
			updateText('Waiting for server to restart <i class="fa-solid fa-spinner fa-spin">', 'text-muted');
		};

		var updateStateHandle;
		var pollUpdateState = function() {
			$.getJSON("/app/api/update-state", function(result) {
				if (result.error) {	
					$('#messageText').hide();
					updateText(result.error, 'text-danger');
				}
				else if (result.downloadEvent) {
					if (result.downloadEvent.value > -1) {
						$('#progress').show();
						$('#progressBar').width(result.downloadEvent.value + '%');
						$('#progressBar').attr('aria-valuenow', result.downloadEvent.value);
					}
					else {
						$('#progress').hide();
					}

					if(result.downloadEvent.message) {	
						$('#messageText').show();
						$('#messageText').html(result.downloadEvent.message);
					}
					else {
						$('#messageText').hide();
					}
					
					if (result.downloadEvent.detail) {
						$('#detailText').show();
						$('#detailText').html(result.downloadEvent.detail);
					}
					else {
						$('#detailText').hide();
					}
				}
			}).fail(function(err) {
				serverRestarting();
				window.clearInterval(updateStateHandle);
				window.setInterval(pollRestartState, 5000);
			});
		};

		var pollRestartState = function() {

			$.getJSON("/app/api/update-state", function(result) {
				if (result && result.ready) {
					/* Server back */
					updateText('<strong>Server is ready ...</strong>', 'text-success');
					window.location.href = '/';
				}
				else {
					updateText('Server is nearly ready ... ', 'text-muted');
				}
			}).fail(function(err) {
				serverRestarting();
				console.log("ping error " + err.status + ' ' + err.responseText);
			});
		};

		/* Check the update state once first in case the page is refreshed during the update
		   (at least while server is actually up) */

		$.getJSON("/app/api/update-state", function(result) {
			if (result.busy) {
				updateStateHandle = window.setInterval(pollUpdateState, 1000);
			}
			else {
				/* Start the update */
				$.getJSON("/app/api/start-update", function(result) {
					updateText('Update started ...');
					updateStateHandle = window.setInterval(pollUpdateState, 1000);
				}).fail(function(err) {
					console.log("error " + err.status);
					/* Update immedaite failed */
					if (err.status == 0) {
						updateText('Waiting for server to restart', 'text-muted');
						window.setInterval(pollRestartState, 5000);
					}
					else {
						/* Some other error */
						updateText(err.responseText, 'text-danger');
						window.location.href = '/';
					}
				});
			}
		}).fail(function(err) {
			serverRestarting();
			window.setInterval(pollRestartState, 5000);
		});
	}, 1000);
});