package io.lumify.web.routes.vertex;

import com.google.inject.Inject;
import io.lumify.core.config.Configuration;
import io.lumify.core.exception.LumifyException;
import io.lumify.core.model.ontology.OntologyProperty;
import io.lumify.core.model.ontology.OntologyRepository;
import io.lumify.core.model.user.UserRepository;
import io.lumify.core.model.workspace.WorkspaceRepository;
import io.lumify.core.user.User;
import io.lumify.core.util.GraphUtil;
import io.lumify.core.util.LumifyLogger;
import io.lumify.core.util.LumifyLoggerFactory;
import io.lumify.miniweb.HandlerChain;
import io.lumify.web.BaseRequestHandler;
import io.lumify.web.clientapi.model.SandboxStatus;
import io.lumify.web.routes.workspace.WorkspaceHelper;
import org.securegraph.Authorizations;
import org.securegraph.Graph;
import org.securegraph.Property;
import org.securegraph.Vertex;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static org.securegraph.util.IterableUtils.toList;

public class VertexDeleteProperty extends BaseRequestHandler {
    private static final LumifyLogger LOGGER = LumifyLoggerFactory.getLogger(VertexDeleteProperty.class);
    private final Graph graph;
    private final WorkspaceHelper workspaceHelper;
    private final OntologyRepository ontologyRepository;

    @Inject
    public VertexDeleteProperty(
            final Graph graph,
            final WorkspaceHelper workspaceHelper,
            final UserRepository userRepository,
            final WorkspaceRepository workspaceRepository,
            final Configuration configuration,
            final OntologyRepository ontologyRepository
    ) {
        super(userRepository, workspaceRepository, configuration);
        this.graph = graph;
        this.workspaceHelper = workspaceHelper;
        this.ontologyRepository = ontologyRepository;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        final String graphVertexId = getAttributeString(request, "graphVertexId");
        final String propertyName = getRequiredParameter(request, "propertyName");
        final String propertyKey = getRequiredParameter(request, "propertyKey");

        User user = getUser(request);
        Authorizations authorizations = getAuthorizations(request, user);
        String workspaceId = getActiveWorkspaceId(request);
        OntologyProperty ontologyProperty = ontologyRepository.getPropertyByIRI(propertyName);

        Vertex graphVertex = graph.getVertex(graphVertexId, authorizations);
        final List<Property> properties = new ArrayList<>();

        properties.addAll(toList(graphVertex.getProperties(propertyKey, propertyName)));
        if (ontologyProperty != null) {
            for (String dependentPropertyIri : ontologyProperty.getDependentPropertyIris()) {
                properties.addAll(toList(graphVertex.getProperties(propertyKey, dependentPropertyIri)));
            }
        }

        if (properties.size() == 0) {
            LOGGER.warn("Could not find property %s:%s on %s", propertyName, propertyKey, graphVertexId);
            respondWithNotFound(response);
            return;
        }

        SandboxStatus[] sandboxStatuses = GraphUtil.getPropertySandboxStatuses(properties, workspaceId);

        Property property = null;
        boolean propertyIsPublic = false;
        for (int i = 0; i < sandboxStatuses.length; i++) {
            if (property != null) {
                throw new LumifyException("Found multiple non public properties.");
            }
            property = properties.get(i);
            if (sandboxStatuses[i] == SandboxStatus.PUBLIC) {
                propertyIsPublic = true;
            }
        }

        if (property == null) {
            LOGGER.warn("Could not find property %s:%s on %s", propertyName, propertyKey, graphVertexId);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            chain.next(request, response);
            return;
        }

        workspaceHelper.deleteProperty(graphVertex, property, propertyIsPublic, workspaceId, user, authorizations);
        respondWithSuccessJson(response);
    }
}

@@ -112,57 +112,8 @@
 on;%0A
-import io.lumify.core.exception.LumifyException;%0A
 impo
@@ -3349,325 +3349,97 @@
     
-Property property = null;%0A        boolean propertyIsPublic = false;%0A        for (int i = 0; i %3C sandboxStatuses.length; i++) %7B%0A            if (property != null) %7B%0A                throw new LumifyException(%22Found multiple non public properties.%22);%0A            %7D%0A            property = properties.get(i);%0A            if
+for (int i = 0; i %3C sandboxStatuses.length; i++) %7B%0A            boolean propertyIsPublic =
  (sa
@@ -3479,18 +3479,17 @@
 .PUBLIC)
- %7B
+;
 %0A       
@@ -3497,342 +3497,51 @@
     
-    p
+P
 roperty
-IsPublic = true;%0A            %7D%0A        %7D%0A%0A        if (property == null) %7B%0A            LOGGER.warn(%22Could not find property %25s:%25s on %25s%22, propertyName, propertyKey, graphVertexId);%0A            response.sendError(HttpServletResponse.SC_BAD_REQUEST);%0A            chain.next(request, response);%0A            return;%0A        %7D%0A%0A
+ property = properties.get(i);%0A    
     
@@ -3644,32 +3644,43 @@
 uthorizations);%0A
+        %7D%0A%0A
         respondW

support deleting compound properties