'use strict';

// Module dependencies.
var _ = require('lodash');
var fs = require('fs');
var mongoose = require('mongoose');
var path = require('path');

var errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
var User = mongoose.model('User');

/*
 * Function to validate if properties parse when signing up are empty.
 * Returns error message for properties that are empty.
 */
var updateUser = function(req, res, query, profile) {
  if (req.user) {
    User.findOneAndUpdate(query, profile, {new: true}, function(err, user) {
      if (err) {
        res.status(400).send(errorHandler.getErrorResponse(2));
      } else if (!user) {
        res.status(400).send(errorHandler.getErrorResponse(501));
      } else {
        res.json(user);
      }
    });
  } else {
    res.status(401).send(errorHandler.getErrorResponse(502));
  }
};

/*
 * Retrieve user profile.
 */
exports.read = function(req, res) {
  res.jsonp(req.profile);
};

/*
 * Update user profile.
 */
exports.updateProfile = function(req, res) {
  var query = req.body._id;
  // Only these properties will be updated.
  var properties = ['link', 'location', 'name'];

  var profile = _.pick(req.body, properties);
  profile.updated = Date.now();

  updateUser(req, res, query, profile);
};

/*
 * Update profile picture.
 */
exports.changeProfilePicture = function(req, res) {
  var user = req.user;

  if (user) {
    fs.writeFile('./modules/users/client/img/profile/uploads/' +
                 req.files.file.name, req.files.file.buffer, function(uploadError) {
      if (uploadError) {
        return res.status(400).send({
          message: 'Error occurred while uploading profile picture'
        });
      } else {
        user.imageUrl = 'modules/users/img/profile/uploads/' + req.files.file.name;

        user.save(function(saveError) {
          if (saveError) {
            return res.status(400).send({
              message: errorHandler.getErrorMessage(saveError)
            });
          } else {
            req.login(user, function(err) {
              if (err) {
                res.status(400).send(err);
              } else {
                res.json(user);
              }
            });
          }
        });
      }
    });
  } else {
    res.status(400).send({
      message: 'User is not signed in.'
    });
  }
};

@@ -482,26 +482,30 @@
 %7B%0A  
-if (req.user) %7B%0A  
+console.log(profile);%0A
   Us
@@ -575,26 +575,24 @@
 user) %7B%0A    
-  
 if (err) %7B%0A 
@@ -588,34 +588,32 @@
 f (err) %7B%0A      
- 
- 
 res.status(400).
@@ -652,26 +652,24 @@
 se(2));%0A    
-  
 %7D else if (!
@@ -678,26 +678,24 @@
 er) %7B%0A      
- 
- 
 res.status(4
@@ -740,26 +740,24 @@
 (501));%0A    
-  
 %7D else %7B%0A   
@@ -755,34 +755,35 @@
 lse %7B%0A      
- 
- res.json
+console.log
 (user);%0A    
@@ -788,94 +788,35 @@
     
-%7D%0A    %7D);%0A  %7D else %7B%0A    res.status(401).send(errorHandler.getErrorResponse(502));%0A  %7D
+res.json(user);%0A    %7D%0A  %7D);
 %0A%7D;%0A
@@ -997,34 +997,63 @@
 %7B%0A  
-var query = req.body
+if (req.user) %7B%0A    var query = %7B_id: req.user
 ._id
+%7D
 ;%0A
+  
   //
@@ -1092,16 +1092,18 @@
 pdated.%0A
+  
   var pr
@@ -1144,16 +1144,18 @@
 ame'%5D;%0A%0A
+  
   var pr
@@ -1192,16 +1192,18 @@
 rties);%0A
+  
   profil
@@ -1227,16 +1227,18 @@
 now();%0A%0A
+  
   update
@@ -1261,32 +1261,109 @@
 uery, profile);%0A
+  %7D else %7B%0A    res.status(401).send(errorHandler.getErrorResponse(502));%0A  %7D%0A
 %7D;%0A%0A/*%0A * Update

Fix update profile bug.