import express from 'express';
import {Router} from 'express';
import {OK, NO_CONTENT, BAD_REQUEST, FORBIDDEN, NOT_FOUND, CONFLICT} from './es6-http-status-codes';
import bcrypt from 'bcrypt-nodejs'
import jwt from 'jsonwebtoken'
import { v4 as uuid} from 'node-uuid'
import UserService from './../service/UserService'

let user = new Router();
let userService = new UserService();

let secret = process.env.SECRET || 'here is my secret';

user.put('/login', (request, response) => {
	let {name, password} = request.body;

	if (!name || !password) {
		response.status(BAD_REQUEST).json();
	}

	userService
		.findUser(name)
		.then(user => {
			if (!user) {
				response.status(NOT_FOUND).json();
				return;
			}

			if (bcrypt.compareSync(password, user.password)) {
				// TODO filip(30/03/2016): save token in db / if token exist and not expired return token
				// if (user.token) jwt.verify -> if expired -> renew -> return token
				// else jwt.sign() -> return token + add to db async
				// TODO filip(30/03/2016): if expired issue new token / on logout invalidate token

				let data = {name: user.name};
				let issuedToken = jwt.sign(data, secret, {expiresIn: 24 * 60 * 60, jwtid: name + uuid()});

				response.status(OK).json({name: user.name, token: issuedToken});
			} else {
				response.status(FORBIDDEN).json();
			}
		})
		.catch(error => response.status(BAD_REQUEST).json());
});


//user.put('/logout', (request, response) => {
//	let {name} = request.body;
//
//	if (!name) {
//		response.status(BAD_REQUEST).json();
//	}
//
//	//userService
//	//	.findUser(name)
//	//	.then(value => {
//	//		//unset session / token !!!
//	//		response.status(OK).json(value);
//	//	})
//	//	.catch(() => {
//	//		response.status(BAD_REQUEST).json();
//	//	})
//	response.status(OK).json();
//});


user.post('/register', (request, response) => {
	let {name, password} = request.body;

	if (!name || !password) {
		response.status(BAD_REQUEST).json();
	}

	userService
		.findUser(name)
		.then(user => {
			if (!user) {
				let hashPassword = bcrypt.hashSync(password);

				userService
					.addUser(name, hashPassword)
					.then(user => response.status(OK).json({name: name}))
					.catch(error => response.status(BAD_REQUEST).json());
			} else {
				response.status(CONFLICT).json()
			}
		})
		.catch(error => response.status(BAD_REQUEST).json())

});


export {user}

@@ -108,27 +108,39 @@
 EN, 
-NOT_FOUND, CONFLICT
+CONFLICT, INTERNAL_SERVER_ERROR
 %7D fr
@@ -559,32 +559,36 @@
 ) %7B%0A%09%09response.s
+endS
 tatus(BAD_REQUES
@@ -581,39 +581,32 @@
 tus(BAD_REQUEST)
-.json()
 ;%0A%09%7D%0A%0A%09userServi
@@ -678,30 +678,29 @@
 se.s
+endS
 tatus(
-NOT_FOUND).json(
+BAD_REQUEST
 );%0A%09
@@ -1090,17 +1090,21 @@
 %09%09%09%09let 
-d
+userD
 ata = %7Bn
@@ -1151,17 +1151,21 @@
 wt.sign(
-d
+userD
 ata, sec
@@ -1250,31 +1250,30 @@
 ).json(%7B
-name
+user
 : user
-.name
+Data
 , token:
@@ -1310,24 +1310,28 @@
 %09%09response.s
+endS
 tatus(FORBID
@@ -1334,23 +1334,16 @@
 RBIDDEN)
-.json()
 ;%0A%09%09%09%7D%0A%09
@@ -1378,32 +1378,39 @@
 se.s
+endS
 tatus(
-BAD_REQUEST).json(
+INTERNAL_SERVER_ERROR
 ));%0A
@@ -1517,32 +1517,36 @@
 %7B%0A//%09%09response.s
+endS
 tatus(BAD_REQUES
@@ -1543,31 +1543,24 @@
 BAD_REQUEST)
-.json()
 ;%0A//%09%7D%0A//%0A//
@@ -1745,32 +1745,39 @@
 se.s
+endS
 tatus(
-BAD_REQUEST).json(
+INTERNAL_SERVER_ERROR
 );%0A/
@@ -1789,32 +1789,36 @@
 %7D)%0A//%09response.s
+endS
 tatus(OK).json()
@@ -1810,23 +1810,16 @@
 atus(OK)
-.json()
 ;%0A//%7D);%0A
@@ -1938,32 +1938,36 @@
 ) %7B%0A%09%09response.s
+endS
 tatus(BAD_REQUES
@@ -1968,23 +1968,16 @@
 REQUEST)
-.json()
 ;%0A%09%7D%0A%0A%09u
@@ -2222,32 +2222,36 @@
 or =%3E response.s
+endS
 tatus(BAD_REQUES
@@ -2252,23 +2252,16 @@
 REQUEST)
-.json()
 );%0A%09%09%09%7D 
@@ -2277,24 +2277,28 @@
 %09%09response.s
+endS
 tatus(CONFLI
@@ -2300,23 +2300,17 @@
 ONFLICT)
-.json()
+;
 %0A%09%09%09%7D%0A%09%09
@@ -2344,42 +2344,47 @@
 se.s
+endS
 tatus(
-BAD_REQUEST).json(
+INTERNAL_SERVER_ERROR
 ))%0A
-%0A
 %7D);%0A%0A
-%0A
 expo

change backend to return sendStatus method instead status+json