package org.jetbrains.plugins.groovy.lang.completion;

import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import org.jetbrains.plugins.groovy.CompositeCompletionData;
import org.jetbrains.plugins.groovy.GroovyFileType;
import org.jetbrains.plugins.groovy.util.TestUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Comparator;

/**
 * author ven
 */
public abstract class CompletionTestBase extends JavaCodeInsightFixtureTestCase {

  protected void doTest() throws Throwable {
    final List<String> stringList = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
    final String fileName = getTestName(true) + "." + getExtension();
    myFixture.addFileToProject(fileName, stringList.get(0));
    myFixture.configureByFile(fileName);

    CompositeCompletionData.restrictCompletion(addReferenceVariants(), addKeywords());

    boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = false;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;


    String result = "";
    try {
      myFixture.completeBasic();

      final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myFixture.getEditor());
      if (lookup != null) {
        LookupElement[] items = lookup.getItems();
        Arrays.sort(items, new Comparator<LookupElement>() {
          public int compare(LookupElement o1, LookupElement o2) {
            return o1.getLookupString().compareTo(o2.getLookupString());
          }
        });
        result = "";
        for (LookupElement item : items) {
          result = result + "\n" + item.getLookupString();
        }
        result = result.trim();
        LookupManager.getInstance(myFixture.getProject()).hideActiveLookup();
      }

    }
    finally {
      CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = true;
      CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = old;
      CompositeCompletionData.restrictCompletion(true, true);
    }
    assertEquals(stringList.get(1), result);
  }

  protected String getExtension() {
    return "groovy";
  }

  protected FileType getFileType() {
    return GroovyFileType.GROOVY_FILE_TYPE;
  }

  protected boolean addKeywords() {
    return true;
  }

  protected boolean addReferenceVariants() {
    return true;
  }
}

@@ -571,21 +571,26 @@
 va.util.
-Array
+Collection
 s;%0Aimpor
@@ -601,20 +601,26 @@
 va.util.
-List
+Comparator
 ;%0Aimport
@@ -630,26 +630,20 @@
 va.util.
-Comparator
+List
 ;%0A%0A/**%0A 
@@ -1593,24 +1593,29 @@
 ) %7B%0A        
+List%3C
 LookupElemen
@@ -1619,10 +1619,9 @@
 ment
-%5B%5D
+%3E
  ite
@@ -1656,13 +1656,18 @@
     
-Array
+Collection
 s.so

introduce LookupArranger to support different sorting strategies in lookups