// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.yaml.schema;

import com.intellij.json.JsonFileType;
import com.intellij.json.psi.JsonFile;
import com.intellij.json.psi.JsonObject;
import com.intellij.json.psi.JsonStringLiteral;
import com.intellij.json.psi.JsonValue;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.jsonSchema.impl.JsonBySchemaHeavyCompletionTest;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

public class YamlByJsonSchemaHeavyCompletionTest extends JsonBySchemaHeavyCompletionTest {
  @NotNull
  @Override
  public String getTestDataPath() {
    return PathManagerEx.getCommunityHomePath() + "/plugins/yaml/testSrc/org/jetbrains/yaml/schema/data/completion";
  }

  @Override
  protected String getBasePath() {
    return ""; // unused
  }

  @Override
  protected String getExtensionWithoutDot() {
    return "yml";
  }

  @Override
  public void testEditingSchemaAffectsCompletion() throws Exception {
    baseTest(getTestName(true), "testEditing", () -> {
      complete();
      assertStringItems("preserve", "react", "react-native");

      final PsiFile schema = myFile.getParent().findFile("Schema.json");
      final int idx = schema.getText().indexOf("react-native");
      Assert.assertTrue(idx > 0);
      PsiElement element = schema.findElementAt(idx);
      element = element instanceof JsonStringLiteral ? element : PsiTreeUtil.getParentOfType(element, JsonStringLiteral.class);
      Assert.assertTrue(element instanceof JsonStringLiteral);

      final PsiFile dummy = PsiFileFactory.getInstance(myProject).createFileFromText("test.json", JsonFileType.INSTANCE,
                                                                                     "{\"a\": \"completelyChanged\"}");
      Assert.assertTrue(dummy instanceof JsonFile);
      final JsonValue top = ((JsonFile)dummy).getTopLevelValue();
      final JsonValue newLiteral = ((JsonObject)top).findProperty("a").getValue();

      PsiElement finalElement = element;
      WriteAction.run(() -> finalElement.replace(newLiteral));

      complete();
      assertStringItems("completelyChanged", "preserve", "react");
    });
  }

  @Override
  public void testOneOfWithNotFilledPropertyValue() throws Exception {
    baseCompletionTest("oneOfWithEnumValue", "oneOfWithEmptyPropertyValue", "business", "home");
  }

  @Override
  public void testObjectLiteral() throws Exception {
    baseInsertTest("insertArrayOrObjectLiteral", "objectLiteral");
    complete();
    assertStringItems("insideTopObject1", "insideTopObject2");
  }

  @Override
  public void testRequiredPropsFirst() throws Exception {
    // do nothing
  }

  @Override
  public void testRequiredPropsLast() throws Exception {
    // do nothing
  }

  @Override
  public void testWhitespaceAfterColon() throws Exception {
    // do nothing
  }

  public void testInsertArrayItemNested() throws Exception {
    baseInsertTest("insertArrayItemNested", "test");
  }

  public void testInsertColonAfterPropName() throws Exception {
    baseInsertTest("insertColonAfterPropName", "test");
  }
}

@@ -639,16 +639,67 @@
 eeUtil;%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import c
@@ -760,16 +760,16 @@
 onTest;%0A
-
 import o
@@ -828,16 +828,40 @@
 ssert;%0A%0A
+import java.util.List;%0A%0A
 public c
@@ -1449,26 +1449,23 @@
   assert
-StringItem
+Contain
 s(%22prese
@@ -2492,26 +2492,23 @@
   assert
-StringItem
+Contain
 s(%22compl
@@ -2648,26 +2648,16 @@
     base
-Completion
 Test(%22on
@@ -2706,16 +2706,63 @@
 Value%22, 
+() -%3E %7B%0A      complete();%0A      assertContains(
 %22busines
@@ -2770,24 +2770,32 @@
 %22, %22home%22);%0A
+    %7D);%0A
   %7D%0A%0A  @Over
@@ -3458,32 +3458,32 @@
 ows Exception %7B%0A
-
     baseInsertTe
@@ -3525,11 +3525,233 @@
 t%22);%0A  %7D
+%0A%0A  protected void assertContains(String... strings) %7B%0A    assertNotNull(myItems);%0A    List%3CString%3E actual = ContainerUtil.map(myItems, element -%3E element.getLookupString());%0A    assertContainsOrdered(actual, strings);%0A  %7D
 %0A%7D%0A

fix tests