/*  OpenTally: Open-source election vote counting
 *  Copyright © 2021–2022 Lee Yingtong Li (RunasSudo)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

function clickAdvancedOptions() {
	if (document.getElementById('divAdvancedOptions').style.display === 'none') {
		document.getElementById('divAdvancedOptions').style.display = 'grid';
		document.getElementById('btnAdvancedOptions').innerHTML = 'Hide advanced options';
	} else {
		document.getElementById('divAdvancedOptions').style.display = 'none';
		document.getElementById('btnAdvancedOptions').innerHTML = 'Show advanced options';
	}
}

var tblResult = document.getElementById('result');
var divLogs2 = document.getElementById('resultLogs2');
var olStageComments;

var detailedTransfers = {};

var worker = new Worker('worker.js?v=GITVERSION');

worker.onmessage = function(evt) {
	if (evt.data.type === 'init') {
		document.getElementById('spanRevNum').innerText = evt.data.version;
		document.getElementById('divLoading').style.display = 'none';
		document.getElementById('divUI').style.display = 'block';
		
		// Init dropdowns
		// Can't compute correct width until #divUI, etc. is display: block
		//document.getElementById('divAdvancedOptions').style.display = 'grid';
		//for (let elSel of document.querySelectorAll('select')) {
		let elSel = document.getElementById('selPreset'); {
			var sel = new CustomSelect({elem: elSel});
			sel.open();
			document.getElementById('custom-' + elSel.id).style.width = (document.getElementById('custom-' + elSel.id).querySelector('.js-Dropdown-list').clientWidth + 32) + 'px';
			sel.close();
		}
		//document.getElementById('divAdvancedOptions').style.display = 'none';
	} else if (evt.data.type === 'initResultsTable') {
		tblResult.innerHTML = evt.data.content;
		divLogs2.innerHTML = '<p>Stage comments:</p>';
		olStageComments = document.createElement('ol');
		olStageComments.id = 'olStageComments';
		divLogs2.append(olStageComments);
		
	} else if (evt.data.type === 'describeCount') {
		document.getElementById('resultLogs1').innerHTML = evt.data.content;
		
	} else if (evt.data.type === 'updateResultsTable') {
		for (let row = 0; row < evt.data.result.length; row++) {
			if (evt.data.result[row]) {
				tblResult.rows[row].insertAdjacentHTML('beforeend', evt.data.result[row]);
				
				// Update candidate status
				if (
					(document.getElementById('selReport').value == 'votes' && row >= 3 && row % 2 == 1) ||
					(document.getElementById('selReport').value == 'ballots_votes' && row >= 4 && row % 2 == 0)
				) {
					if (tblResult.rows[row].lastElementChild.classList.contains('elected')) {
						tblResult.rows[row].cells[0].classList.add('elected');
					} else {
						tblResult.rows[row].cells[0].classList.remove('elected');
					}
				}
			}
		}
		
	} else if (evt.data.type === 'updateStageComments') {
		let elLi = document.createElement('li');
		elLi.id = 'stage' + evt.data.stageNum;
		elLi.innerHTML = evt.data.comment;
		olStageComments.append(elLi);
		
	} else if (evt.data.type === 'updateDetailedTransfers') {
		detailedTransfers[evt.data.stageNum] = evt.data.table;
		
	} else if (evt.data.type === 'finalResultSummary') {
		divLogs2.insertAdjacentHTML('beforeend', evt.data.summary);
		document.getElementById('printPane').style.display = 'block';
		
		// Linkify stage numbers
		document.querySelectorAll('tr.stage-no a').forEach(function(elA) {
			elA.onclick = function() {
				olStageComments.childNodes.forEach(function(elLi) { elLi.classList.remove('highlight'); });
				document.getElementById(elA.href.substring(elA.href.indexOf('#') + 1)).classList.add('highlight');
			};
		});
		
	} else if (evt.data.type === 'requireInput') {
		let response = window.prompt(evt.data.message);
		while (response === null) {
			response = window.prompt(evt.data.message);
		}
		worker.postMessage({'type': 'userInput', 'response': response});
		
	} else if (evt.data.type === 'errorMessage') {
		divLogs2.insertAdjacentHTML('beforeend', evt.data.message);
	}
}

worker.onerror = function(evt) {
	alert('An unknown error occurred while counting the votes. More details may be available in the browser\'s developer console.');
}

async function clickCount() {
	if (document.getElementById('bltFile').files.length === 0) {
		return;
	}
	
	// Read BLT file
	let bltPath = document.getElementById('bltFile').value;
	bltPath = bltPath.substring(Math.max(bltPath.lastIndexOf('\\'), bltPath.lastIndexOf('/')) + 1);
	
	let bltFile = document.getElementById('bltFile').files[0];
	let bltData = await bltFile.text();
	
	// Read CON file (if applicable)
	let conPath = null;
	let conData = null;
	if (document.getElementById('conFile').files.length > 0) {
		conPath = document.getElementById('conFile').value;
		conPath = conPath.substring(Math.max(conPath.lastIndexOf('\\'), conPath.lastIndexOf('/')) + 1);
		
		let conFile = document.getElementById('conFile').files[0];
		conData = await conFile.text();
	}
	
	// Init STV options
	let optsStr = [
		document.getElementById('chkRoundSFs').checked ? parseInt(document.getElementById('txtRoundSFs').value) : null,
		document.getElementById('chkRoundValues').checked ? parseInt(document.getElementById('txtRoundValues').value) : null,
		document.getElementById('chkRoundVotes').checked ? parseInt(document.getElementById('txtRoundVotes').value) : null,
		document.getElementById('chkRoundQuota').checked ? parseInt(document.getElementById('txtRoundQuota').value) : null,
		document.getElementById('selSumTransfers').value,
		document.getElementById('txtMeekSurplusTolerance').value,
		document.getElementById('selQuota').value,
		document.getElementById('selQuotaCriterion').value,
		document.getElementById('selQuotaMode').value,
		document.getElementById('selTies').value.split(','),
		document.getElementById('txtSeed').value,
		document.getElementById('selMethod').value,
		document.getElementById('selSurplus').value,
		document.getElementById('selPapers').value,
		document.getElementById('selExclusion').value,
		document.getElementById('chkMeekNZExclusion').checked,
		document.getElementById('selSample').value,
		document.getElementById('chkSamplePerBallot').checked,
		document.getElementById('chkBulkElection').checked,
		document.getElementById('chkBulkExclusion').checked,
		document.getElementById('chkDeferSurpluses').checked,
		document.getElementById('chkImmediateElect').checked,
		document.getElementById('txtMinThreshold').value,
		conPath,
		document.getElementById('selConstraintMethod').value,
		parseInt(document.getElementById('txtPPDP').value),
	];
	
	// Reset UI
	document.getElementById('printPane').style.display = 'none';
	document.getElementById('resultLogs1').innerHTML = '';
	tblResult.innerHTML = '';
	divLogs2.innerHTML = '';
	
	detailedTransfers = {};
	
	// Dispatch to worker
	worker.postMessage({
		'type': 'countElection',
		// Data
		'bltData': bltData,
		'conData': conData,
		'bltPath': bltPath,
		'conPath': conPath,
		// Options
		'optsStr': optsStr,
		'numbers': document.getElementById('selNumbers').value,
		'decimals': document.getElementById('txtDP').value,
		'reportStyle': document.getElementById('selReport').value,
	});
}

function viewDetailedTransfers(stageNum) {
	let wtransfers = window.open('', '', 'location=0,width=800,height=600');
	wtransfers.document.title = 'OpenTally Detailed Transfers: Stage ' + stageNum;
	
	// Add stylesheets
	for (let elCSSBase of document.querySelectorAll('head link')) {
		let elCSS = wtransfers.document.createElement('link');
		elCSS.rel = elCSSBase.rel;
		elCSS.type = elCSSBase.type;
		if (elCSSBase.href.endsWith('?v=GITVERSION')) {
			elCSS.href = elCSSBase.href.replace('?v=GITVERSION', '?v=' + Math.random());
		} else {
			elCSS.href = elCSSBase.href;
		}
		wtransfers.document.head.appendChild(elCSS);
	}
	
	wtransfers.document.body.innerHTML = detailedTransfers[stageNum];
}

// Provide a default seed
if (document.getElementById('txtSeed').value === '') {
	function pad(x) { if (x < 10) { return '0' + x; } return '' + x; }
	let d = new Date();
	document.getElementById('txtSeed').value = d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate());
}
