package edu.asu.scrapbook.digital.api;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
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 edu.asu.scrapbook.digital.dao.ImageDAO;
import edu.asu.scrapbook.digital.dao.ImageDAOFactory;
import edu.asu.scrapbook.digital.dao.UserDAO;
import edu.asu.scrapbook.digital.dao.UserDAOFactory;
import edu.asu.scrapbook.digital.model.Image;
import edu.asu.scrapbook.digital.model.User;
import edu.asu.scrapbook.digital.util.ImageUtil;
import edu.asu.scrapbook.digital.util.UserUtil;

@Path("/images")
public class ImageResource {
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Image> getUserImageIds() {
		String username = UserUtil.getRequestUsername();
		
		ImageDAO imageDao = ImageDAOFactory.getInstance();
		UserDAO userDao = UserDAOFactory.getInstance();
		try {
			User user = userDao.findById(username);
			List<Image> imageIds = null;
			// user should never be null
			if (user != null) {
				imageIds = imageDao.getAllImagesByUser(user);
			}
			
			return imageIds;
			
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
	
	@PUT
	@Path("{id}/edit")
	@Consumes(MediaType.APPLICATION_OCTET_STREAM)
	public void editImage(byte[] imageData, @PathParam("id") long id) {
		String username = "";// UserUtil.getRequestUsername();
		
		ImageDAO imageDao = ImageDAOFactory.getInstance();
		UserDAO userDao = UserDAOFactory.getInstance();
		try {
			User user = userDao.findById(username);
			List<Image> userImages = imageDao.getAllImagesByUser(user);
			
			for (Image image : userImages) {
				if (image.getId() == id) {
					ImageUtil.updateEditedImage(image, imageData);
					break;
				}
			}
			
		} catch (Exception e) {
			throw new InternalServerErrorException(e);
		}
	}
}

@@ -1384,32 +1384,18 @@
 ype.
-APPLICATION_OC
 TE
+X
 T_
-STREAM
+PLAIN
 )%0D%0A%09
@@ -1420,16 +1420,22 @@
 age(
-byte%5B%5D i
+String base64I
 mage
@@ -1492,14 +1492,8 @@
 me =
- %22%22;//
  Use
@@ -1517,32 +1517,112 @@
 sername();%0D%0A%09%09%0D%0A
+%09%09byte%5B%5D imageData = DatatypeConverter.parseBase64Binary(base64ImageData);%0D%0A%09%09%0D%0A
 %09%09ImageDAO image
@@ -1954,16 +1954,16 @@
 Data);%0D%0A
-
 %09%09%09%09%09bre
@@ -2068,11 +2068,850 @@
 %09%7D%0D%0A%09%7D%0D%0A
+%09%0D%0A%09@DELETE%0D%0A%09@Path(%22%7Bid%7D%22)%0D%0A%09public void delete(@PathParam(%22id%22) long id) %7B%0D%0A%09%09String username = UserUtil.getRequestUsername();%0D%0A%09%09%0D%0A%09%09ImageDAO imageDao = ImageDAOFactory.getInstance();%0D%0A%09%09UserDAO userDao = UserDAOFactory.getInstance();%0D%0A%09%09try %7B%0D%0A%09%09%09User user = userDao.findById(username);%0D%0A%09%09%09if (user != null) %7B%0D%0A%09%09%09%09List%3CImage%3E images = imageDao.getAllImagesByUser(user);%0D%0A%09%09%09%09Image image = images.get(0);%0D%0A%09%09%09%09for (Iterator%3CImage%3E iter = images.iterator(); iter.hasNext(); image = iter.next()) %7B%0D%0A%09%09%09%09%09if (image.getId() == id) %7B%0D%0A%09%09%09%09%09%09BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();%0D%0A%09%09%09%09%09%09bs.delete(image.getBlobKey());%0D%0A%09%09%09%09%09%09%0D%0A%09%09%09%09%09%09iter.remove();%0D%0A%09%09%09%09%09%09userDao.update(user);%0D%0A%09%09%09%09%09%09%0D%0A%09%09%09%09%09%09break;%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09throw new InternalServerErrorException(e);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Change edit image to accept base64 encoded image data instead of raw image data. Implemented delete.