package com.altamiracorp.lumify.web.routes.entity;

import com.altamiracorp.lumify.core.ingest.ArtifactDetectedObject;
import com.altamiracorp.lumify.core.model.audit.AuditAction;
import com.altamiracorp.lumify.core.model.audit.AuditRepository;
import com.altamiracorp.lumify.core.model.graph.GraphRepository;
import com.altamiracorp.lumify.core.model.graph.GraphVertex;
import com.altamiracorp.lumify.core.model.ontology.PropertyName;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;
import org.json.JSONArray;
import org.json.JSONObject;

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

public class EntityObjectDetectionCreate extends BaseRequestHandler {
    private final GraphRepository graphRepository;
    private final EntityHelper entityHelper;
    private final AuditRepository auditRepository;

    @Inject
    public EntityObjectDetectionCreate(
            final EntityHelper entityHelper,
            final GraphRepository graphRepository,
            final AuditRepository auditRepository) {
        this.entityHelper = entityHelper;
        this.graphRepository = graphRepository;
        this.auditRepository = auditRepository;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {

        // required parameters
        final String artifactId = getRequiredParameter(request, "artifactId");
        final String sign = getRequiredParameter(request, "sign");
        final String conceptId = getRequiredParameter(request, "conceptId");
        String x1 = getRequiredParameter(request, "x1"), x2 = getRequiredParameter(request, "x2"),
                y1 = getRequiredParameter(request, "y1"), y2 = getRequiredParameter(request, "y2");
        String existing = getOptionalParameter(request, "existing");
        final String boundingBox = "x1: " + x1 + ", y1: " + y1 + ", x2: " + x2 + ", y2: " + y2;

        User user = getUser(request);

        GraphVertex conceptVertex = graphRepository.findVertex(conceptId, user);
        GraphVertex artifactVertex = graphRepository.findVertex(artifactId, user);

        // create new graph vertex
        // TODO: replace second "" when we implement commenting on ui
        GraphVertex resolvedVertex = entityHelper.createGraphVertex(conceptVertex, sign, existing,"", "", artifactId, user);

        ArtifactDetectedObject newDetectedObject = entityHelper.createObjectTag(x1, x2, y1, y2, resolvedVertex, conceptVertex);

        // adding to detected object property if one exists, if not add detected object property to the artifact vertex
        JSONArray detectedObjectList = new JSONArray();
        if (artifactVertex.getPropertyKeys().contains(PropertyName.DETECTED_OBJECTS.toString())) {
            detectedObjectList = new JSONArray(artifactVertex.getProperty(PropertyName.DETECTED_OBJECTS).toString());
        }

        JSONObject result = new JSONObject();

        JSONObject entityVertex = newDetectedObject.getJson();
        entityVertex.put("artifactId", artifactId);
        detectedObjectList.put(entityVertex);
        artifactVertex.setProperty(PropertyName.DETECTED_OBJECTS, detectedObjectList.toString());
        graphRepository.saveVertex(resolvedVertex, user);

        // TODO: replace "" when we implement commenting on ui
        auditRepository.auditEntityProperties(AuditAction.UPDATE.toString(), artifactVertex, PropertyName.DETECTED_OBJECTS.toString(), "", "", user);

        result.put("entityVertex", entityVertex);

        JSONObject updatedArtifactVertex =
                entityHelper.formatUpdatedArtifactVertexProperty(artifactId, PropertyName.DETECTED_OBJECTS.toString(), artifactVertex.getProperty(PropertyName.DETECTED_OBJECTS));

        result.put("updatedArtifactVertex", updatedArtifactVertex);

        // TODO: index the new vertex

        respondWithJson(response, result);
    }
}

@@ -242,134 +242,8 @@
 ry;%0A
-import com.altamiracorp.lumify.core.model.graph.GraphRepository;%0Aimport com.altamiracorp.lumify.core.model.graph.GraphVertex;%0A
 impo
@@ -451,16 +451,151 @@
 rChain;%0A
+import com.altamiracorp.securegraph.Graph;%0Aimport com.altamiracorp.securegraph.Vertex;%0Aimport com.altamiracorp.securegraph.Visibility;%0A
 import c
@@ -866,34 +866,14 @@
 raph
-Repository graphRepository
+ graph
 ;%0A  
@@ -1087,26 +1087,16 @@
 al Graph
-Repository
  graphRe
@@ -1218,26 +1218,16 @@
 is.graph
-Repository
  = graph
@@ -1426,16 +1426,99 @@
 eption %7B
+%0A        // TODO set visibility%0A        Visibility visibility = new Visibility(%22%22);
 %0A%0A      
@@ -2173,29 +2173,24 @@
 );%0A%0A        
-Graph
 Vertex conce
@@ -2201,39 +2201,28 @@
 rtex = graph
-Repository.find
+.get
 Vertex(conce
@@ -2231,16 +2231,36 @@
 Id, user
+.getAuthorizations()
 );%0A     
@@ -2258,29 +2258,24 @@
 ));%0A        
-Graph
 Vertex artif
@@ -2295,23 +2295,12 @@
 raph
-Repository.find
+.get
 Vert
@@ -2310,32 +2310,52 @@
 artifactId, user
+.getAuthorizations()
 );%0A%0A        // c
@@ -2455,21 +2455,16 @@
         
-Graph
 Vertex r
@@ -2915,23 +2915,13 @@
 erty
-Keys().contains
+Value
 (Pro
@@ -2957,17 +2957,28 @@
 String()
-)
+, 0) != null
 ) %7B%0A    
@@ -3038,32 +3038,37 @@
 rtex.getProperty
+Value
 (PropertyName.DE
@@ -3081,17 +3081,16 @@
 _OBJECTS
-)
 .toStrin
@@ -3084,32 +3084,36 @@
 JECTS.toString()
+, 0)
 );%0A        %7D%0A%0A  
@@ -3382,16 +3382,27 @@
 _OBJECTS
+.toString()
 , detect
@@ -3428,66 +3428,20 @@
 ng()
-);%0A        graphRepository.saveVertex(resolvedVertex, user
+, visibility
 );%0A%0A
@@ -3885,32 +3885,37 @@
 rtex.getProperty
+Value
 (PropertyName.DE
@@ -3928,16 +3928,30 @@
 _OBJECTS
+.toString(), 0
 ));%0A%0A   

fix create object detection