
// Load the Shift model
var Shift = require('../models/shiftModel');
var Template = require('../models/templateModel');
var moment = require('moment');
var mongoose = require('mongoose');
var ObjectID = require('mongodb').ObjectID;

// Get options from config file
var config = require('../config');

// Check if shifts have been created for the current week
exports.checkShifts = function (req, res, next) {
  var query = getFriday(moment());
  Shift.find(query, function (err1, results1) {
    if (err1) {console.log(err1);}
    // If there are no shifts for this week, create them
    if (!results1.length) {
      Template.findOne({}, null, {sort: {version: -1}}, function (err2, results2) {
        if (err2) {console.log(err2);}
        if (!results2) {console.log("No templates were found. Please create a template first");}
        Template.find({version: results2.version}, function (err3, results3) {
          var i, j, k, l, m;
          for (i = 0; i < results3.length; i++) {
            j = results3[i].nSpots; k = results3[i].nExec;
            Shift.create({date: query.date, index: results3[i].index, time: results3[i].time, nVol: (j-k), nExec: k}, function (err4, results4) {
              if (err4) {console.log(err4);}
            });
          }
          console.log("Creating new shifts for this Friday");
        });
      });
    }
  });
  return next();
};

exports.getShifts = function (req, res, next) {
  // http://mongoosejs.com/docs/populate.html
  var query = getFriday(moment());
  var shifts = Shift.find(query).populate({path: 'Vol', select: '_id firstName lastNameInitial profilePicture'}).populate({path: 'Exec', select: '_id firstName lastNameInitial profilePicture'}).exec(function (err, results) {
    if (err) {console.log(err)}
    res.json(results);
  });
};

exports.volunteer = function (req, res, next) {
  console.log("req.body was", req.body);
  var shiftID = req.body.shiftID;
  // Just quit if the ObjectID isn't valid
  if (!mongoose.Types.ObjectId.isValid(shiftID)) {
    console.log("shiftID was invalid");
    return next;
  }
  // Quit if shifts aren't open
  if (!shouldWrite()) {
    console.log("shouldWrite was false (exiting)");
    return next;
  }
  var rightNow = moment();
  var query = getFriday(rightNow);
  query.Vol = [];
  query.Vol[0] = req.user._id;
  console.log(query);
  // Check if the volunteer already has a shift this week
  Shift.findOne(query, function (err, result) {
    if (err) {console.log(err);}
    console.log("result was", result);
    if (!result) {
      // Check that there actually is a spot available
      Shift.findOne({"_id": ObjectID(shiftID)}, function (err0, result0) {
        var nVolNow = result0.Vol.length;
        if (nVolNow < result0.nVol) {
          var uQuery = getFriday(rightNow); // Ensure shift is for this week
          uQuery["_id"] = ObjectID(shiftID);
          Shift.update(uQuery, {$push:{"Vol":req.user._id}}, function (err1, results1) {
            if (err1) {console.log(err1)};
            console.log(results1);
          });
        } else {
          console.log("No more volunteering spots left");
        }
      });
    }
  });
  return next;
};

function getFriday(now) {
  if (now.day() < 5 && now.day() > 0) {
    query = {"date" : now.day(5).startOf('day').toDate()};
  } else if (now.day() === 5) {
    query = {"date" : now.startOf('day').toDate()};
  } else if (now.day() === 6 || now.day() === 0) {
    query = {"date" : now.day(-2).startOf('day').toDate()};
  } else {
    console.log("Could not interpret day of week in checkShifts. Had now.day() = ", now.day());
    return;
  }
	return query;
};

function shouldWrite() {
  var now = moment();
  if (now.day() < 5 && now.day() > 0) {
    return true;
  } else if (now.day() === 5 && now.hour() < 17) {
    return true;
  } else if (now.day() === 5 && now.hour() >= 17) {
    return false;
  } else if (now.day() === 6 || now.day() === 0) {
    return false;
  } else {
    console.log("Could not interpret time in shouldWrite. Had now.day() = ", now.day(), "and now.hour() = ", now.hour());
    return false;
  }
};

@@ -493,32 +493,39 @@
 %0A    if (err1) %7B
+return 
 console.log(err1
@@ -524,24 +524,24 @@
 log(err1);%7D%0A
-
     // If th
@@ -712,24 +712,31 @@
  if (err2) %7B
+return 
 console.log(
@@ -767,16 +767,23 @@
 ults2) %7B
+return 
 console.
@@ -1235,16 +1235,23 @@
 (err4) %7B
+return 
 console.
@@ -1767,32 +1767,39 @@
 %7B%0A    if (err) %7B
+return 
 console.log(err)
@@ -1885,49 +1885,8 @@
 ) %7B%0A
-  console.log(%22req.body was%22, req.body);%0A
   va
@@ -2314,30 +2314,8 @@
 id;%0A
-  console.log(query);%0A
   //
@@ -2430,16 +2430,23 @@
  (err) %7B
+return 
 console.
@@ -2456,50 +2456,11 @@
 (err
-);%7D%0A    console.log(%22result was%22, result
 );
+%7D
 %0A   
@@ -2919,16 +2919,23 @@
 (err1) %7B
+return 
 console.
@@ -2970,16 +2970,31 @@
 ole.log(
+%22Added shift%22, 
 results1
@@ -3109,32 +3109,32 @@
 %7D);%0A    %7D%0A  %7D);%0A
-
   return next;%0A%7D
@@ -3130,16 +3130,18 @@
 urn next
+()
 ;%0A%7D;%0A%0Afu

Fix redirect after adding shift, fix #5