// Copyright 2000-2019 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 com.intellij.java.codeInsight;

import com.intellij.codeInsight.intention.AddAnnotationPsiFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.JavaModuleExternalPaths;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
import com.intellij.testFramework.fixtures.*;

public class ExternalAnnotationsTest extends UsefulTestCase {
  private CodeInsightTestFixture myFixture;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    final TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());

    myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());
    final String dataPath = PathManagerEx.getTestDataPath() + "/codeInsight/externalAnnotations";
    myFixture.setTestDataPath(dataPath);
    JavaModuleFixtureBuilder<?> builder = projectBuilder.addModule(JavaModuleFixtureBuilder.class);
    builder.setMockJdkLevel(JavaModuleFixtureBuilder.MockJdkLevel.jdk15);

    myFixture.setUp();
    Module myModule = builder.getFixture().getModule();
    ModuleRootModificationUtil.updateModel(myModule, model -> {
      String contentUrl = VfsUtilCore.pathToUrl(myFixture.getTempDirPath());
      model.addContentEntry(contentUrl).addSourceFolder(contentUrl + "/src", false);
      final JavaModuleExternalPaths extension = model.getModuleExtension(JavaModuleExternalPaths.class);
      extension.setExternalAnnotationUrls(new String[]{VfsUtilCore.pathToUrl(myFixture.getTempDirPath() + "/content/anno")});
    });

    Project myProject = myFixture.getProject();

    JavaCodeStyleSettings.getInstance(myProject).USE_EXTERNAL_ANNOTATIONS = true;
  }

  @Override
  protected void tearDown() throws Exception {
    try {
      myFixture.tearDown();
    }
    catch (Throwable e) {
      addSuppressedException(e);
    }
    finally {
      myFixture = null;
      super.tearDown();
    }
  }

  public void testAddedAnnotationInCodeWhenAlreadyPresent() {
    PsiFile file = myFixture.configureByFile("src/withAnnotation/Foo.java");
    PsiMethod method = PsiTreeUtil.getParentOfType(myFixture.getElementAtCaret(), PsiMethod.class, false);
    assertNotNull(method);
    AddAnnotationPsiFix.createAddNullableFix(method).invoke(myFixture.getProject(), file, method, method);
    myFixture.checkResultByFile("src/withAnnotation/Foo_after.java");
  }

  public void testRenameClassWithExternalAnnotations() {
    myFixture.configureByFiles("src/rename/Foo.java", "content/anno/rename/annotations.xml");

    myFixture.renameElementAtCaret("Bar");

    myFixture.checkResultByFile("content/anno/rename/annotations.xml",
                                "content/anno/rename/annotations_after.xml",
                                true);
  }

  public void testBringToSrc() {
    myFixture.configureByFiles("src/toSrc/Foo.java", "content/anno/toSrc/annotations.xml");

    IntentionAction action = myFixture.findSingleIntention("Insert '@Deprecated'");
    assertNotNull(action);

    myFixture.launchAction(action);

    myFixture.checkResultByFile("src/toSrc/Foo_after.java");
    myFixture.checkResultByFile("content/anno/toSrc/annotations.xml",
                                "content/anno/toSrc/annotations_after.xml",
                                true);
  }

  public void testFromSrcToExternal() {
    myFixture.configureByFiles("src/fromSrc/Foo.java", "content/anno/fromSrc/annotations.xml");

    IntentionAction action = myFixture.findSingleIntention("Annotate externally");
    assertNotNull(action);

    myFixture.launchAction(action);

    myFixture.checkResultByFile("src/fromSrc/Foo_after.java");
    myFixture.checkResultByFile("content/anno/fromSrc/annotations.xml",
                                "content/anno/fromSrc/annotations_after.xml",
                                true);

  }
}

@@ -174,16 +174,67 @@
 sight;%0A%0A
+import com.intellij.codeInsight.CodeInsightBundle;%0A
 import c
@@ -4237,18 +4237,44 @@
 ion(
-%22A
+CodeInsightBundle.message(%22a
 nnotate
- 
+.
 exte
@@ -4281,16 +4281,17 @@
 rnally%22)
+)
 ;%0A    as

fix family name capitalization