package com.github.platan.idea.dependencies;

import com.intellij.codeInsight.editorActions.PasteHandler;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import com.intellij.util.Producer;
import org.jetbrains.annotations.Nullable;

import java.awt.Component;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import static com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_PASTE;

public class MavenToGradleDependenciesCopyPasteProcessorTest extends LightPlatformCodeInsightFixtureTestCase {

    public void test__convert_maven_to_gradle_while_pasting_to_build_gradle() {
        myFixture.configureByText("build.gradle", "//\n<caret>\n//");
        String toPaste = "<dependency>\n"
                + "\t<groupId>org.spockframework</groupId>\n"
                + "\t<artifactId>spock-core</artifactId>\n"
                + "\t<version>1.0-groovy-2.4</version>\n"
                + "</dependency>";

        runPasteAction(toPaste);

        myFixture.checkResult("//\ncompile 'org.spockframework:spock-core:1.0-groovy-2.4'\n//");
    }

    public void test__convert_maven_to_gradle_while_pasting_to_any_gradle_file() {
        myFixture.configureByText("download.gradle", "<caret>");
        String toPaste = "<dependency>\n"
                + "\t<groupId>org.spockframework</groupId>\n"
                + "\t<artifactId>spock-core</artifactId>\n"
                + "\t<version>1.0-groovy-2.4</version>\n"
                + "</dependency>";

        runPasteAction(toPaste);

        myFixture.checkResult("compile 'org.spockframework:spock-core:1.0-groovy-2.4'");
    }

    public void test__do_not_convert_maven_to_gradle_while_pasting_to_setting_gradle() {
        myFixture.configureByText("settings.gradle", "<caret>");
        String toPaste = "<dependency>\n"
                + "\t<groupId>org.spockframework</groupId>\n"
                + "\t<artifactId>spock-core</artifactId>\n"
                + "\t<version>1.0-groovy-2.4</version>\n"
                + "</dependency>";

        runPasteAction(toPaste);

        myFixture.checkResult("<dependency>\n"
                + "<groupId>org.spockframework</groupId>\n"
                + "\t<artifactId>spock-core</artifactId>\n"
                + "<version>1.0-groovy-2.4</version>\n"
                + "</dependency>");
    }

    private void runPasteAction(final String toPaste) {
        final Producer<Transferable> producer = new Producer<Transferable>() {
            @Nullable
            @Override
            public Transferable produce() {
                return new StringSelection(toPaste);
            }
        };
        EditorActionManager actionManager = EditorActionManager.getInstance();
        EditorActionHandler pasteActionHandler = actionManager.getActionHandler(ACTION_EDITOR_PASTE);
        final PasteHandler pasteHandler = new PasteHandler(pasteActionHandler);
        new WriteCommandAction.Simple(getProject(), new PsiFile[0]) {
            protected void run() throws Throwable {
                Component component = myFixture.getEditor().getComponent();
                DataContext dataContext = DataManager.getInstance().getDataContext(component);
                pasteHandler.execute(myFixture.getEditor(), dataContext, producer);
            }
        }.execute();
    }
}

@@ -2680,24 +2680,733 @@
 %3E%22);%0A    %7D%0A%0A
+    public void test__do_not_convert_maven_to_gradle_while_pasting_to_non_gradle_file() %7B%0A        myFixture.configureByText(%22Test.groovy%22, %22%3Ccaret%3E%22);%0A        String toPaste = %22%3Cdependency%3E%5Cn%22%0A                + %22%5Ct%3CgroupId%3Eorg.spockframework%3C/groupId%3E%5Cn%22%0A                + %22%5Ct%3CartifactId%3Espock-core%3C/artifactId%3E%5Cn%22%0A                + %22%5Ct%3Cversion%3E1.0-groovy-2.4%3C/version%3E%5Cn%22%0A                + %22%3C/dependency%3E%22;%0A%0A        runPasteAction(toPaste);%0A%0A        myFixture.checkResult(%22%3Cdependency%3E%5Cn%22%0A                + %22%3CgroupId%3Eorg.spockframework%3C/groupId%3E%5Cn%22%0A                + %22%5Ct%3CartifactId%3Espock-core%3C/artifactId%3E%5Cn%22%0A                + %22%3Cversion%3E1.0-groovy-2.4%3C/version%3E%5Cn%22%0A                + %22%3C/dependency%3E%22);%0A    %7D%0A%0A
     private 

Test for pasting into non gradle file