package org.fiteagle.proprietary.rest;

import java.util.List;

import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
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 org.fiteagle.api.usermanagement.Course;
import org.fiteagle.api.usermanagement.UserManager;
import org.fiteagle.api.usermanagement.UserManager.CourseNotFoundException;
import org.fiteagle.api.usermanagement.UserManager.UserNotFoundException;
import org.fiteagle.proprietary.rest.UserPresenter.FiteagleWebApplicationException;

@Path("/course")
public class CoursePresenter {
  
private UserManager manager;
  
  public CoursePresenter() throws NamingException{
    final Context context = new InitialContext();
    manager = (UserManager) context.lookup("java:global/usermanagement/JPAUserManager!org.fiteagle.api.usermanagement.UserManager");
  }
  
  @GET
  @Path("{id}")
  @Produces(MediaType.APPLICATION_JSON)
  public Course get(@PathParam("id") long id) {
  try {
      return manager.get(id);
    } catch (EJBException e) {
      if(e.getCausedByException() instanceof CourseNotFoundException){
        throw new FiteagleWebApplicationException(404, e.getMessage());
      }
      return null;
    }   
  }
  
  @PUT
  @Path("")
  @Consumes(MediaType.APPLICATION_JSON)
  public Response add(Course course) {
//    try {
      manager.add(course);
//    } catch(EJBException e){
//      if(e.getCausedByException() instanceof DuplicateUsernameException){
//      throw new FiteagleWebApplicationException(409, e.getMessage());
//      }
//      else if(e.getCausedByException() instanceof DuplicateEmailException){
//        throw new FiteagleWebApplicationException(409, e.getMessage());
//      }
//      else if(e.getCausedByException() instanceof DuplicatePublicKeyException){
//        throw new FiteagleWebApplicationException(409, e.getMessage());
//      }
//      if(e.getCausedByException() instanceof InValidAttributeException || e.getCausedByException() instanceof NotEnoughAttributesException){
//        throw new FiteagleWebApplicationException(422, e.getMessage());
//      }
//    } catch(NotEnoughAttributesException | InValidAttributeException e){
//        throw new FiteagleWebApplicationException(422, e.getMessage());
//    }
    return Response.status(201).build();
  }
  
  @GET
  @Path("")
  @Produces(MediaType.APPLICATION_JSON)
  public List<Course> getAllCourses(){
    return manager.getAllCourses();
  }
  
  @POST
  @Path("{id}/participant/{username}")
  public Response addParticipant(@PathParam("id") long id, @PathParam("username") String username) {    
    try{
      manager.addParticipant(id, username);
    } catch(EJBException e){
      if(e.getCausedByException() instanceof UserNotFoundException){
        throw new FiteagleWebApplicationException(404, e.getMessage());
      }
      if(e.getCausedByException() instanceof CourseNotFoundException){
        throw new FiteagleWebApplicationException(404, e.getMessage());
      }
    }
    
    return Response.status(200).build();
  }
  

  
  
}

@@ -1527,39 +1527,69 @@
 ION_JSON)%0A  
-public Response
+@Produces(MediaType.TEXT_PLAIN)%0A  public long
  add(Course 
@@ -1620,28 +1620,8 @@
     
-manager.add(course);
 %0A// 
@@ -2526,43 +2526,60 @@
     
-return Response.status(201).build()
+long id = manager.add(course).getId();%0A    return id
 ;%0A  

return generated id when persisting courses