/* global WorkerGlobalScope */

import mapKeys from 'lodash/object/mapKeys'
import map from 'lodash/collection/map'
import filter from 'lodash/collection/filter'
import round from 'lodash/math/round'
import present from 'present'

import stringifyError from './stringify-error'
import evaluate from '../area-tools/evaluate'
import findLeafRequirements from '../area-tools/find-leaf-requirements'
import getActiveStudentCourses from './get-active-student-courses'

function alterCourse(course) {
	return mapKeys(course, (value, key) => {
		if (key === 'depts') {
			key = 'department'
		}
		else if (key === 'num') {
			key = 'number'
		}
		return key
	})
}

function checkStudentAgainstArea(student, area) {
	const areaData = area._area

	if (area._error) {
		console.error('checkStudentAgainstArea:', area._error, area)
		return Promise.resolve(area)
	}

	student.courses = map(getActiveStudentCourses(student), alterCourse)

	let details = {}
	try {
		details = evaluate(student, areaData)
	}
	catch (err) {
		console.error('checkStudentAgainstArea:', err)
		return Promise.resolve({...area, _error: err.message})
	}

	const finalReqs = findLeafRequirements(details)
	const maxProgress = finalReqs.length
	const currentProgress = filter(finalReqs, {computed: true}).length

	return Promise.resolve({
		...area,
		_area: details,
		_checked: true,
		_progress: {
			at: currentProgress,
			of: maxProgress,
		},
	})
}

export default checkStudentAgainstArea

if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
	self.addEventListener('message', ({data}) => {
		const start = present()

		// why stringify? 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. :(

		const [id, student, area] = JSON.parse(data)
		// console.log('[check-student] received message:', id, student, area)

		checkStudentAgainstArea(student, area)
			.then(result => {
				console.log(`[check-student(${id})] took ${round(present() - start)} ms`)
				self.postMessage(JSON.stringify([id, 'result', result]))
			})
			.catch(err => {
				console.error(`[check-student(${id})]`, err)
				self.postMessage(JSON.stringify([id, 'error', stringifyError(err)]))
			})
	})
}


@@ -2047,24 +2047,85 @@
 result =%3E %7B%0A
+%09%09%09%09self.postMessage(JSON.stringify(%5Bid, 'result', result%5D))%0A
 %09%09%09%09console.
@@ -2190,16 +2190,41 @@
 )%7D ms%60)%0A
+%09%09%09%7D)%0A%09%09%09.catch(err =%3E %7B%0A
 %09%09%09%09self
@@ -2261,51 +2261,38 @@
 d, '
-result', result%5D))%0A%09%09%09%7D)%0A%09%09%09.catch(err =%3E %7B
+error', stringifyError(err)%5D))
 %0A%09%09%09
@@ -2341,81 +2341,8 @@
 rr)%0A
-%09%09%09%09self.postMessage(JSON.stringify(%5Bid, 'error', stringifyError(err)%5D))%0A
 %09%09%09%7D

Move the logging in checkStudentAgainstArea