var express = require('express');
var Label = require('../models/label');
var bodyParser = require('body-parser');
// API router
var router = express.Router();
var jsonParser = bodyParser.json();
var fs = require('fs');
var multer  = require('multer');
var AM = require('../modules/account-manager');
var mongoose = require('mongoose');

var storage = multer.diskStorage({
  destination: function (request, file, callback) {
    callback(null, 'public/uploads/');
  },
  filename: function (request, file, callback) {
    console.log(request);
    callback(null, file.originalname)
  }
});

var upload = multer({storage: storage}).single('file');

router.route('/upload')
.post(upload, function(req, res) {
  // find the image_id in the database
  // If the file is not in the database, update it!
  Label.find({"image_id" : req.file.originalname}, function(err, label) {
    if(err)
      res.send(err);

    if(typeof label !== 'undefined' && label.length > 0) {
      console.log('File existed!');
      res.end('file existed');
    } else {
        var label = new Label();
        label.user_id = "";
        label.image_id = req.file.originalname;
        label.labels = "";
        label.save(function(err){
           if (err)
           res.send(err);
           return;
        });
        console.log('DB is updated!');
        res.end('Images Uploaded');
        return;
    }
  });
});

router.route('/explore')
.get(function(req, res) {
    Label.find(function(err, label) {
        if(err)
            res.send(err);
        res.json(label);
    })
});

/* Save the labels */
router.route('/saveLabels')
.post(jsonParser, function(req, res) {

    console.log(req.body.Labels);
    var str = req.body.img.split('/');
    var imgName = str[str.length - 1];
    console.log({"image_id" : imgName});
    Label.findOneAndUpdate({"image_id" : imgName}, {
        "user_id" : "",
        "image_id" : imgName,
        "labels" : req.body.Labels
    }, {upsert: true, new: true},
    function(err, numberAffected, raw){
        console.log(err, numberAffected, raw)
    });
});

router.route('/upload')
.get(function(req, res) {
    Label.find(function(err, label) {
        if(err)
            res.send(err);
        res.json(label);
    })
});

router.route('/explore')
.post(jsonParser, function(req, res) {
    /* not used now!! */
    /* find the class in mongoDB and return the file name */
    /* Use file name to test the function now */
    Label.find(function(err, label) {
        if(err)
            res.send(err);
        return res.send('hi');
    })
});


/* Signup process */
router.route('/login')
.post(jsonParser, function(req, res) {

    AM.manualLogin(req.body.username, req.body.password, function(e, o){
			if (!o){
                // Send the error message.
				res.status(400).send(e);
			}	else{
				res.json(o);
			}
		});
});

/* logout process */
router.route('/logout')
.post(jsonParser, function(req, res) {


});



module.exports = router;
@@ -2579,14 +2579,13 @@
 %0A/* 
-Signup
+Login
  pro
@@ -2859,22 +2859,22 @@
 %7D);%0A%0A/* 
-logout
+Signup
  process
@@ -2896,14 +2896,14 @@
 e('/
-logout
+signUp
 ')%0A.
@@ -2941,22 +2941,221 @@
 res) %7B%0A%0A
+    AM.addNewAccount(%7B%0A%09%09%09user : req.body.user_name,%0A            pass : req.body.password%0A%09%09%7D, function(e)%7B%0A%09%09%09if (e)%7B%0A%09%09%09%09res.status(400).send(e);%0A%09%09%09%7D%09else%7B%0A%09%09%09%09res.status(200).send('ok');%0A%09%09%09%7D%0A%09%09%7D);
 %0A%7D);
-%0A%0A
 %0A%0Amodule

Fix the signup