"use strict"

const jwt = require('jsonwebtoken'),
      crypto = require('crypto'),
      User = require('../models/user'),
      Recipe = require('../models/recipe'),
      config = require('../libs/config'),
      randomstring = require("randomstring"),
      status = require('../status');

// Set user info from request
function setUserInfo(request) {
  let getUserInfo = {
    _id: request._id,
    firstName: request.profile.firstName,
    lastName: request.profile.lastName,
    email: request.email,
    role: request.role
  };

  return getUserInfo;
}

exports.filteredArticles = function(req, res, next) {

}

exports.allArticles = function(req, res, next) {
  Recipe.find({}, (err, recipes) => {
    if (err) { return next(err); }
    return res.status(200).send({recipes: recipes});
  });
}

exports.getOneById = function(req, res, next) {
  Recipe.findById(req.params.id, (err, oneUser) => {
    if (err) {
      console.log(req.params.id)
      console.log("error here")
      return next(err);
    }
    return res.status(200).send({user: oneUser});
  });

}

exports.editById = function(req, res, next) {
  const newMesage = req.body.message;
  Recipe.findOne({_id: req.params.id}, (err, oneUser) => {
    if (err) { return next(err); }
    if (!newMesage) {
      return res.status(204).json({ message: 'Nothing to edit' });
    }
    let newRecipe = new Recipe({
    title         : newRecipe.title,
    intro         : newRecipe.intro,
    txt           : newRecipe.txt,
    price         : newRecipe.price,
    coverimg      : newRecipe.coverimg,
    symbolLink    : newRecipe.symbolLink,
    otherImg      : newRecipe.otherImg,
    diagramImg    : newRecipe.diagramImg
    })
    newRecipe.save( (err) => {
      if (err) {return res.status(400).send({message: 'Somthing went wrong' + err, status: 2345}); }
      return res.status(200).send({message: 'Accepted - recepie changed'})
    })
  });
}

exports.deleteById = function(req, res, next) {
  Recipe.findByIdAndRemove(req.params.id, (err, recipe) => {
    if (err) { return next(err); }
    return res.status(200).send({message: 'Accepted - recepie deleted', deleted: recipe})
  });
}

exports.addRecipe = function(req, res, next) {
  let userInfo = setUserInfo(req.user);
  let newRecipe = req.body;
  // Check that the new article is not empty.
  if ((typeof(newArticle) == 'undefined') ) {
        // console.log("\nUnprocessable Entity ERROR\n")
        return res.status(422);
  }
  // Check for bad recipe.
  if (newRecipe.title == '' || newRecipe.intro == '' || newRecipe.txt == '' ||
      !(typeof(parseInt(newRecipe.price)) == "number")) {
        console.error("Recipe format is wrong");
        return res.status(422).send({message: 'The recipe is not formated right, is the price a number? ' + newRecipe, status: 4567})
      }

    let theNewRecipe = new Recipe({
    title         : theNewRecipe.title,
    intro         : theNewRecipe.intro,
    txt           : theNewRecipe.txt,
    price         : theNewRecipe.price,
    coverimg      : theNewRecipe.coverimg,
    symbolLink    : theNewRecipe.symbolLink,
    otherImg      : theNewRecipe.otherImg,
    diagramImg    : theNewRecipe.diagramImg
  });

    // Save the new article
    theNewRecipe.save(function(err){
      if (err) {
        return res.status(400).send({message: 'Something is wrong. Could not add article: ' + err, status: 2345});
      }
      return res.status(200).send({message: 'Accepted - recepie added'})
    });

}

exports.recentlyViewed = function(req, res, next) {

}

@@ -1005,24 +1005,169 @@
 err);%0A    %7D%0A
+    if (oneUser == null) %7B%0A      console.log(%22- no user found%22)%0A      return res.status(406).send(%7Bmessage: %22Could not find the recipe%22%7D);%0A    %7D%0A
     return r
@@ -2345,24 +2345,54 @@
 es, next) %7B%0A
+  console.log(%22- processing%22)%0A
   let userIn
@@ -2419,16 +2419,38 @@
 .user);%0A
+  console.log(%22- OK%22)%0A
   let ne
@@ -2469,16 +2469,63 @@
 q.body;%0A
+  console.log(%22- OK%22)%0A  console.log(req.body);%0A
   // Che
@@ -2579,22 +2579,21 @@
 peof(new
-Articl
+Recip
 e) == 'u
@@ -2727,16 +2727,47 @@
 recipe.%0A
+  console.log(newRecipe.title)%0A
   if (ne
@@ -3086,16 +3086,39 @@
       %7D%0A
+    console.log(%22- OK%22)
 %0A    let
@@ -3138,32 +3138,72 @@
  = new Recipe(%7B%0A
+    strakkId      : newRecipe.strakkId,%0A
     title       
@@ -3198,36 +3198,33 @@
 title         : 
-theN
+n
 ewRecipe.title,%0A
@@ -3235,36 +3235,33 @@
 intro         : 
-theN
+n
 ewRecipe.intro,%0A
@@ -3272,36 +3272,33 @@
 txt           : 
-theN
+n
 ewRecipe.txt,%0A  
@@ -3307,36 +3307,33 @@
 price         : 
-theN
+n
 ewRecipe.price,%0A
@@ -3344,36 +3344,33 @@
 coverimg      : 
-theN
+n
 ewRecipe.coverim
@@ -3388,28 +3388,25 @@
 olLink    : 
-theN
+n
 ewRecipe.sym
@@ -3430,28 +3430,25 @@
 rImg      : 
-theN
+n
 ewRecipe.oth
@@ -3474,20 +3474,17 @@
 mg    : 
-theN
+n
 ewRecipe

add addrecipe route and controller