var express = require('express');
var r = module.exports = express.Router();
var _ = require('lodash');

r.get('/session.json', function(req, res, next) {
  res.send(req.session);
});

var Board = require('./models/board');

r.get('/boards/:id', function(req, res, next) {
  Board.find({ id: '1' }, function(err, boards) {
    if (err) {
      res.send(500);
    } else if (boards.length === 0) {
      res.send(404);
    } else {
      res.send({ board: boards[0] });
    }
  });
});

// Deleting columns
r.delete('/boards/:id/columns/:col', function (req, res, next) {
  Board.find({ id: req.params.id }, function(err, boards) {
    if (err) {
      res.send(500);
    } else if (boards.length === 0) {
      res.send(404);
    } else {
      var board = boards[0];
      board.columns.splice(req.params.col, 1);
      Board.update({ _id: board._id }, { columns: board.columns }, function(err) {
        if (err) { res.send(500, err.message); }
        else { res.send({ board: board }); }
      });
    }
  });
});

// Deleting cards
r.delete('/boards/:id/columns/:col/cards/:row', function(req, res, next) {
  Board.find({ id: req.params.id }, function(err, boards) {
    if (err) {
      res.send(500);
    } else if (boards.length === 0) {
      res.send(404);
    } else {
      var board = boards[0];
      board.columns[req.params.col].cards.splice(req.params.row, 1);
      Board.update({ _id: board._id }, { columns: board.columns }, function(err) {
        if (err) { res.send(500, err.message); }
        else { res.send({ board: board }); }
      });
    }
  });
});

// Importing cards from Github
r.post('/boards/:id/columns/:col/cards/import/github', function(req, res, next) {
  Board.find({ id: req.params.id }, function(err, boards) {
    if (err) {
      res.send(500);
    } else if (boards.length === 0) {
      res.send(404);
    } else {
      var board = boards[0];
      _.each(req.body.openIssues, function(issue) {
        board.columns[req.params.col].cards.push(issue);
      });
      Board.update({ _id: board._id }, { columns: board.columns }, function(err) {
        if (err) { res.send(500, err.message); }
        else { res.send({ board: { columns: board.columns } }) }
      });
    }
  });
})

// Move a card
r.put('/boards/:id/columns/:col/cards/:row/move/:direction', function(req, res, next) {
  Board.find({ id: req.params.id }, function(err, boards) {
    if (err) {
      res.send(500);
    } else if (boards.length === 0) {
      res.send(404);
    } else {
      var board = boards[0];
      var Mover = function(popCard, done) {
        var directions = {
          left: function() {
            board.columns[parseInt(req.params.col)-1].cards.push(popCard());
            done();
          },
          right: function() {
            board.columns[parseInt(req.params.col)+1].cards.push(popCard());
            done();
          }
        };
        directions[req.params.direction]()
      };
      Mover(function() {
        return board.columns[req.params.col].cards.splice(req.params.row, 1)[0];
      }, function() {
        Board.update({ _id: board._id }, { columns: board.columns }, function(err) {
          if (err) { res.send(500, err.message); }
          else { res.send({ board: { columns: board.columns } }) }
        });
      })
    }
  });
});

@@ -2598,16 +2598,334 @@
 ons = %7B%0A
+          up: function() %7B%0A            board.columns%5BparseInt(req.params.col)%5D.cards.splice(req.params.row-1, 0, popCard());%0A            done();%0A          %7D,%0A          down: function() %7B%0A            board.columns%5BparseInt(req.params.col)%5D.cards.splice(req.params.row+1, 0, popCard());%0A            done();%0A          %7D,%0A
         

add up down movers