//var app = require('../index');
var db = require('../app/config');
var Tracker = require('../app/model/track');

module.exports = function(app) {
  app.get('/', function(req, res) {
    res.redirect('index.html');
  });

  app.post('/api/all', function(req, res) {
    console.log('REQ BODY: ', req.body)
    Tracker.find({userID: req.body.userID}, function(err, tracks) {
      respObj = []
      tracks.forEach(function(object){
        object.activities.forEach(function(activity){
          inputObj = {
            category: '',
            activity: '',
            time: []
          };
          inputObj.category = object.category;
          inputObj.activity = activity.name;
          function calculateTime(timeArray){
            if(timeArray.length % 2 === 0){
              for(var i = 0; i<timeArray.length; i+=2){
                inputObj.time.push([timeArray[i], timeArray[i +1]])
              }
            } else {
              for(var i = 0; i<timeArray.length-1; i+=2){
                inputObj.time.push([timeArray[i], timeArray[i +1]])
              }
            }
          }
          calculateTime(activity.time)
          respObj.push(inputObj);
        })
      })
      res.send(200, respObj);
    })
  })

  app.post('/api/toggleActivity', function(req, res) {
    console.log('SERVER: in server post handler');
    
    Tracker.findOne({'category': req.body.category, 'activities.name': req.body.activity}, function(err, track) {
      if (err) {
        console.error('Stop time error: ', err);
      } else if(track === null) {
        var dbObj = new Tracker({
          userID: '001',
          category: req.body.category,
          activities: [{
            name: req.body.activity,
            time: [req.body.time]
          }]
        });
        dbObj.save(function(err) {
          if (err) { console.error("Start time error: ", err); } 
          else { res.send(200); } 
        });
      } else {
        var activities = track.activities;
        activities[0].time.push(req.body.time);
        track.save(function(err) {
          if (err) {
            console.error("Start time error: ", err);
          } else {
            if(activities[0].time.length % 2 === 0){
              var array = activities[0].time
              var time1 = array[array.length -1]
              var time2 = array[array.length -2]
              var duration = time1 - time2;
              console.log('duration: ', duration)
              var respObj = {
                category: req.body.category,
                activity: req.body.activity,
                duration: duration
              }
              res.send(200, respObj) 
            } else{
              res.send(200);
            }
          }
        });
      }
    });
  });
}

          

@@ -1340,20 +1340,16 @@
 dler');%0A
-    
 %0A    Tra
@@ -1869,25 +1869,24 @@
 : %22, err); %7D
- 
 %0A          e
@@ -1907,17 +1907,16 @@
 (200); %7D
- 
 %0A       
@@ -2652,17 +2652,16 @@
 respObj)
- 
 %0A       
@@ -2766,18 +2766,465 @@
 %7D);%0A
-%7D%0A%0A          
+%0A  app.post('/api/goals', function(req, res) %7B%0A    console.log(req.body.userID)%0A    if (!req.body) %7B%0A      return res.sendStatus(404);%0A    %7D%0A    var goal = %7B%0A      userID: req.body.userID,%0A      goalCategory: req.body.goalCategory,%0A      goalActivityName: req.body.goalActivityName,%0A      goalTime: req.body.goalTime%0A    %7D%0A    res.status(201).send(JSON.stringify(goal));%0A  %7D)%0A%0A  app.get('/api/goals', function(req, res) %7B%0A    return res.sendStatus(200);%0A  %7D)%0A%7D
 %0A

Add server route for posting goals.