var QRCodeReader, selectedDeviceId, videoId, videoBlockId, resultBlock, modelNo; 

function decodeOnce(QRCodeReader, selectedDeviceId, callback) 
{
	QRCodeReader.decodeFromVideoDevice(selectedDeviceId, videoId, (result, err) => 
	{
		if (result) 
		{
			getProductByModel(result.text, callback)
		}
		if (err && !(err instanceof ZXing.NotFoundException)) 
		{
			console.error(err);
			document.getElementById('result-spl').textContent = err;
		}
	});
}

window.addEventListener('load', function () 
{
	//---------- QR CODES
	QRCodeReader = new ZXing.BrowserMultiFormatReader()
	QRCodeReader.getVideoInputDevices()
	.then((videoInputDevices) => 
	{
		selectedDeviceId = videoInputDevices[videoInputDevices.length - 1].deviceId;
	})
	.catch((err) => 
	{
		console.error(err)
	})
})

function setResultNormal(){document.getElementById(resultBlock).style.backgroundColor = 'white';}

function setResultError()
{
	document.getElementById(resultBlock).style.backgroundColor = '#009fe0';
}

function checkErrors()
{
	var obj = document.getElementById(resultBlock);
	var arr = obj.getElementsByClassName('error');	
	setResultNormal();
	if (arr.length > 0) 
	{
		var input = 'edtSplModel';
		deleteCookie(input);
		document.getElementById(input).value = ''; //clear the text because it is an invalid code.
		setResultError();
	}
	document.getElementById('btnClearSpl').style.display = 'block';
}

function clearBlock()
{
	resetScanner();
	setResultNormal();
	var obj = document.getElementById('result-spl');
	var input = 'edtSplModel';
	var inpobj = document.getElementById(input);
	deleteCookie(input);
	if (obj !== null) obj.innerHTML = '';
	if (inpobj !== null) inpobj.value = ''; //clear the text because it is an invalid code.
	document.getElementById('btnClearSpl').style.display = 'none';
}

function setIds()
{
	videoBlockId	= 'scan-spl';
	videoId 		= 'video-spl';
	modelNo 		= 'model-spl';
	resultBlock 	= 'result-spl';
	document.getElementById('btnClearSpl').style.display = 'block';
}

function showInputField()
{
	setIds();
	resetScanner();
	document.getElementById(modelNo).style.display 		= 'block';
	document.getElementById(resultBlock).style.display  = 'none';
}

function scanQRBarcodes(callback)
{
	decodeOnce(QRCodeReader, selectedDeviceId, callback);
}

function startScanner(callback)
{
	setIds();
	document.getElementById(videoBlockId).style.display = 'block';
	document.getElementById(resultBlock).style.display	= 'none';
	document.getElementById(modelNo).style.display 		= 'none';
	scanQRBarcodes(callback);
}

function resetScanner()
{
	if (videoId 		!== undefined) 	document.getElementById(videoBlockId).style.display = 'none';
	if (resultBlock 	!== undefined) 	document.getElementById(resultBlock).style.display 	= 'block';
	if (modelNo 		!== undefined) 	document.getElementById(modelNo).style.display 		= 'none';
	if (QRCodeReader 	!== undefined) 	QRCodeReader.reset();
	checkErrors();
}

function updateScanFields(rec)
{
	var field = document.getElementById(resultBlock);
	field.innerHTML = '<input id="edtInspectionModel" type="text" readonly class="disabled" value="'+rec.spl_description+'" />';
}

function getSPL(data, callback)
{
	var field = document.getElementById(data.field);
	sableFindRow('spl_model', data.model, updateScanFields)
	callback();
}

function getProductByModel(mdl, callback)
{
	setIds();
	var input = 'edtSplModel';
	var data = {};
	var model = mdl !== undefined ? mdl : document.getElementById(input).value;
	data.model = model.toUpperCase();
	data.field	= resultBlock;
	document.getElementById('hddnInspectionModel').value = data.model;
	setCookie(input, data.model, 90);
	getSPL(data, resetScanner);
	if (callback !== undefined) callback();
}