
/**
 * Time in seconds between every request.
 */
var INTERVAL = 15;

var requestInterval = INTERVAL;
var failedRequests = 0;
var timeoutId = null;
var request = null;
var myText;
var myStatus;
var myError;

function onBodyLoad ()
{
	setTimeout (setText, 1000);
}

function onSettingsClick ()
{
	var deviceId = localStorage.getItem ('hederaDeviceId');

	if (deviceId == null)
		deviceId = '';

	deviceId = prompt ('Enter the device identifier', deviceId);

	if (deviceId == null)
		return;

	if (deviceId)
		localStorage.setItem ('hederaDeviceId', deviceId);
	else if (deviceId === '')
		localStorage.removeItem ('hederaDeviceId');

	if (timeoutId != null)
	{
		clearTimeout (timeoutId);
		timeoutId = null;
	}
	if (request)
	{
		request.abort ();
		request = null;
	}

	setText ();
}

function setText ()
{
	display (null, null, null);
	updateText ();
}

function updateText ()
{
	var deviceId = localStorage.getItem ('hederaDeviceId');

	if (!deviceId)
	{
		display (null, null, 'Device id not set');
		return;
	}

	var formData = new FormData ();
	formData.append ('srv', 'json:misc/production');
	formData.append ('deviceId', deviceId);

	request = new XMLHttpRequest();
	request.open ('POST', '', true);
	request.onreadystatechange =
		onRequestChange.bind (null, request);
	request.send (formData);
}

function onRequestChange (request)
{
	if (request.readyState !== 4)
		return;

	try {
		switch (request.status)
		{
			case 200:
				var data = JSON.parse (request.responseText).data;
				display (data.displayText, data.status, null);
				requestInterval = INTERVAL;
				failedRequests = 0;
				break;
			case 400:
				var json = JSON.parse (request.responseText);
				throw new Error (json.data.message);
			case 0:
				throw new Error ('Connection lost');
			default:
				throw new Error ('HTTP '+ request.status +': '+ request.statusText);
		}
	}
	catch (e)
	{
		display (myText, myStatus, e.message);
		console.error (e.message);
		failedRequests++;
		
		if (failedRequests > 10 && requestInterval < 60)
		{
			requestInterval += parseInt (Math.random() * INTERVAL) + 1;
			console.warn ('Request interval increased to %d seconds.', requestInterval);
		}
	}

	timeoutId = setTimeout (updateText, requestInterval * 1000);
	request = null;
}

function display (text, status, error)
{
	if (text === myText && status === myStatus && error === myError)
		return;

	var bgColor;
	var body = document.body;

	if (error)
	{
		text = text ? text : 'Er';
		$('error').textContent = error;
		$('error').style.display = 'block';
		bgColor = 'red';
	}
	else
	{
		$('error').textContent = '';
		$('error').style.display = 'none';
		bgColor = 'green';
	}

	$('text').textContent = text;
	body.className = status ? status : '';

	body.addEventListener ('transitionend', onTransitionEnd);
	body.style.backgroundColor = bgColor;

	myText = text;
	myStatus = status;
	myError = error;
}

function onTransitionEnd ()
{
	var body = document.body;
	body.removeEventListener ('transitionend', onTransitionEnd);
	body.style.backgroundColor = '';
}

function $ (elementId)
{
	return document.getElementById (elementId);
}
