package ch.heigvd.amt.moussaraser.rest.resources;

import ch.heigvd.amt.moussaraser.model.entities.ApiKey;
import ch.heigvd.amt.moussaraser.model.entities.Badge;
import ch.heigvd.amt.moussaraser.rest.dto.BadgeDTO;
import ch.heigvd.amt.moussaraser.services.dao.ApiKeyDAOLocal;
import ch.heigvd.amt.moussaraser.services.dao.ApplicationDAOLocal;
import ch.heigvd.amt.moussaraser.services.dao.BadgeDAOLocal;
import javax.ejb.EJB;
import javax.ejb.Stateless;
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.QueryParam;
import javax.ws.rs.core.Response;
import ch.heigvd.amt.moussaraser.rest.config.SendResponse;
import ch.heigvd.amt.moussaraser.web.utils.EncryptionManager;

@Stateless
@Path("/badges")
public class BadgeResource {

   @EJB
   BadgeDAOLocal badgeDAO;
   
   @EJB
   ApplicationDAOLocal applicationDAO;
   
   @EJB
   ApiKeyDAOLocal apiKeyDAO;

   @GET
   @Produces("application/json")
   public Response getBadges(@QueryParam("apiKey") String apiKey) {
      
      if(apiKey == null || apiKey.length() != EncryptionManager.getAPIKey().length()) {
         return SendResponse.errorApiKeyNotProvided();
      }
      
      ApiKey key = apiKeyDAO.findByApiKeyString(apiKey);
      
      if(key == null) {
         return SendResponse.errorApiKeyInvalid();
      }
      
      return SendResponse.send200OK(badgeDAO.getBadgesByApiKey(key));
   }
   
   @POST
   @Consumes("application/json")
   @Produces("application/json")
   public Response createBadge(Badge b, @QueryParam("apiKey") String apiKey) {
      
      if(apiKey == null || apiKey.length() != EncryptionManager.getAPIKey().length()) {
         return SendResponse.errorApiKeyNotProvided();
      }
      
      ApiKey key = apiKeyDAO.findByApiKeyString(apiKey);
      
      if(key == null) {
         return SendResponse.errorApiKeyInvalid();
      }
      
      b.setApplication(applicationDAO.getApplicationByApiKey(key));
      
      badgeDAO.create(b);
      
      return SendResponse.send200OK(new BadgeDTO(b.getId(), b.getName(), b.getCategory(), b.getDescription(), b.getImage()));
   }
   
   @GET
   @Path("/{id}")
   @Produces("application/json")
   public Response getBadge(@PathParam("id") long id, @QueryParam("apiKey") String apiKey) {
      
      if(apiKey == null || apiKey.length() != EncryptionManager.getAPIKey().length()) {
         return SendResponse.errorApiKeyNotProvided();
      }
      
      ApiKey key = apiKeyDAO.findByApiKeyString(apiKey);
      
      if(key == null) {
         return SendResponse.errorApiKeyInvalid();
      }
      
      return SendResponse.send200OK(badgeDAO.getBadgeByIdAndByApiKey(id, key));
   }
}

@@ -794,16 +794,67 @@
 Manager;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A@State
@@ -1453,32 +1453,333 @@
 %0A      %7D%0A      %0A
+      List%3CBadge%3E badges = badgeDAO.getBadgesByApiKey(key);%0A      List%3CBadgeDTO%3E badgesDTO = new ArrayList%3C%3E();%0A      %0A      for(Badge badge : badges) %7B%0A         badgesDTO.add(new BadgeDTO(badge.getId(), badge.getName(), badge.getCategory(), badge.getDescription(), badge.getImage()));%0A      %7D%0A      %0A
       return Sen
@@ -1807,34 +1807,12 @@
 adge
-DAO.getBadgesByApiKey(key)
+sDTO
 );%0A 

fix bug when send all badges (use now DTO)