/* global require, module, process, console, __dirname */
/* jshint -W097 */
'use strict';

module.exports = function(router) {

  var
    Media   = require('../models/Media'),
    apiUtil = require('../../../lib/apiUtil');


  router.route('/media')

    // get all the media (accessed at GET http://localhost:port/api/media)
    .get(function(req, res) {
      Media.find(function(err, models) {

        /* TODO: don't know how to trigger this from the tests */
        /* istanbul ignore next */
        if (err) {
          res.status(500).json(apiUtil.getErrorResponse(500, null, err));
          return;
        }

        res.json(models);
      });
    })


    // create a media (accessed at POST http://localhost:port/api/media)
    .post(function(req, res) {

      // create a new instance of the Media model
      var model = new Media();

      // set the media attributes
      model.name        = req.body.name;
      model.description = req.body.description;
      model.url         = req.body.url;
      model.active      = req.body.active;

      // save the media and check for errors
      model.save(function(err) {

        /* TODO: don't know how to trigger this from the tests */
        /* istanbul ignore next */
        if (err) {
          res.status(500).json(apiUtil.getErrorResponse(500, null, err));
          return;
        }

        res.json(model);
      });

    });


  router.route('/media/:media_id')

    // get the media with that id (accessed at GET http://localhost:port/api/media/:media_id)
    .get(function(req, res) {
      Media.findById(req.params.media_id, function(err, model) {

        /* TODO: don't know how to trigger this from the tests */
        /* istanbul ignore next */
        if (err) {
          res.status(500).json(apiUtil.getErrorResponse(500, null, err));
          return;
        }

        if (!model) {
          res.status(404).json(apiUtil.getErrorResponse(404));
          return;
        }

        res.json(model);
      });
    })


    // update the media with this id (accessed at PUT http://localhost:port/api/media/:media_id)
    .put(function(req, res) {

      // use our media model to find the media we want
      Media.findById(req.params.media_id, function(err, model) {

        /* TODO: don't know how to trigger this from the tests */
        /* istanbul ignore next */
        if (err) {
          res.status(500).json(apiUtil.getErrorResponse(500, null, err));
          return;
        }

        if (!model) {
          res.status(404).json(apiUtil.getErrorResponse(404));
          return;
        }

        // update the media info
        model.name        = req.body.name;
        model.description = req.body.description;
        model.url         = req.body.url;
        model.active      = req.body.active;

        // save the model
        model.save(function(err) {

          /* TODO: don't know how to trigger this from the tests */
          /* istanbul ignore next */
          if (err) {
            res.send(err);
            return;
          }

          res.json(model);
        });
      });
    })


    // delete the media with this id (accessed at DELETE http://localhost:port/api/media/:media_id)
    .delete(function(req, res) {
      var model = Media.findOne({
        _id: req.params.media_id
      });

      Media.findById(req.params.media_id, function(err, model) {

        /* TODO: don't know how to trigger this from the tests */
        /* istanbul ignore next */
        if (err) {
          res.status(500).json(apiUtil.getErrorResponse(500, null, err));
          return;
        }

        if (!model) {
          res.status(404).json(apiUtil.getErrorResponse(404));
          return;
        }

        model.remove(function() {
          res.json(model);
        });
      });
    });

};

@@ -174,23 +174,23 @@
 '),%0A    
-apiUtil
+Errors 
  = requi
@@ -206,23 +206,22 @@
 /../lib/
-apiUtil
+errors
 ');%0A%0A%0A  
@@ -337,32 +337,38 @@
 unction(req, res
+, next
 ) %7B%0A      Media.
@@ -412,188 +412,87 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A        /* istanbul ignore next */%0A        if (err) %7B%0A          res.status(500).json(apiUtil.getErrorResponse(500, null, 
+istanbul ignore next */%0A        if (err) %7B%0A          next( new Errors.App(
 err)
+ 
 );%0A 
@@ -656,32 +656,38 @@
 unction(req, res
+, next
 ) %7B%0A%0A      // cr
@@ -1058,218 +1058,84 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A        /* istanbul ignore next */%0A        if (err) %7B%0A          res.status(500).json(apiUtil.getErrorResponse(500, null, err));%0A          return;%0A        %7D
+istanbul ignore next */%0A        if (err) return next( new Errors.App(err) );
 %0A%0A  
@@ -1326,32 +1326,38 @@
 unction(req, res
+, next
 ) %7B%0A      Media.
@@ -1425,332 +1425,142 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A        /* istanbul ignore next */%0A        if (err) %7B%0A          res.status(500).json(apiUtil.getErrorResponse(500, null, err));%0A          return;%0A        %7D%0A%0A        if (!model) %7B%0A          res.status(404).json(apiUtil.getErrorResponse(404));%0A          return;%0A        %7D
+istanbul ignore next */%0A        if (err) return next( new Errors.App(err) );%0A        if (!model) return next( new Errors.NotFound() );
 %0A%0A  
@@ -1720,24 +1720,30 @@
 ion(req, res
+, next
 ) %7B%0A%0A      /
@@ -1871,332 +1871,142 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A        /* istanbul ignore next */%0A        if (err) %7B%0A          res.status(500).json(apiUtil.getErrorResponse(500, null, err));%0A          return;%0A        %7D%0A%0A        if (!model) %7B%0A          res.status(404).json(apiUtil.getErrorResponse(404));%0A          return;%0A        %7D
+istanbul ignore next */%0A        if (err) return next( new Errors.App(err) );%0A        if (!model) return next( new Errors.NotFound() );
 %0A%0A  
@@ -2296,179 +2296,68 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A          /* istanbul ignore next */%0A          if (err) %7B%0A            res.send(err);%0A            return;%0A          %7D
+istanbul ignore next */%0A          if (err) return next(err);
 %0A%0A  
@@ -2541,16 +2541,22 @@
 req, res
+, next
 ) %7B%0A    
@@ -2710,332 +2710,142 @@
  /* 
-TODO: don't know how to trigger this from the tests */%0A        /* istanbul ignore next */%0A        if (err) %7B%0A          res.status(500).json(apiUtil.getErrorResponse(500, null, err));%0A          return;%0A        %7D%0A%0A        if (!model) %7B%0A          res.status(404).json(apiUtil.getErrorResponse(404));%0A          return;%0A        %7D
+istanbul ignore next */%0A        if (err) return next( new Errors.App(err) );%0A        if (!model) return next( new Errors.NotFound() );
 %0A%0A  

Implement a generic error handling mechanism for the API