package io.github.vcuswimlab.stackintheflow.controller;

import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import io.github.vcuswimlab.stackintheflow.controller.info.match.StringMatchUtils;
import io.github.vcuswimlab.stackintheflow.model.score.combiner.Combiner;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Chase on 1/7/2017.
 */
public class AutoQueryGenerator {

    private static final int MAX_QUERY_TERMS = 4;


    public static String generateQuery(Editor editor, Combiner combiner) {

        CaretModel caretModel = editor.getCaretModel();
        LogicalPosition logicalPosition = caretModel.getLogicalPosition();

        final Document document = editor.getDocument();
        String editorText = document.getText();

        Set<String> imports = StringMatchUtils.extractImports(editorText);

        Map<String, Integer> termsFreqMap = new HashMap<>();
        imports.forEach(i -> Arrays.stream(i.toLowerCase().split("\\."))
                .forEach(t -> termsFreqMap.put(t, 1 + termsFreqMap.getOrDefault(t, 0))));

        String[] lines = editorText.split("\\n");
        String currentLine = lines[logicalPosition.line];

        Arrays.stream(currentLine.toLowerCase().split("\\b"))
                .forEach(t -> termsFreqMap.put(t, 2 + termsFreqMap.getOrDefault(t, 0)));

        Map<String, Double> scores =
                termsFreqMap.entrySet().stream()
                        .collect(Collectors.toMap(Map.Entry::getKey, e -> combiner.generateCumulativeScore(e.getKey())));

        //Collects the MAX_QUERY_TERMS most frequent elements in the list
        List<String> top = scores
                .entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(MAX_QUERY_TERMS)
                .map(Map.Entry::getKey).collect(Collectors.toList());

        return top.stream().collect(Collectors.joining(" "));
    }
}

@@ -89,158 +89,9 @@
 tor.
-CaretModel;%0Aimport com.intellij.openapi.editor.Document;%0Aimport com.intellij.openapi.editor.Editor;%0Aimport com.intellij.openapi.editor.LogicalPosition
+*
 ;%0Aim
@@ -642,65 +642,392 @@
 ();%0A
-%0A        final Document document = editor.getDocument();%0A
+        SelectionModel selectionModel = editor.getSelectionModel();%0A%0A        final Document document = editor.getDocument();%0A%0A        String selectedText = selectionModel.getSelectedText();%0A%0A        Map%3CString, Integer%3E termsFreqMap = new HashMap%3C%3E();%0A%0A        // If the user has not selected anything then extract query from entire document%0A        if (selectedText.isEmpty()) %7B%0A    
     
@@ -1063,32 +1063,36 @@
 ent.getText();%0A%0A
+    
         Set%3CStri
@@ -1159,65 +1159,8 @@
     
-    Map%3CString, Integer%3E termsFreqMap = new HashMap%3C%3E();%0A
     
@@ -1224,24 +1224,28 @@
 lit(%22%5C%5C.%22))%0A
+    
             
@@ -1327,24 +1327,28 @@
 );%0A%0A        
+  
+  
 String%5B%5D lin
@@ -1377,16 +1377,20 @@
 %22%5C%5Cn%22);%0A
+    
         
@@ -1440,16 +1440,20 @@
 line%5D;%0A%0A
+    
         
@@ -1514,32 +1514,36 @@
                 
+    
 .forEach(t -%3E te
@@ -1594,24 +1594,278 @@
 ult(t, 0)));
+%0A        %7D else %7B // The user has highlighted as selection, pull our terms from that%0A            Arrays.stream(selectedText.toLowerCase().split(%22%5C%5Cb%22))%0A                    .forEach(t -%3E termsFreqMap.put(t, 2 + termsFreqMap.getOrDefault(t, 0)));%0A        %7D
 %0A%0A        Ma

Implement AutoQuery on selection.