package io.lumify.web.routes.artifact;

import com.altamiracorp.bigtable.model.user.ModelUserContext;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;
import io.lumify.core.EntityHighlighter;
import io.lumify.core.config.Configuration;
import io.lumify.core.model.properties.RawLumifyProperties;
import io.lumify.core.model.termMention.TermMentionModel;
import io.lumify.core.model.termMention.TermMentionRepository;
import io.lumify.core.model.user.UserRepository;
import io.lumify.core.model.workspace.WorkspaceRepository;
import io.lumify.core.user.User;
import io.lumify.web.BaseRequestHandler;
import org.apache.commons.io.IOUtils;
import org.securegraph.Authorizations;
import org.securegraph.Graph;
import org.securegraph.Vertex;
import org.securegraph.property.StreamingPropertyValue;

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

public class ArtifactHighlightedText extends BaseRequestHandler {
    private final Graph graph;
    private final TermMentionRepository termMentionRepository;
    private final EntityHighlighter entityHighlighter;
    private final UserRepository userRepository;

    @Inject
    public ArtifactHighlightedText(
            final Graph graph,
            final UserRepository userRepository,
            final TermMentionRepository termMentionRepository,
            final EntityHighlighter entityHighlighter,
            final WorkspaceRepository workspaceRepository,
            final Configuration configuration) {
        super(userRepository, workspaceRepository, configuration);
        this.graph = graph;
        this.termMentionRepository = termMentionRepository;
        this.entityHighlighter = entityHighlighter;
        this.userRepository = userRepository;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        User user = getUser(request);
        Authorizations authorizations = getAuthorizations(request, user);
        String workspaceId = getActiveWorkspaceId(request);
        ModelUserContext modelUserContext = userRepository.getModelUserContext(authorizations, workspaceId);

        String graphVertexId = getRequiredParameter(request, "graphVertexId");
        String propertyKey = getRequiredParameter(request, "propertyKey");

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

        StreamingPropertyValue textPropertyValue = RawLumifyProperties.TEXT.getPropertyValue(artifactVertex, propertyKey);
        if (textPropertyValue == null) {
            respondWithNotFound(response);
            return;
        }

        String highlightedText;
        String text = IOUtils.toString(textPropertyValue.getInputStream(), "UTF-8");
        if (text == null) {
            highlightedText = "";
        } else {
            Iterable<TermMentionModel> termMentions = termMentionRepository.findByGraphVertexIdAndPropertyKey(artifactVertex.getId().toString(), propertyKey, modelUserContext);
            highlightedText = entityHighlighter.getHighlightedText(text, termMentions);
        }

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        IOUtils.write(highlightedText, response.getOutputStream(), "UTF-8");
    }
}

@@ -259,16 +259,130 @@
 ration;%0A
+import io.lumify.core.ingest.video.VideoTranscript;%0Aimport io.lumify.core.model.properties.MediaLumifyProperties;%0A
 import i
@@ -2820,17 +2820,17 @@
 tyValue 
-=
+!
 = null) 
@@ -2839,78 +2839,8 @@
     
-        respondWithNotFound(response);%0A            return;%0A        %7D%0A%0A
     
@@ -2871,24 +2871,28 @@
 xt;%0A        
+   
+ 
 String text 
@@ -2956,32 +2956,36 @@
 TF-8%22);%0A        
+    
 if (text == null
@@ -2992,32 +2992,36 @@
 ) %7B%0A            
+   
+ 
 highlightedText 
@@ -3034,16 +3034,20 @@
         
+    
 %7D else %7B
@@ -3059,16 +3059,20 @@
         
+  
+  
 Iterable
@@ -3232,32 +3232,36 @@
 t);%0A            
+    
 highlightedText 
@@ -3320,32 +3320,36 @@
 tions);%0A        
+    
 %7D%0A%0A        respo
@@ -3347,43 +3347,53 @@
     
+  
+  
 respon
-se.setContentType(%22text/html%22
+dWithHtml(response, highlightedText
 );%0A 
@@ -3403,57 +3403,167 @@
     
+ 
-response.setChar
+   return;%0A        %7D%0A%0A        VideoTranscript videoTranscript = MediaLumifyProperties.VIDEO_TRANSCRIPT.getPropertyValue(artif
 act
+V
 er
-Encoding(%22UTF-8%22
+tex, propertyKey
 );%0A
-%0A
         
 IOUt
@@ -3562,74 +3562,171 @@
     
-IOUtils.write(highlightedText, response.getOutputStream(), %22UTF-8%22
+if (videoTranscript != null) %7B%0A            respondWithJson(response, videoTranscript.toJson());%0A            return;%0A        %7D%0A%0A        respondWithNotFound(response
 );%0A 

return video transcripts as json. also don't store video transcript if ccextract couldn't find anything