// Copyright 2000-2020 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.plugins.gradle.codeInsight.actions;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MvnDependencyPasteTest extends LightJavaCodeInsightTestCase {

  public void testPastedGradleDependency() {
    configureFromFileText("pom.xml", getDependency("group", "artifact", "1.0", "runtime", null));
    selectWholeFile();
    performCut();

    configureGradleFile();
    performPaste();
    checkResultByText("dependencies {\n" +
                      "    runtime 'group:artifact:1.0'\n" +
                      "}");
  }

  public void testDependencyWithClassifier() {
    configureFromFileText("pom.xml", getDependency("group", "artifact", "1.0", "runtime", "jdk14"));
    selectWholeFile();
    performCut();

    configureGradleFile();
    performPaste();
    checkResultByText("dependencies {\n" +
                      "    runtime 'group:artifact:1.0:jdk14'\n" +
                      "}");
  }

  public void test_DoNotConvertIfCoordinatesNotClear() {
    String noArtifact = getDependency("group", null, "1.0", "runtime", null);
    configureFromFileText("pom.xml", noArtifact);

    selectWholeFile();

    performCut();

    configureGradleFile();
    performPaste();
    checkResultByText(null, "dependencies {\n" +
                            "    <dependency>\n" +
                            "    <groupId>group</groupId>\n" +
                            "    <version>1.0</version>\n" +
                            "    <scope>runtime</scope>\n" +
                            "    </dependency>\n" +
                            "}", true);
  }

  public void test_AddCompile() {
    String noArtifact = getDependency("group", "artifact", "1.0", null, null);
    configureFromFileText("pom.xml", noArtifact);
    selectWholeFile();
    performCut();

    configureGradleFile();
    performPaste();
    checkResultByText("dependencies {\n" +
                      "    compile 'group:artifact:1.0'\n" +
                      "}");
  }

  public void test_AddProvided() {
    configureFromFileText("pom.xml", getDependency("group", "artifact", "1.0", "provided", null));
    selectWholeFile();
    performCut();

    configureGradleFile();
    performPaste();
    checkResultByText("dependencies {\n" +
                      "    compileOnly 'group:artifact:1.0'\n" +
                      "}");
  }

  private void configureGradleFile() {
    configureFromFileText("build.gradle",
                          "dependencies {\n" +
                          "    <caret>\n" +
                          "}");
  }

  private void selectWholeFile() {
    Document document = getEditor().getDocument();
    getEditor().getSelectionModel().setSelection(0, document.getTextLength());
  }

  private void performCut() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CUT);
    actionHandler.execute(getEditor(), null, DataManager.getInstance().getDataContextFromFocus().getResultSync());
  }

  private void performPaste() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_PASTE);
    actionHandler.execute(getEditor(), null, DataManager.getInstance().getDataContextFromFocus().getResultSync());
  }

  @NotNull
  private static String getDependency(@Nullable String groupId,
                                      @Nullable String artifactId,
                                      @Nullable String version,
                                      @Nullable String scope,
                                      @Nullable String classifier) {

    String dependency = "<dependency>\n";
    if (groupId != null) {
      dependency += "<groupId>" + groupId + "</groupId>\n";
    }
    if (artifactId != null) {
      dependency += "<artifactId>" + artifactId + "</artifactId>\n";
    }
    if (version != null) {
      dependency += "<version>" + version + "</version>\n";
    }
    if (scope != null) {
      dependency += "<scope>" + scope + "</scope>\n";
    }
    if (classifier != null) {
      dependency += "<classifier>" + classifier + "</classifier>\n";
    }
    dependency += "</dependency>";
    return dependency;
  }
}

@@ -222,32 +222,86 @@
 de.DataManager;%0A
+import com.intellij.openapi.actionSystem.DataContext;%0A
 import com.intel
@@ -337,16 +337,16 @@
 ctions;%0A
-
 import c
@@ -512,32 +512,99 @@
 rActionManager;%0A
+import com.intellij.psi.impl.source.PostprocessReformattingAspect;%0A
 import com.intel
@@ -651,16 +651,56 @@
 stCase;%0A
+import com.intellij.util.ExceptionUtil;%0A
 import o
@@ -777,16 +777,55 @@
 lable;%0A%0A
+import java.util.concurrent.TimeUnit;%0A%0A
 public c
@@ -1909,32 +1909,137 @@
 performPaste();%0A
+    PostprocessReformattingAspect.getInstance(getProject()).disablePostprocessFormattingInside(() -%3E %7B%0A  
     checkResultB
@@ -2095,24 +2095,26 @@
             
+ 
+ 
 %22    %3Cdepend
@@ -2116,32 +2116,34 @@
 ependency%3E%5Cn%22 +%0A
+  
                 
@@ -2213,24 +2213,26 @@
             
+ 
+ 
 %22    %3Cversio
@@ -2248,24 +2248,26 @@
 rsion%3E%5Cn%22 +%0A
+  
             
@@ -2339,24 +2339,26 @@
             
+ 
+ 
 %22    %3C/depen
@@ -2397,16 +2397,18 @@
         
+  
 %22%7D%22, tru
@@ -2411,16 +2411,24 @@
  true);%0A
+    %7D);%0A
   %7D%0A%0A  p
@@ -3812,73 +3812,18 @@
 ll, 
-DataManager.getInstance().getDataContextFromFocus().getResultSync
+getContext
 ());
@@ -4088,76 +4088,290 @@
 ll, 
-DataManager.getInstance().getDataContextFromFocus().getResultSync())
+getContext());%0A  %7D%0A%0A  @Nullable%0A  private static DataContext getContext() %7B%0A    try %7B%0A      return DataManager.getInstance().getDataContextFromFocusAsync().blockingGet(100, TimeUnit.MILLISECONDS);%0A    %7D%0A    catch (Exception e) %7B%0A      ExceptionUtil.rethrow(e);%0A    %7D%0A    return null
 ;%0A  

disable postprocess formatting to fix MvnDependencyPasteTest