package io.corbel.resources.rem.restor;

import java.io.InputStream;
import java.util.Base64;
import java.util.Optional;

import javax.ws.rs.core.Response;

import io.corbel.lib.ws.api.error.ErrorResponseFactory;
import io.corbel.resources.rem.model.RestorResourceUri;
import io.corbel.resources.rem.request.RequestParameters;
import io.corbel.resources.rem.dao.RestorDao;
import io.corbel.resources.rem.request.ResourceId;
import io.corbel.resources.rem.request.ResourceParameters;
import io.corbel.resources.rem.model.RestorObject;

/**
 * @author Rubén Carrasco
 */
public class RestorPutRem extends AbstractRestorRem {

	public RestorPutRem(RestorDao dao) {
		super(dao);
	}

	@Override
	public Response resource(String collection, ResourceId resource, RequestParameters<ResourceParameters> parameters,
			Optional<InputStream> entity) {
		if (!entity.isPresent()) {
			return ErrorResponseFactory.getInstance().badRequest();
		}
		try {
			String encoding = parameters.getCustomParameterValue("resource:encoding");
			InputStream stream = entity.get();
			Long contentLength = parameters.getContentLength();
			if (encoding != null) {
				switch (encoding) {
					case "base64":
						String length = parameters.getCustomParameterValue("resource:length");
						if (length == null) {
							return ErrorResponseFactory.getInstance().badRequest();
						}
						contentLength = Long.valueOf(length);
						stream = Base64.getDecoder().wrap(entity.get());
						break;
				}
			}
            RestorResourceUri resourceUri = new RestorResourceUri(parameters.getRequestedDomain(), getMediaType(parameters), collection, resource.getId());

            dao.uploadObject(resourceUri, new RestorObject(getMediaType(parameters), stream, contentLength));
			return Response.noContent().build();
		} catch (NumberFormatException e) {
			return ErrorResponseFactory.getInstance().badRequest();
		}
	}
}

@@ -617,17 +617,20 @@
 rRem %7B%0A%0A
-%09
+    
 public R
@@ -658,18 +658,24 @@
  dao) %7B%0A
-%09%09
+        
 super(da
@@ -682,13 +682,19 @@
 o);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -699,17 +699,20 @@
 verride%0A
-%09
+    
 public R
@@ -818,19 +818,28 @@
 meters,%0A
-%09%09%09
+            
 Optional
@@ -862,18 +862,24 @@
 tity) %7B%0A
-%09%09
+        
 if (!ent
@@ -893,27 +893,36 @@
 resent()) %7B%0A
-%09%09%09
+            
 return Error
@@ -969,23 +969,44 @@
 ();%0A
-%09%09%7D%0A%09%09try %7B%0A%09%09%09
+        %7D%0A        try %7B%0A            
 Stri
@@ -1076,19 +1076,28 @@
 ding%22);%0A
-%09%09%09
+            
 InputStr
@@ -1123,19 +1123,28 @@
 .get();%0A
-%09%09%09
+            
 Long con
@@ -1187,19 +1187,28 @@
 ngth();%0A
-%09%09%09
+            
 if (enco
@@ -1223,23 +1223,27 @@
 null
-) %7B%0A%09%09%09%09switch 
+ && %22base64%22.equals
 (enc
@@ -1252,37 +1252,28 @@
 ing)
+)
  %7B%0A
-%09%09%09%09%09case %22base64%22:%0A%09%09%09%09%09%09
+                
 Stri
@@ -1339,22 +1339,32 @@
 ngth%22);%0A
-%09%09%09%09%09%09
+                
 if (leng
@@ -1381,15 +1381,28 @@
 ) %7B%0A
-%09%09%09%09%09%09%09
+                    
 retu
@@ -1457,22 +1457,42 @@
 ();%0A
-%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09
+                %7D%0A                
 cont
@@ -1525,22 +1525,32 @@
 ength);%0A
-%09%09%09%09%09%09
+                
 stream =
@@ -1594,30 +1594,20 @@
 ));%0A
-%09%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09
+            
 %7D%0A  
@@ -1871,19 +1871,28 @@
 ngth));%0A
-%09%09%09
+            
 return R
@@ -1920,18 +1920,24 @@
 uild();%0A
-%09%09
+        
 %7D catch 
@@ -1968,11 +1968,20 @@
 ) %7B%0A
-%09%09%09
+            
 retu
@@ -2036,13 +2036,22 @@
 ();%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%7D%0A

refactor switch into if