package io.corbel.resources.rem;

import io.corbel.lib.ws.api.error.ErrorResponseFactory;
import io.corbel.resources.rem.plugin.RestorRemNames;
import io.corbel.resources.rem.request.CollectionParameters;
import io.corbel.resources.rem.request.RequestParameters;
import io.corbel.resources.rem.request.ResourceId;
import io.corbel.resources.rem.request.ResourceParameters;
import io.corbel.resources.rem.service.RemService;
import io.corbel.resources.rem.util.ImageRemUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;

import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.Optional;

public class ImageDeleteRem extends BaseRem<InputStream> {

    private static final Logger LOG = LoggerFactory.getLogger(ImageDeleteRem.class);
    private final String cacheCollection;
    private final ImageRemUtil imageRemUtil;
    private RemService remService;

    public ImageDeleteRem(String cacheCollection, ImageRemUtil imageRemUtil) {
        this.cacheCollection = cacheCollection;
        this.imageRemUtil = imageRemUtil;
    }

    public void setRemService(RemService remService) {
        this.remService = remService;
    }

    @Override
    public Response collection(String collection, RequestParameters<CollectionParameters> requestParameters, URI uri,
                               Optional<InputStream> entity) {

        @SuppressWarnings("unchecked")
        Rem<InputStream> restorDeleteRem = remService.getRem(RestorRemNames.RESTOR_DELETE);

        if (restorDeleteRem == null) {
            LOG.warn("RESTOR not found. May  be is needed to install it?");
            return ErrorResponseFactory.getInstance().notFound();
        }

        restorDeleteRem.collection(collection, requestParameters, uri, Optional.empty());

        return Response.noContent().build();
    }

    @Override
    public Response resource(String collection, ResourceId resourceId, RequestParameters<ResourceParameters> requestParameters,
                             Optional<InputStream> entity) {

        @SuppressWarnings("unchecked")
        Rem<InputStream> restorDeleteRem = (Rem<InputStream>) remService.getRem(collection, requestParameters.getAcceptedMediaTypes(),
                HttpMethod.DELETE, Collections.singletonList(this));

        if (restorDeleteRem == null) {
            LOG.warn("RESTOR not found. May  be is needed to install it?");
            return ErrorResponseFactory.getInstance().notFound();
        }

        restorDeleteRem.collection(cacheCollection, imageRemUtil.getCollectionParametersWithPrefix(resourceId.getId(), requestParameters, cacheCollection),
                null, Optional.empty());
        restorDeleteRem.resource(collection, resourceId, requestParameters, Optional.empty());

        return Response.noContent().build();
    }

    @Override
    public Class<InputStream> getType() {
        return InputStream.class;
    }
}

@@ -471,107 +471,83 @@
 il;%0A
+%0A
 import 
-org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.http.HttpMethod
+java.io.InputStream;%0Aimport java.net.URI;%0Aimport java.util.Optional
 ;%0A%0Ai
@@ -583,112 +583,64 @@
 se;%0A
+%0A
 import 
-java.io.InputStream;%0Aimport java.net.URI;%0Aimport java.util.Collections;%0Aimport java.util.Optional
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ap
@@ -1314,35 +1314,16 @@
 RI uri,%0A
-                   
         
@@ -1968,33 +1968,16 @@
 meters,%0A
-                 
         
@@ -2099,166 +2099,54 @@
 m = 
-(Rem%3CInputStream%3E) remService.getRem(collection, requestParameters.getAcceptedMediaTypes(),%0A                HttpMethod.DELETE, Collections.singletonList(this)
+remService.getRem(RestorRemNames.RESTOR_DELETE
 );%0A%0A
@@ -2388,16 +2388,32 @@
 lection,
+%0A               
  imageRe
@@ -2508,16 +2508,22 @@
 ection),
+ null,
 %0A       
@@ -2530,22 +2530,16 @@
         
- null,
  Optiona

fix bug at delete image when using acl + rem image