  /*
 * api Routes
 */

var async = require('async');

var Workout = require('../data/models/workout');
var notLoggedIn = require('./middleware/not_logged_in');
var loadWorkout = require('./middleware/load_workout');
var loggedIn = require('./middleware/logged_in');
var maxWorkoutsPerPage = 5;
var Exercise = require('../data/models/exercise');
var loadExercise = require('./middleware/load_exercise');
var Moment = require('moment');

module.exports = function(app) {

  app.get('/api/exerciselist', function(req, res) {
      res.contentType('json');
      Exercise.find()
              .exec(function(err, exercises) {
                if (err) {
                  return next(err);
                }
                if (! exercises) {
                  return res.send(JSON.stringify({ "error": "true" }));
                }
               res.send(exercises);
          });

  });

  app.get('/api/getexercisemodifier/:userid/:exerciseid', function(req, res) {
      res.contentType('json');
      console.log('userID' + req.params.userid);
      console.log('exerciseID' + req.params.exerciseid);
      //DB search workouts
          // where userid
          //where exerciseid

    Workout
    .where('userID', req.params.userid)
    .where('elements.exerciseID', req.params.exerciseid)
    .select('elements')
    // .exec(callback);

    // workout.findOne({_id: req.params._id})
    // //.populate('elements.exerciseID')
    .exec(function(err, workout) {
      if (err) {
        return next(err);
      }
      if (! workout) {
        return res.send('Not found', 404);
      }
         //need for loop for workout as it returns an array
         var filteredarray = workout[0].elements.filter( function(i) {
          return i.exerciseID == req.params.exerciseid;
        });
         // then for each in filtered array  grab sets reps weight
         // punch that into an array.
         
      // res.send(filteredarray);
      // //console.log("hmm" + JSON.stringify(workout[0].elements));
      res.send(workout);
      //next();
    });


  });

};