/*jslint node: true, sloppy: true, indent: 2 */

var restify = require('restify');
var deemjs = require(__dirname + '/deem.js');

var Rest = function (deem) {
  this.deem = deem;
};

Rest.prototype.getUser = function (req, res, next) {
  var userId = req.params.userId;
  this.deem.getUser(userId, function (obj) {
    console.log('getUser(): %j', obj);
    res.send(obj);
    next();
  });
};

Rest.prototype.putUser = function (req, res, next) {
  var deem = this.deem;
  var body = '';
  req.on('data', function (chunk) {
    console.log('got %d bytes of data', chunk.length);
    body += chunk;
  });
  req.on('end', function () {
      console.log('putUser(): body = %s', body);
      var user = JSON.parse(body);
      var userId = user.userId;
      var email = user.email;
      var password = user.password;

      deem.putUser(userId, email, password, function (obj) {
        console.log(obj);
        res.send(obj);
        next();
      });
  });
};

Rest.prototype.getUserRating = function (req, res, next) {
  var userId = req.params.userId;
  var itemType = req.params.itemType;
  var itemId = req.params.itemId;
  this.deem.getUserRating(userId, itemType, itemId, function (obj) {
    console.log(obj);
    res.end(JSON.stringify(obj));
  });
};

Rest.prototype.putUserRating = function (req, res, next) {
  var userId = req.params.userId;
  var itemType = req.params.itemType;
  var itemId = req.params.itemId;
  var rating = req.params.rating;
  this.deem.putUserRating(userId, itemType, itemId, rating, function (obj) {
    console.log(obj);
    res.end(JSON.stringify(obj));
  });
};

Rest.prototype.configure = function () {
  this.server = restify.createServer({name: 'deem'});
  this.server.pre(restify.pre.userAgentConnection());

  // setup routes
  var rest = this;

  this.server.get('/user/:userId', function (req, res, next) {
    rest.getUser(req, res, next);
  });

  this.server.put('/user/:userId', function (req, res, next) {
    rest.putUser(req, res, next);
  });

  this.server.get('/user/:userId/:itemType/:itemId', function (req, res, next) {
    rest.getUserRating(req, res, next);
  });

  this.server.put('/user/:userId/:itemType/:itemId/:rating', function (req, res, next) {
    rest.putUserRating(req, res, next);
  });

  return this;
};

Rest.prototype.listen = function (port) {
  var that = this;
  this.server.listen(port, function () {
    console.log('%s listening at %s -- bucket => \'%s\'', that.server.name, that.server.url, that.deem.bucket);
  });
  return this;
};

exports.createRest = function (riakHostname, riakPort, bucket) {
  var deem = deemjs.createDeem(riakHostname, riakPort, bucket);
  return new Rest(deem).configure();
};

@@ -180,220 +180,8 @@
 %7D;%0A%0A
-Rest.prototype.getUser = function (req, res, next) %7B%0A  var userId = req.params.userId;%0A  this.deem.getUser(userId, function (obj) %7B%0A    console.log('getUser(): %25j', obj);%0A    res.send(obj);%0A    next();%0A  %7D);%0A%7D;%0A%0A
 Rest
@@ -763,24 +763,236 @@
 type.getUser
+ = function (req, res, next) %7B%0A  var userId = req.params.userId;%0A  this.deem.getUser(userId, function (obj) %7B%0A    console.log('getUser(): %25j', obj);%0A    res.send(obj);%0A    next();%0A  %7D);%0A%7D;%0A%0ARest.prototype.putUser
 Rating = fun
@@ -1118,24 +1118,58 @@
 ams.itemId;%0A
+  var rating = req.params.rating;%0A
   this.deem.
@@ -1164,26 +1164,26 @@
   this.deem.
-ge
+pu
 tUserRating(
@@ -1207,16 +1207,24 @@
  itemId,
+ rating,
  functio
@@ -1306,34 +1306,34 @@
 %0ARest.prototype.
-pu
+ge
 tUserRating = fu
@@ -1468,42 +1468,8 @@
 Id;%0A
-  var rating = req.params.rating;%0A
   th
@@ -1468,34 +1468,34 @@
 Id;%0A  this.deem.
-pu
+ge
 tUserRating(user
@@ -1511,32 +1511,24 @@
 ype, itemId,
- rating,
  function (o
@@ -1793,34 +1793,34 @@
 %0A%0A  this.server.
-ge
+pu
 t('/user/:userId
@@ -1855,26 +1855,26 @@
  %7B%0A    rest.
-ge
+pu
 tUser(req, r
@@ -1897,34 +1897,34 @@
 %0A%0A  this.server.
-pu
+ge
 t('/user/:userId
@@ -1955,34 +1955,34 @@
 ext) %7B%0A    rest.
-pu
+ge
 tUser(req, res, 
@@ -2001,34 +2001,34 @@
 %0A%0A  this.server.
-ge
+pu
 t('/user/:userId
@@ -2045,16 +2045,24 @@
 /:itemId
+/:rating
 ', funct
@@ -2089,26 +2089,26 @@
  %7B%0A    rest.
-ge
+pu
 tUserRating(
@@ -2137,34 +2137,34 @@
 %0A%0A  this.server.
-pu
+ge
 t('/user/:userId
@@ -2177,32 +2177,24 @@
 Type/:itemId
-/:rating
 ', function 
@@ -2213,34 +2213,34 @@
 ext) %7B%0A    rest.
-pu
+ge
 tUserRating(req,

reorder get/put methods