/**
 * - BufferedPersonScanner will check if the scanned reference is in the supplied `students` object.
 *	 @type type
 */
class RegistrationScanner extends BufferedPersonScanner {

	constructor( sfxCount, students, registerId, markUrl, lateMark,
	             presentMark, absentMark, lateTS, registerStartTS, registerEndTS ) {
		
		super(sfxCount, students);
		
		this.currStudentRef = null;
		this.currMark = null;
		
		this.lateDate = new Date(parseInt(lateTS+''+'000'));
		this.markUrl = markUrl;
		this.registerId = registerId;
		this.registerStartTS = registerStartTS;
		this.registerEndTS = registerEndTS;
		
		this.lateMark = lateMark;
		this.presentMark = presentMark;
		this.absentMark = absentMark;
	}
	
	// [Override]
	processScan(reference) {
		
		// Note: Success hasn't been confirmed yet. Ajax[Success/Fail] below are callbacks to finalise
		// register entry submission.
		
		// Show the loading image
		this.setLoading();

		var markReceived = Date.now() > this.lateDate ? this.lateMark : this.presentMark; 

		var markPostData = {
			'student': this.people[reference]['id'],
			'register': this.registerId,
			'register_start': this.registerStartTS,
			'register_end': this.registerEndTS,
			'mark': markReceived['id'],
			'absent_mark': this.absentMark['id']
		};

		$.ajax({
			type: "POST",
			url: this.markUrl,
			data: markPostData,
			dataType: "text",
			success: (resp) => this._onRegAjaxSuccess(resp, reference, markReceived),
			error: (req) => this._onRegAjaxError(req, reference)
		});
		
	}
	
	_onRegAjaxSuccess(response, reference, markReceived) {

		if (response) { // On success we expect an empty response.
			this.onFailedScan('Server Error. Try refreshing the page if this keeps happening.');
			return;
		}
		
		// Show the mark dialog
		this.showDialog(markReceived['id']+' - '+markReceived['name'], markReceived['highlightClass']);
		
		// Set buffered student scan results.
		super.processScan(reference);
	}
	
	_onRegAjaxError(request) {
		this.onFailedScan(request.responseText);
	}
}