package edu.asu.scrapbook.digital.api;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import edu.asu.scrapbook.digital.dao.UserDAO;
import edu.asu.scrapbook.digital.dao.UserDAOFactory;
import edu.asu.scrapbook.digital.model.User;
import edu.asu.scrapbook.digital.util.UserUtil;

@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {
	
	@GET
	public User getUser() {
		String username = UserUtil.getRequestUsername();
		
		UserDAO dao = UserDAOFactory.getInstance();
		try {
			User user = dao.findById(username);
			if (user == null) {
				user = new User();
				user.setUsername(username);
			}
			
			return user;
			
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public User createUser(User user) {
		String username = UserUtil.getRequestUsername();
		
		if (user == null || user.getSettings() == null) {
			throw new BadRequestException();
		}
		
		user.setUsername(username);
		
		UserDAO dao = UserDAOFactory.getInstance();
		try {
			if (UserUtil.userExists(user.getUsername())) {
				throw new BadRequestException("A user already exists with that username");
			}
			
			return dao.create(user);
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
	
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	public User updateUser(User updatedUser) {
		String username = UserUtil.getRequestUsername();
		
		if (updatedUser == null) {
			throw new BadRequestException();
		}
		
		UserDAO dao = UserDAOFactory.getInstance();
		try {
			User user = dao.findById(username);
			if (user == null) {
				throw new NotFoundException();
			}
			
			if (!user.getUsername().equals(updatedUser.getUsername())) {
				if (UserUtil.userExists(updatedUser.getUsername())) {
					throw new BadRequestException("A user already exists with that username");
				}
				user.setUsername(updatedUser.getUsername());
			}
			
			if (updatedUser.getSettings() != null) {
				user.setSettings(updatedUser.getSettings());
			}
			
			return dao.update(user);
			
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
	
	@DELETE
	public void deleteUser() {
		String username = UserUtil.getRequestUsername();
		
		UserDAO dao = UserDAOFactory.getInstance();
		try {
			// TODO: delete images
			dao.delete(username);
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
}

@@ -2047,281 +2047,8 @@
 %09%09%0D%0A
-%09%09%09if (!user.getUsername().equals(updatedUser.getUsername())) %7B%0D%0A%09%09%09%09if (UserUtil.userExists(updatedUser.getUsername())) %7B%0D%0A%09%09%09%09%09throw new BadRequestException(%22A user already exists with that username%22);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09user.setUsername(updatedUser.getUsername());%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A
 %09%09%09i
@@ -2080,32 +2080,32 @@
 s() != null) %7B%0D%0A
+
 %09%09%09%09user.setSett
@@ -2270,16 +2270,18 @@
 %09%7D%0D%0A%09%0D%0A%09
+/*
 @DELETE%0D
@@ -2554,15 +2554,17 @@
 ;%0D%0A%09%09%7D%0D%0A
-
 %09%7D
+*/
 %0D%0A%7D%0D%0A

Comment out the method for deleting user since it isn't used, and remove the ability to change a user's username