package com.example.beer.services;

import java.net.HttpURLConnection;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.example.beer.dao.ProjectDAO;
import com.example.beer.dao.TaskDAO;
import com.example.beer.dao.UserDAO;
import com.example.beer.dto.TaskDTO;
import com.example.beer.model.Project;
import com.example.beer.model.Task;
import com.example.beer.model.User;

@Stateless
@Path("task")
public class TaskManager {

	@Inject
	ProjectDAO projectDAO;

	@Inject
	TaskDAO taskDAO;

	@Inject
	UserDAO userDAO;

	@Inject
	UserContext userContext;

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createTask(TaskDTO task) {
		User currentUser = userContext.getCurrentUser();
		if (currentUser == null || !currentUser.isAdmin()) {
			return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
		}

		User assignee = userDAO.getUserById(task.getAssigneeId());

		if (assignee == null) {
			return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();
		}

		Project project = projectDAO.findProjectById(task.getProjectId());
		if (project == null) {
			return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();
		}

		String name = task.getName();
		if (!taskDAO.addTask(name, task.getDescription(), task.getFinalDate(),
				task.getEstimatedHours(), project, assignee)) {
			return Response.status(HttpURLConnection.HTTP_NOT_ACCEPTABLE)
					.build();
		}

		Task foundTask = taskDAO.findTaskByName(name);

		if (taskDAO.addToProject(project, foundTask)) {
			return Response.status(HttpURLConnection.HTTP_OK).build();
		} else {
			return Response.status(HttpURLConnection.HTTP_NOT_ACCEPTABLE)
					.build();
		}
	}

	@POST
	@Path("assign/{taskId}/{userId}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response assignUser(@PathParam("taskId") int taskId,
			@PathParam("userId") int assigneeId) {
		User currentUser = userContext.getCurrentUser();
		if (currentUser == null) {
			return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
		}

		if (taskDAO.assignUser(taskId, assigneeId)) {
			return Response.status(HttpURLConnection.HTTP_OK).build();
		}

		return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();
	}

	// @POST
	// @Consumes(MediaType.APPLICATION_JSON)
	// public Response changeStatus(Task task, TaskStatus status) {
	// User currentUser = userContext.getCurrentUser();
	//
	// if (currentUser == null) {
	// return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
	// }
	//
	// if (!task.getAssignee().equals(currentUser) && !currentUser.isAdmin()) {
	// return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).build();
	// }
	//
	// if (taskDAO.changeStatus(task, status)) {
	// return Response.status(HttpURLConnection.HTTP_OK).build();
	// }
	//
	// return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
	// }
	//
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response findTaskById(int taskId) {
		User currentUser = userContext.getCurrentUser();

		if (currentUser == null) {
			return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
		}

		Task foundTask = taskDAO.getTaskById(taskId);

		if (foundTask == null) {
			return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();
		}

		return Response.ok(foundTask).build();
	}

}

@@ -2434,18 +2434,16 @@
 %7D%0A%0A%09
-//
  @POST%0A%09
 // @
@@ -2438,18 +2438,16 @@
  @POST%0A%09
-//
  @Consum
@@ -2478,18 +2478,16 @@
 _JSON)%0A%09
-//
  public 
@@ -2541,18 +2541,16 @@
 tus) %7B%0A%09
-//
  User cu
@@ -2592,22 +2592,18 @@
 ser();%0A%09
-//%0A%09//
+%0A%09
  if (cur
@@ -2619,26 +2619,24 @@
 == null) %7B%0A%09
-//
  return Resp
@@ -2690,36 +2690,30 @@
 ).build();%0A%09
-//
  %7D%0A%09
-//%0A%09//
+%0A%09
  if (!task.g
@@ -2771,26 +2771,24 @@
 Admin()) %7B%0A%09
-//
  return Resp
@@ -2845,28 +2845,22 @@
 ild();%0A%09
-//
  %7D%0A%09
-//%0A%09//
+%0A%09
  if (tas
@@ -2895,18 +2895,16 @@
 us)) %7B%0A%09
-//
  return 
@@ -2960,20 +2960,14 @@
 );%0A%09
-//
  %7D%0A%09
-//%0A%09//
+%0A%09
  ret
@@ -3037,16 +3037,12 @@
 );%0A%09
-//
  %7D%0A%09
-//
 %0A%09@G

fix createTask