package jetbrains.mps.baseLanguage.unitTest.intentions;

/*Generated by MPS */

import jetbrains.mps.intentions.AbstractIntentionDescriptor;
import jetbrains.mps.openapi.intentions.IntentionFactory;
import java.util.Collection;
import jetbrains.mps.openapi.intentions.IntentionExecutable;
import jetbrains.mps.openapi.intentions.Kind;
import jetbrains.mps.smodel.SNodePointer;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.openapi.editor.EditorContext;
import java.util.Collections;
import jetbrains.mps.intentions.AbstractIntentionExecutable;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.openapi.intentions.IntentionDescriptor;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SConcept;

public final class ConvertJUnit4_To_JUnit5_Intention extends AbstractIntentionDescriptor implements IntentionFactory {
  private Collection<IntentionExecutable> myCachedExecutable;

  public ConvertJUnit4_To_JUnit5_Intention() {
    super(Kind.NORMAL, false, new SNodePointer("r:ae5a3427-e70c-4b57-99b6-7ec8fc28a394(jetbrains.mps.baseLanguage.unitTest.intentions)", "5528547093836509144"));
  }

  @Override
  public String getPresentation() {
    return "ConvertJUnit4_To_JUnit5";
  }

  @Override
  public boolean isSurroundWith() {
    return false;
  }

  public Collection<IntentionExecutable> instances(final SNode node, final EditorContext context) {
    if (myCachedExecutable == null) {
      myCachedExecutable = Collections.<IntentionExecutable>singletonList(new IntentionImplementation());
    }
    return myCachedExecutable;
  }
  /*package*/ final class IntentionImplementation extends AbstractIntentionExecutable {
    public IntentionImplementation() {
    }

    @Override
    public String getDescription(final SNode node, final EditorContext editorContext) {
      return "Convert JUnit4 Test Case to JUnit5 \"jupiter\" Test Case";
    }

    @Override
    public void execute(final SNode node, final EditorContext editorContext) {
      Iterable<SNode> allMethods = SNodeOperations.ofConcept(SLinkOperations.getChildren(node, LINKS.member$L_2d), CONCEPTS.InstanceMethodDeclaration$39);
      // JUnit4's @Test -> JUnit5's @Test
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~Test"));
      }), createAnnotationInstance_abiijm_a0a0a0a0c0a()));
      // JUnit4's @Ignore -> JUnit5's @Disabled
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~Ignore"));
      }), createAnnotationInstance_abiijm_a0a0a0a0e0a()));
      // JUnit4's @Before -> JUnit5's @BeforeEach
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~Before"));
      }), createAnnotationInstance_abiijm_a0a0a0a0g0a()));
      // JUnit4's @After -> JUnit5's @AfterEach
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~After"));
      }), createAnnotationInstance_abiijm_a0a0a0a0i0a()));
      // JUnit4's @BeforeClass -> JUnit5's @BeforeAll
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~BeforeClass"));
      }), createAnnotationInstance_abiijm_a0a0a0a0k0a()));
      // JUnit4's @AfterClass -> JUnit5's @AfterAll
      Sequence.fromIterable(allMethods).visitAll((it) -> SNodeOperations.replaceWithAnother(ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).findFirst((a) -> {
        // JUnit4's @Test annotation
        return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~AfterClass"));
      }), createAnnotationInstance_abiijm_a0a0a0a0m0a()));
    }

    @Override
    public boolean isApplicable(final SNode node, final EditorContext editorContext) {
      if (!(isApplicableToNode(node, editorContext))) {
        return false;
      }
      return true;
    }

    private boolean isApplicableToNode(final SNode node, final EditorContext editorContext) {
      if (SConceptOperations.isExactly(SNodeOperations.asSConcept(SNodeOperations.getConcept(node)), CONCEPTS.ClassifierType$bL)) {
        Iterable<SNode> allMethods = SNodeOperations.ofConcept(SLinkOperations.getChildren(node, LINKS.member$L_2d), CONCEPTS.InstanceMethodDeclaration$39);
        Iterable<SNode> junit4TestMethods = Sequence.fromIterable(allMethods).where((it) -> ListSequence.fromList(SLinkOperations.getChildren(it, LINKS.annotation$K49I)).any((a) -> {
          // JUnit4's @Test annotation
          return SLinkOperations.hasPointer(a, LINKS.annotation$12Ek, new SNodePointer("49808fad-9d41-4b96-83fa-9231640f6b2b/java:org.junit(JUnit/)", "~Test"));
        }));
        return Sequence.fromIterable(junit4TestMethods).isNotEmpty();
      }
      return false;
    }


    @Override
    public IntentionDescriptor getDescriptor() {
      return ConvertJUnit4_To_JUnit5_Intention.this;
    }

  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0c0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~Test")));
    return n0.getResult();
  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0e0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~Disabled")));
    return n0.getResult();
  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0g0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~BeforeEach")));
    return n0.getResult();
  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0i0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~AfterEach")));
    return n0.getResult();
  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0k0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~BeforeAll")));
    return n0.getResult();
  }
  private static SNode createAnnotationInstance_abiijm_a0a0a0a0m0a() {
    PersistenceFacade facade = PersistenceFacade.getInstance();
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.AnnotationInstance$yl);
    n0.setReference(LINKS.annotation$12Ek, new SNodePointer(facade.createModelReference("63b449db-0918-4a4a-a891-2c430ab133e4/java:org.junit.jupiter.api(org.junit.junit5/)"), facade.createNodeId("~AfterAll")));
    return n0.getResult();
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink member$L_2d = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, 0x4a9a46de59132803L, "member");
    /*package*/ static final SContainmentLink annotation$K49I = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6be947aL, 0x114a6beb0bdL, "annotation");
    /*package*/ static final SReferenceLink annotation$12Ek = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, 0x114a6b85d40L, "annotation");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept InstanceMethodDeclaration$39 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b21dL, "jetbrains.mps.baseLanguage.structure.InstanceMethodDeclaration");
    /*package*/ static final SConcept ClassifierType$bL = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType");
    /*package*/ static final SConcept AnnotationInstance$yl = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, "jetbrains.mps.baseLanguage.structure.AnnotationInstance");
  }
}
