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

import com.altamiracorp.lumify.core.model.artifactHighlighting.TermMentionOffsetItem;
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.graph.InMemoryGraphVertex;
import com.altamiracorp.lumify.core.model.ontology.LabelName;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.core.model.ontology.PropertyName;
import com.altamiracorp.lumify.core.model.termMention.TermMentionModel;
import com.altamiracorp.lumify.core.model.termMention.TermMentionRowKey;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;

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

public class EntityTermCreate extends BaseRequestHandler {
    private final EntityHelper entityHelper;
    private final GraphRepository graphRepository;
    private final AuditRepository auditRepository;
    private final OntologyRepository ontologyRepository;

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

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        // required parameters
        final String artifactId = getRequiredParameter(request, "artifactId");
        final long mentionStart = getRequiredParameterAsLong(request, "mentionStart");
        final long mentionEnd = getRequiredParameterAsLong(request, "mentionEnd");
        final String sign = getRequiredParameter(request, "sign");
        final String conceptId = getRequiredParameter(request, "conceptId");

        User user = getUser(request);
        TermMentionRowKey termMentionRowKey = new TermMentionRowKey(artifactId, mentionStart, mentionEnd);

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

        final GraphVertex artifactVertex = graphRepository.findVertex(artifactId, user);
        final GraphVertex createdVertex = new InMemoryGraphVertex();
        createdVertex.setProperty(PropertyName.ROW_KEY, termMentionRowKey.toString());

        // TODO: replace second "" when we implement commenting on ui
        entityHelper.updateGraphVertex(createdVertex, conceptId, sign, "", "", user);

        // TODO: replace second "" when we implement commenting on ui
        auditRepository.auditEntity(AuditAction.CREATE.toString(), createdVertex.getId(), artifactId, sign, conceptId ,"", "", user);
        auditRepository.auditEntityProperties(AuditAction.UPDATE.toString(), createdVertex, PropertyName.ROW_KEY.toString(), "", "", user);

        graphRepository.saveRelationship(artifactId, createdVertex.getId(), LabelName.HAS_ENTITY, user);

        String labelDisplayName = ontologyRepository.getDisplayNameForLabel(LabelName.HAS_ENTITY.toString(), user);
        // TODO: replace second "" when we implement commenting on ui
        auditRepository.auditRelationships(AuditAction.CREATE.toString(), artifactVertex, createdVertex, labelDisplayName, "", "", user);

        TermMentionModel termMention = new TermMentionModel(termMentionRowKey);
        entityHelper.updateTermMention(termMention, sign, conceptVertex, createdVertex, user);

        // Modify the highlighted artifact text in a background thread
        entityHelper.scheduleHighlight(artifactId, user);

        TermMentionOffsetItem offsetItem = new TermMentionOffsetItem(termMention, createdVertex);
        respondWithJson(response, offsetItem.toJson());
    }
}

@@ -261,203 +261,8 @@
 ry;%0A
-import com.altamiracorp.lumify.core.model.graph.GraphRepository;%0Aimport com.altamiracorp.lumify.core.model.graph.GraphVertex;%0Aimport com.altamiracorp.lumify.core.model.graph.InMemoryGraphVertex;%0A
 impo
@@ -748,16 +748,151 @@
 rChain;%0A
+import com.altamiracorp.securegraph.Graph;%0Aimport com.altamiracorp.securegraph.Vertex;%0Aimport com.altamiracorp.securegraph.Visibility;%0A
 import c
@@ -1142,34 +1142,14 @@
 raph
-Repository graphRepository
+ graph
 ;%0A  
@@ -1364,26 +1364,16 @@
 al Graph
-Repository
  graphRe
@@ -1552,26 +1552,16 @@
 is.graph
-Repository
  = graph
@@ -1815,16 +1815,100 @@
 ption %7B%0A
+        // TODO set visibility%0A        Visibility visibility = new Visibility(%22%22);%0A%0A
         
@@ -2478,21 +2478,16 @@
         
-Graph
 Vertex c
@@ -2502,39 +2502,28 @@
 rtex = graph
-Repository.find
+.get
 Vertex(conce
@@ -2524,32 +2524,52 @@
 (conceptId, user
+.getAuthorizations()
 );%0A%0A        fina
@@ -2566,29 +2566,24 @@
       final 
-Graph
 Vertex artif
@@ -2603,23 +2603,12 @@
 raph
-Repository.find
+.get
 Vert
@@ -2622,24 +2622,44 @@
 factId, user
+.getAuthorizations()
 );%0A        f
@@ -2663,21 +2663,16 @@
   final 
-Graph
 Vertex c
@@ -2690,32 +2690,34 @@
 x = 
-new InMemoryGraphVertex(
+graph.addVertex(visibility
 );%0A 
@@ -2769,16 +2769,27 @@
 .ROW_KEY
+.toString()
 , termMe
@@ -2810,16 +2810,28 @@
 String()
+, visibility
 );%0A%0A    
@@ -3174,10 +3174,10 @@
 ptId
- 
 ,
+ 
 %22%22, 
@@ -3345,97 +3345,91 @@
 raph
-Repository.saveRelationship(artifactId, createdVertex.getId(), LabelName.HAS_ENTITY, user
+.addEdge(createdVertex, artifactVertex, LabelName.HAS_ENTITY.toString(), visibility
 );%0A%0A

fix entity term create