'use strict';

import * as _ from 'lodash'

import uuid from '../helpers/uuid'
import randomChar from '../helpers/randomChar'
import emitter from '../helpers/emitter'
import db from '../helpers/db'

import ScheduleSet from './scheduleSet'
import StudySet from './studySet'

import * as demoStudent from '../../mockups/demo_student.json'

let Student = (encodedStudent) => {
	encodedStudent = encodedStudent || {}

	let student = {
		id: uuid(),
		name: 'Student ' + randomChar(),
		active: false,

		credits: { needed: 35, has: 0 },

		matriculation: 1894,
		graduation: 1898,

		studies: {},
		schedules: {},
		overrides: [],
		fabrications: [],
	}

	Object.defineProperty(student, 'courses', { get() {
		return student.schedules.activeCourses
	}})

	Object.defineProperty(student, 'encode', { value() {
		return JSON.stringify(student)
	}})

	Object.defineProperty(student, 'save', { value() {
		console.log('saving student', student.name)
		localStorage.setItem(student.id, JSON.stringify(student))
		// return db.store('students').put(student)
	}})

	emitter.on('saveStudent', student.save)

	student.id = encodedStudent.id || student.id
	student.name = encodedStudent.name || student.name

	if (encodedStudent.credits)
		student.credits.needed = encodedStudent.credits.needed || student.credits.needed

	student.matriculation = encodedStudent.matriculation || student.matriculation
	student.graduation = encodedStudent.graduation || student.graduation

	student.studies   = new StudySet(encodedStudent.studies || student.studies)
	student.schedules = new ScheduleSet(encodedStudent.schedules || student.schedules)
	student.overrides = encodedStudent.overrides || student.overrides
	student.fabrications = encodedStudent.fabrications || student.fabrications

	// todo: remove me when we have something more than tape.
	// for one thing, i'm pretty sure that all these emitters are causing some
	// level of a memory leak.
	emitter.on('change', student.save)

	return student
}

function loadStudentFromDb(forceDemo) {
	forceDemo = forceDemo || false

	let rawStudent;
	if (!forceDemo) {
		rawStudent = localStorage.getItem('3AE9E7EE-DA8F-4014-B987-8D88814BB848')
		// again, ick. reassignment.
		rawStudent = JSON.parse(rawStudent)
	}
	if (!rawStudent)
		rawStudent = demoStudent

	let student = new Student(rawStudent)
	window.student = student

	emitter.emit('loadedStudent', student)
	return student
}

emitter.on('revertStudentToDemo', () => loadStudentFromDb(true))

emitter.on('loadStudent', loadStudentFromDb)
window.loadStudentFromDb = loadStudentFromDb

export default Student
export {loadStudentFromDb, Student}

@@ -648,453 +648,8 @@
 %09%7D%0A%0A
-%09Object.defineProperty(student, 'courses', %7B get() %7B%0A%09%09return student.schedules.activeCourses%0A%09%7D%7D)%0A%0A%09Object.defineProperty(student, 'encode', %7B value() %7B%0A%09%09return JSON.stringify(student)%0A%09%7D%7D)%0A%0A%09Object.defineProperty(student, 'save', %7B value() %7B%0A%09%09console.log('saving student', student.name)%0A%09%09localStorage.setItem(student.id, JSON.stringify(student))%0A%09%09// return db.store('students').put(student)%0A%09%7D%7D)%0A%0A%09emitter.on('saveStudent', student.save)%0A%0A
 %09stu
@@ -1311,16 +1311,461 @@
 ations%0A%0A
+%09Object.defineProperty(student, 'courses', %7B get() %7B%0A%09%09return student.schedules.activeCourses%0A%09%7D%7D)%0A%0A%09Object.defineProperty(student, 'encode', %7B value() %7B%0A%09%09return JSON.stringify(student)%0A%09%7D%7D)%0A%0A%09Object.defineProperty(student, 'save', %7B value() %7B%0A%09%09console.log('saving student', student.name)%0A%09%09localStorage.setItem(student.id, JSON.stringify(student))%0A%09%09// return db.store('students').put(student)%0A%09%7D%7D)%0A%0A%09emitter.on('saveStudent', student.save)%0A%0A
 %09// todo

Edit the student before defining listeners