package io.lumify.web.routes.vertex;

import com.google.inject.Inject;
import io.lumify.core.config.Configuration;
import io.lumify.core.model.audit.AuditAction;
import io.lumify.core.model.audit.AuditRepository;
import io.lumify.core.model.properties.LumifyProperties;
import io.lumify.core.model.user.UserRepository;
import io.lumify.core.model.workQueue.WorkQueueRepository;
import io.lumify.core.model.workspace.WorkspaceRepository;
import io.lumify.core.security.VisibilityTranslator;
import io.lumify.core.user.User;
import io.lumify.core.util.GraphUtil;
import io.lumify.core.util.JsonSerializer;
import io.lumify.core.util.LumifyLogger;
import io.lumify.core.util.LumifyLoggerFactory;
import io.lumify.miniweb.HandlerChain;
import io.lumify.web.BaseRequestHandler;
import org.json.JSONObject;
import org.securegraph.Authorizations;
import org.securegraph.Graph;
import org.securegraph.Vertex;
import org.securegraph.Visibility;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class VertexSetVisibility extends BaseRequestHandler {
    private static final LumifyLogger LOGGER = LumifyLoggerFactory.getLogger(VertexSetVisibility.class);
    private final Graph graph;
    private final VisibilityTranslator visibilityTranslator;
    private final WorkQueueRepository workQueueRepository;
    private final AuditRepository auditRepository;

    @Inject
    public VertexSetVisibility(
            final Graph graph,
            final UserRepository userRepository,
            final Configuration configuration,
            final VisibilityTranslator visibilityTranslator,
            final WorkspaceRepository workspaceRepository,
            final WorkQueueRepository workQueueRepository,
            final AuditRepository auditRepository) {
        super(userRepository, workspaceRepository, configuration);
        this.graph = graph;
        this.visibilityTranslator = visibilityTranslator;
        this.workQueueRepository = workQueueRepository;
        this.auditRepository = auditRepository;
    }

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

        User user = getUser(request);
        Authorizations authorizations = getAuthorizations(request, user);
        String workspaceId = getActiveWorkspaceId(request);

        Vertex graphVertex = graph.getVertex(graphVertexId, authorizations);
        if (graphVertex == null) {
            respondWithNotFound(response);
            return;
        }

        if (!graph.isVisibilityValid(new Visibility(visibilitySource), authorizations)) {
            LOGGER.warn("%s is not a valid visibility for %s user", visibilitySource, user.getDisplayName());
            respondWithBadRequest(response, "visibilitySource", getString(request, "visibility.invalid"));
            chain.next(request, response);
            return;
        }
        LOGGER.info("changing vertex (%s) visibility source to %s", graphVertex.getId(), visibilitySource);

        GraphUtil.VisibilityAndElementMutation<Vertex> setPropertyResult = GraphUtil.updateElementVisibilitySource(visibilityTranslator, graphVertex, GraphUtil.getSandboxStatus(graphVertex, workspaceId), visibilitySource, workspaceId, authorizations);
        auditRepository.auditVertexElementMutation(AuditAction.UPDATE, setPropertyResult.elementMutation, graphVertex, "", user, setPropertyResult.visibility.getVisibility());

        this.graph.flush();

        this.workQueueRepository.pushGraphPropertyQueue(graphVertex, null,
                LumifyProperties.VISIBILITY_JSON.getPropertyName(), workspaceId, visibilitySource);

        JSONObject json = JsonSerializer.toJson(graphVertex, workspaceId, authorizations);
        respondWithJson(response, json);
    }
}

@@ -543,25 +543,34 @@
 re.util.
-GraphUtil
+ClientApiConverter
 ;%0Aimport
@@ -590,30 +590,25 @@
 re.util.
-JsonSerializer
+GraphUtil
 ;%0Aimport
@@ -781,26 +781,44 @@
 ort 
-org.json.JSONObjec
+io.lumify.web.clientapi.model.Elemen
 t;%0Ai
@@ -2569,194 +2569,8 @@
 );%0A%0A
-        Vertex graphVertex = graph.getVertex(graphVertexId, authorizations);%0A        if (graphVertex == null) %7B%0A            respondWithNotFound(response);%0A            return;%0A        %7D%0A%0A
     
@@ -2945,16 +2945,556 @@
       %7D%0A
+%0A        Element element = handle(graphVertexId, visibilitySource, workspaceId, user, authorizations);%0A        if (element == null) %7B%0A            respondWithNotFound(response);%0A            return;%0A        %7D%0A        respondWith(response, element);%0A    %7D%0A%0A    private Element handle(String graphVertexId, String visibilitySource, String workspaceId, User user, Authorizations authorizations) %7B%0A        Vertex graphVertex = graph.getVertex(graphVertexId, authorizations);%0A        if (graphVertex == null) %7B%0A            return null;%0A        %7D%0A%0A
         
@@ -4240,47 +4240,45 @@
     
-JSONObject json = JsonSerializer.toJson
+return ClientApiConverter.toClientApi
 (gra
@@ -4321,49 +4321,8 @@
 s);%0A
-        respondWithJson(response, json);%0A
     

refactor set visibility to use jackson