'use strict';

var _ = require('lodash')
var Promise = require('bluebird')
var db = require('./db')

var convertTimeStringsToOfferings = require('./time').convertTimeStringsToOfferings

var deptNumToCrsidCache = {}

function getCourse(clbid) {
	if (window.courseCache[clbid]) {
		console.log('using course cache')
		return Promise.resolve(window.courseCache[clbid])
	} else {
		console.log('using course database')
		return window.db.courses
			.query('clbid')
			.only(clbid)
			.limit(1)
			.execute()
			.then(function(courses) {
				var course = courses[0]
				course = convertTimeStringsToOfferings(course)
				window.courseCache[clbid] = course
				return course
			}).catch(function(records, err) {
				console.warn('course retrieval failed for: ' + clbid, arguments)
			})
	}
}

function getCourses(clbids) {
	// Takes a list of clbids, and returns a list of the course objects for
	// those clbids.

	// console.log('called getCourses with', clbids)
	return Promise.all(_.map(clbids, getCourse))
}

function deptNumToCrsid(deptNumString) {
	return new Promise(function(resolve, reject) {
		if (deptNumToCrsidCache[deptNumString]) {
			resolve(deptNumToCrsidCache[deptNumString])
		} else {
			// Filter to only those with matching dept strings
			window.db.courses
				.query('deptnum')
				.only(deptNumString)
				.limit(1)
				.execute()
				.then(function(courses) {
					if (_.size(courses)) {
						resolve(courses[0].crsid)
					}
					reject(new Error('Course ' + deptNumString + ' was not found'))
				})
		}
	})
}

function logResult(result) {
	console.log(result)
}

function checkCoursesForDeptNum(courses, deptNumString) {
	var crsidsToCheckAgainst = _.chain(courses).pluck('crsid').uniq().value()

	return deptNumToCrsid(deptNumString)
		.then(function(crsid) {
			/*console.log(
				'checkCoursesForDeptNum',
				'checking for', crsid, 'in', crsidsToCheckAgainst,
				'result', _.contains(crsidsToCheckAgainst, crsid))*/
			return _.contains(crsidsToCheckAgainst, crsid)
		}).catch(function(err) {
			console.error('checkCoursesForDeptNum error', err.stack)
		})
}

module.exports.getCourses = getCourses
module.exports.getCourse = getCourse
module.exports.logResult = logResult

module.exports.deptNumToCrsidCache = deptNumToCrsidCache
module.exports.deptNumToCrsid = deptNumToCrsid
module.exports.checkCoursesForDeptNum = checkCoursesForDeptNum

window.getCourses = module.exports

@@ -242,268 +242,49 @@
  %7B%0A%09
-if (window.courseCache%5Bclbid%5D) %7B%0A%09%09console.log('using course cache')%0A%09%09return Promise.resolve(window.courseCache%5Bclbid%5D)%0A%09%7D else %7B%0A%09%09console.log('using course database')%0A%09%09return window.db.courses%0A%09%09%09.query('clbid')%0A%09%09%09.only(clbid)%0A%09%09%09.limit(1)%0A%09%09%09.execute()%0A%09
+return db.store('courses')%0A%09%09.get(clbid)%0A
 %09%09.t
@@ -306,42 +306,12 @@
 urse
-s
 ) %7B%0A
-%09%09%09%09var course = courses%5B0%5D%0A%09
 %09%09%09c
@@ -360,48 +360,8 @@
 se)%0A
-%09%09%09%09window.courseCache%5Bclbid%5D = course%0A%09
 %09%09%09r
@@ -373,17 +373,16 @@
  course%0A
-%09
 %09%09%7D).cat
@@ -409,17 +409,16 @@
  err) %7B%0A
-%09
 %09%09%09conso
@@ -483,15 +483,11 @@
 )%0A%09%09
-%09
 %7D)%0A
-%09%7D%0A
 %7D%0A%0Af
@@ -958,36 +958,38 @@
 %0A%09%09%09
-window.db.
+db.store('
 courses
+')
 %0A%09%09%09%09.
-query
+index
 ('de
@@ -1001,20 +1001,19 @@
 ')%0A%09%09%09%09.
-only
+get
 (deptNum
@@ -1024,37 +1024,8 @@
 ng)%0A
-%09%09%09%09.limit(1)%0A%09%09%09%09.execute()%0A
 %09%09%09%09
@@ -1046,24 +1046,24 @@
 (courses) %7B%0A
-
 %09%09%09%09%09if (_.s
@@ -1074,16 +1074,20 @@
 courses)
+ %3E 0
 ) %7B%0A%09%09%09%09

Update getCourses to use treo