/*
 * workout 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 loadUser = require('./middleware/load_user');
var maxWorkoutsPerPage = 5;
var Exercise = require('../data/models/exercise');
var loadExercise = require('./middleware/load_exercise');
var MuscleArray = require('./middleware/musclearray');
var Moment = require('moment');

module.exports = function(app) {

  app.get('/workouts/recent', loggedIn, function(req, res, next){
    var page = req.query.page  && parseInt(req.query.page, 10) || 0;
    async.parallel([

        function(next) {
          Workout.count(next);
        },

        function(next) {
          Workout.find({})
            //.sort('title', 1)
            .skip(page * maxWorkoutsPerPage)
            .limit(maxWorkoutsPerPage)
            .exec(next);
        }
      ],

      //  callback from async
      function(err, results) {

        if (err) {
          return next(err);
        }

        var count = results[0];
        var workouts = results[1];

        var lastPage = (page + 1) * maxWorkoutsPerPage >= count;

        res.render('workouts/recent', {
          title: 'workouts',
          workouts: workouts,
          page: page,
          lastPage: lastPage
        });

      }
    );
  });

  app.get('/workouts/new', loggedIn, function(req, res) {
    res.render('workouts/new', {title: "New workout"});
  });

app.get('/workouts/:name/recent', loadUser, function(req, res, next){
    req.user.recentworkouts(function(err, workouts) {
    if (err) {
    return next(err);
    }
    res.render('workouts/recent', {
    //user: req.user,
    //requested: req.params.name,
    recentworkouts: workouts
    });
    });
  });

  app.get('/workouts/:_id', loggedIn, loadWorkout, function(req, res, next){
    console.log("time" + Moment.utc(req.workout.workoutDate).format("ddd, hA"));
      var output = MuscleArray(req.workout);
      console.log('output - ' + JSON.stringify(output));

    res.render('workouts/workout', {title: req.workout.title,
      workout: req.workout,
      workouttime: Moment.utc(req.workout.workoutDate).format("ddd, hA")
      });
  });

  app.post('/workouts', loggedIn, function(req, res, next) {
    console.log("/nreq.body" + JSON.stringify(req.body));
    var workout = req.body;
    //workout.author = req.session.user._id;
    Workout.create(workout, function(err) {
      if (err) {
        if (err.code === 11000) {
          res.send('Conflict', 409);
        } else {
          if (err.name === 'ValidationError') {
            return res.send(Object.keys(err.errors).map(function(errField) {
              return err.errors[errField].message;
            }).join('. '), 406);
          } else {
            next(err);
          }
        }
        return;
      }
      res.redirect('/workouts');
    });
  });

  app.del('/workouts/:_id', loggedIn, loadWorkout, function(req, res, next) {
    req.workout.remove(function(err) {
      if (err) { return next(err); }
      res.redirect('/workouts');
    });

  });

};