{#
	Eos - Verifiable elections
	Copyright © 2017  RunasSudo (Yingtong Li)
	
	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 <http://www.gnu.org/licenses/>.
#}

<div class="ui active text loader">Preparing your ballot. Please wait.</div>

<script>
	boothWorker.onmessage = function(msg) {
		try {
			rawAnswers = [];
			for (var answer_json of booth.answers) {
				rawAnswers.push(eosjs.eos.core.objects.__all__.EosObject.deserialise_and_unwrap(answer_json, null));
			}
			
			encryptedAnswers = [];
			for (var encrypted_answer_json of msg.data.encrypted_answers) {
				encryptedAnswers.push(eosjs.eos.core.objects.__all__.EosObject.deserialise_and_unwrap(encrypted_answer_json, null));
			}
			
			booth.ballot = eosjs.eos.base.election.__all__.Ballot();
			booth.ballot.answers = rawAnswers;
			booth.ballot.encrypted_answers = encryptedAnswers;
			booth.ballot.election_id = election._id;
			booth.ballot.election_hash = eosjs.eos.core.hashing.__all__.SHA256().update_obj(election).hash_as_b64();
			
			if (should_do_fingerprint) {
				// String.prototype.join confuses fingerprintjs2
				var strjoin = String.prototype.join;
				String.prototype.join = undefined;
				try {
					new Fingerprint2().get(function(result, components) {
						String.prototype.join = strjoin;
						booth.fingerprint = components;
						nextTemplate();
					});
				} catch (ex) {
					console.error(ex);
					nextTemplate();
				}
			} else {
				nextTemplate();
			}
		} catch (err) {
			boothError(err);
			throw err;
		}
	}
	boothWorker.onerror = function(err) {
		boothError(err);
		throw err;
	}
	try {
		boothWorker.postMessage({
			"action": "generateEncryptedVote",
			"static_base_url": "{{ static_base_url }}",
			"election": eosjs.eos.core.objects.__all__.EosObject.serialise_and_wrap(election, null),
			"answers": booth.answers
		});
	} catch (err) {
		boothError(err);
		throw err;
	}
</script>