import uniqueId from 'lodash/utility/uniqueId'
import loadArea from './load-area'
import getStudentData from './get-student-data'

import Worker from './check-student-against-area.worker.js'
const worker = new Worker()
// worker.onmessage = msg => console.log('[main] received message from check-student worker:', msg)
worker.addEventListener('error', msg => console.log('[main] received error from check-student worker:', msg))


function onMessage({sourceId, resolve, reject}, {data}) {
	const [resultId, type, contents] = JSON.parse(data)

	if (resultId === sourceId) {
		worker.removeEventListener('message', onMessage)

		if (type === 'result') {
			resolve(contents)
		}
		else if (type === 'error') {
			reject({_error: contents.message})
		}
	}
}


/**
 * Checks a student object against an area of study.
 *
 * @param {Object} student - the student to check
 * @param {Object} area - the area to check against
 * @returns {Promise} - the promise for a result
 * @promise ResultsPromise
 * @fulfill {Object} - The details of the area check.
 */
export default async function checkStudentAgainstArea(student, area) {
	const sourceId = uniqueId()

	return new Promise(async (resolve, reject) => {
		worker.addEventListener('message', onMessage.bind(null, {sourceId, resolve, reject}))

		const areaData = {
			...area,
		}
		try {
			areaData.data = await loadArea(area)
		}
		catch (err) {
			reject(err)
		}

		let studentData
		try {
			studentData = await getStudentData(student)
		}
		catch (err) {
			reject(err)
		}

		/* why stringify? from https://code.google.com/p/chromium/issues/detail?id=536620#c11:
		 > We know that serialization/deserialization is slow. It's actually faster to
		 > JSON.stringify() then postMessage() a string than to postMessage() an object. :(
		 */
		worker.postMessage(JSON.stringify([sourceId, studentData, areaData]))
	})
}

@@ -216,11 +216,8 @@
 r()%0A
-// 
 work
@@ -221,23 +221,21 @@
 orker.on
-message
+error
  = msg =
@@ -248,506 +248,69 @@
 ole.
-log('%5Bmain%5D received message from check-student worker:', msg)%0Aworker.addEventListener('error', msg =%3E console.log('%5Bmain%5D received error from check-student worker:', msg))%0A%0A%0Afunction onMessage(%7BsourceId, resolve, reject%7D, %7Bdata%7D) %7B%0A%09const %5BresultId, type, contents%5D = JSON.parse(data)%0A%0A%09if (resultId === sourceId) %7B%0A%09%09worker.removeEventListener('message', onMessage)%0A%0A%09%09if (type === 'result') %7B%0A%09%09%09resolve(contents)%0A%09%09%7D%0A%09%09else if (type === 'error') %7B%0A%09%09%09reject(%7B_error: contents.message%7D)%0A%09%09%7D%0A%09%7D%0A%7D
+warn('%5Bmain%5D received error from check-student worker:', msg)
 %0A%0A%0A/
@@ -760,65 +760,455 @@
 %7B%0A%09%09
-worker.addEventListener('message', onMessage.bind(null, %7B
+// This is inside of the function so that it doesn't get unregistered too early%0A%09%09function onMessage(%7Bdata%7D) %7B%0A%09%09%09const %5BresultId, type, contents%5D = JSON.parse(data)%0A%0A%09%09%09if (resultId === sourceId) %7B%0A%09%09%09%09worker.removeEventListener('message', onMessage)%0A%0A%09%09%09%09if (type === 'result') %7B%0A%09%09%09%09%09resolve(contents)%0A%09%09%09%09%7D%0A%09%09%09%09else if (type === 'error') %7B%0A%09%09%09%09%09reject(%7B_error: contents.message%7D)%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09worker.addEventListener('message', onMessage(
 sour
@@ -1228,17 +1228,16 @@
 , reject
-%7D
 ))%0A%0A%09%09co
@@ -1561,16 +1561,18 @@
 #c11:%0A%09%09
+ *
  %3E We kn
@@ -1644,16 +1644,18 @@
 er to%0A%09%09
+ *
  %3E JSON.

move the onMessage function back into checkStudentAgainstArea