package com.example.helloworld.resources;

import io.dropwizard.hibernate.UnitOfWork;
import io.dropwizard.jersey.errors.ErrorMessage;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.example.helloworld.core.Token;
import com.example.helloworld.core.User;
import com.example.helloworld.db.UserDAO;
import com.example.helloworld.util.AuthUtils;
import com.google.common.base.Optional;

@Path("/api/me")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {
	
	private final UserDAO dao;
	
	public UserResource(UserDAO userDAO) {
		this.dao = userDAO;
	}

	@GET
	@UnitOfWork
	public Response getUser(@Context HttpServletRequest request) throws ParseException {
		Optional<User> foundUser = getAuthUser(request);
		
		if (!foundUser.isPresent()) {
			return Response.status(Status.NOT_FOUND).build();
		}
		return Response.ok().entity(foundUser.get()).build();
	}
	
	// for testing
	@GET
	@Path("/all")
	@UnitOfWork
	public Response getAllUsers() {
		return Response.ok().entity(dao.findAll()).build();
	}

	@PUT
	@UnitOfWork
	public Response updateUser(@Valid User user, @Context HttpServletRequest request) throws ParseException {
		String authHeader = request.getHeader(AuthUtils.AUTH_HEADER_KEY);
		Optional<User> foundUser = getAuthUser(authHeader);
		
		if (!foundUser.isPresent()) {
			return Response
					.status(Status.NOT_FOUND)
					.entity(new ErrorMessage(AuthResource.NOT_FOUND_MSG)).build();
		}
		
		User userToUpdate = foundUser.get();
		userToUpdate.setDisplayName(user.getDisplayName());
		userToUpdate.setEmail(user.getEmail());
		dao.save(userToUpdate);
		
		Token token = new Token(AuthUtils.getSerializedToken(authHeader));
		return Response.ok().entity(token).build();
	}
	
	/*
	 * Helper methods
	 */
	private Optional<User> getAuthUser(String authHeader) throws ParseException {
		String subject = AuthUtils.getSubject(authHeader);
		return dao.findById(Long.parseLong(subject));
	}
	
	private Optional<User> getAuthUser(HttpServletRequest request) throws ParseException {
		String authHeader = request.getHeader(AuthUtils.AUTH_HEADER_KEY);
		return getAuthUser(authHeader);
	}

}

@@ -519,50 +519,8 @@
 s;%0A%0A
-import com.example.helloworld.core.Token;%0A
 impo
@@ -1482,36 +1482,55 @@
 ion %7B%0A%09%09
-String authHeader = 
+Optional%3CUser%3E foundUser = getAuthUser(
 request.
@@ -1569,61 +1569,8 @@
 KEY)
-;%0A%09%09Optional%3CUser%3E foundUser = getAuthUser(authHeader
 );%0A%09
@@ -1893,79 +1893,8 @@
 e);%0A
-%09%09%0A%09%09Token token = new Token(AuthUtils.getSerializedToken(authHeader));
 %0A%09%09r
@@ -1917,22 +1917,8 @@
 k().
-entity(token).
 buil

return 200 OK when updating user instead of sending token