package jetbrains.mps.baseLanguage.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.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.action.SNodeFactoryOperations;
import jetbrains.mps.openapi.intentions.IntentionDescriptor;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SConcept;

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

  public InvertIfCondition_Intention() {
    super(Kind.NORMAL, false, new SNodePointer("r:00000000-0000-4000-0000-011c895902c6(jetbrains.mps.baseLanguage.intentions)", "1195647359405"));
  }

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

  @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 "Invert If Condition";
    }

    @Override
    public void execute(final SNode node, final EditorContext editorContext) {
      if (ListSequence.fromList(SLinkOperations.getChildren(node, LINKS.elsifClauses$EVJW)).isNotEmpty()) {
        SNode newIfFalsefStatement = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, "jetbrains.mps.baseLanguage.structure.IfStatement"));
        SLinkOperations.setTarget(newIfFalsefStatement, LINKS.condition$5R17, SLinkOperations.getTarget(ListSequence.fromList(SLinkOperations.getChildren(node, LINKS.elsifClauses$EVJW)).first(), LINKS.condition$k0T9));
        SLinkOperations.setTarget(newIfFalsefStatement, LINKS.ifTrue$5Rg8, SLinkOperations.getTarget(ListSequence.fromList(SLinkOperations.getChildren(node, LINKS.elsifClauses$EVJW)).first(), LINKS.statementList$neQf));
        SLinkOperations.setTarget(newIfFalsefStatement, LINKS.ifFalseStatement$psZK, SLinkOperations.getTarget(node, LINKS.ifFalseStatement$psZK));
        ListSequence.fromList(SLinkOperations.getChildren(newIfFalsefStatement, LINKS.elsifClauses$EVJW)).addSequence(ListSequence.fromList(SLinkOperations.getChildren(node, LINKS.elsifClauses$EVJW)));
        ListSequence.fromList(SLinkOperations.getChildren(newIfFalsefStatement, LINKS.elsifClauses$EVJW)).removeElementAt(0);
        SLinkOperations.setTarget(node, LINKS.ifFalseStatement$psZK, newIfFalsefStatement);
      }


      // Invert condition
      SNode condition = SLinkOperations.getTarget(node, LINKS.condition$5R17);
      if ((condition != null)) {
        if (SNodeOperations.isInstanceOf(condition, CONCEPTS.NotExpression$Pc)) {
          SNodeOperations.replaceWithAnother(condition, SLinkOperations.getTarget(SNodeOperations.cast(condition, CONCEPTS.NotExpression$Pc), LINKS.expression$sv_H));
        } else {
          SNode newCondition = null;
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.EqualsExpression$MF)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.NotEqualsExpression$aX, null);
          } else
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.NotEqualsExpression$aX)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.EqualsExpression$MF, null);
          } else
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.GreaterThanExpression$XD)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.LessThanOrEqualsExpression$jY, null);
          } else
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.GreaterThanOrEqualsExpression$NV)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.LessThanExpression$Li, null);
          } else
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.LessThanExpression$Li)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.GreaterThanOrEqualsExpression$NV, null);
          } else
          if (SNodeOperations.isInstanceOf(condition, CONCEPTS.LessThanOrEqualsExpression$jY)) {
            newCondition = SNodeFactoryOperations.createNewNode(CONCEPTS.GreaterThanExpression$XD, null);
          }
          if (newCondition != null) {
            SLinkOperations.setTarget(newCondition, LINKS.leftExpression$sEj, SLinkOperations.getTarget(SNodeOperations.cast(condition, CONCEPTS.BinaryOperation$W1), LINKS.leftExpression$sEj));
            SLinkOperations.setTarget(newCondition, LINKS.rightExpression$nvX, SLinkOperations.getTarget(SNodeOperations.cast(condition, CONCEPTS.BinaryOperation$W1), LINKS.rightExpression$nvX));
            SLinkOperations.setTarget(node, LINKS.condition$5R17, newCondition);
          } else {
            SNodeFactoryOperations.replaceWithNewChild(condition, CONCEPTS.NotExpression$Pc);
          }
        }
      }
      // Flip ifTrue and ifFalse
      SNode ifTrue = SLinkOperations.getTarget(node, LINKS.ifTrue$5Rg8);
      SNode ifFalse = SLinkOperations.getTarget(node, LINKS.ifFalseStatement$psZK);
      SNode newIfTrue;
      SNode newIfFalse;
      // Set new ifFalse
      if (ListSequence.fromList(SLinkOperations.getChildren(ifTrue, LINKS.statement$53DE)).isEmpty()) {
        newIfFalse = null;
      } else
      if (ListSequence.fromList(SLinkOperations.getChildren(ifTrue, LINKS.statement$53DE)).count() == 1 && SNodeOperations.isInstanceOf(ListSequence.fromList(SLinkOperations.getChildren(ifTrue, LINKS.statement$53DE)).first(), CONCEPTS.IfStatement$Q4)) {
        newIfFalse = ListSequence.fromList(SLinkOperations.getChildren(ifTrue, LINKS.statement$53DE)).first();
      } else {
        newIfFalse = SNodeFactoryOperations.createNewNode(CONCEPTS.BlockStatement$u4, null);
        SLinkOperations.setTarget(SNodeOperations.cast(newIfFalse, CONCEPTS.BlockStatement$u4), LINKS.statements$q65M, ifTrue);
      }
      // Set new ifTrue
      if (SNodeOperations.isInstanceOf(ifFalse, CONCEPTS.BlockStatement$u4)) {
        newIfTrue = SLinkOperations.getTarget(SNodeOperations.cast(ifFalse, CONCEPTS.BlockStatement$u4), LINKS.statements$q65M);
      } else {
        newIfTrue = SNodeFactoryOperations.createNewNode(CONCEPTS.StatementList$m_, null);
        if (ifFalse != null) {
          ListSequence.fromList(SLinkOperations.getChildren(newIfTrue, LINKS.statement$53DE)).addElement(ifFalse);
        }
      }
      SLinkOperations.setTarget(node, LINKS.ifTrue$5Rg8, newIfTrue);
      SLinkOperations.setTarget(node, LINKS.ifFalseStatement$psZK, newIfFalse);
    }

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



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

  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink condition$5R17 = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0xf8cc56b218L, "condition");
    /*package*/ static final SContainmentLink elsifClauses$EVJW = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0x118cecf1287L, "elsifClauses");
    /*package*/ static final SContainmentLink condition$k0T9 = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x118ceceb41aL, 0x118ced0983eL, "condition");
    /*package*/ static final SContainmentLink ifTrue$5Rg8 = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0xf8cc56b219L, "ifTrue");
    /*package*/ static final SContainmentLink statementList$neQf = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x118ceceb41aL, 0x118ced0f8fdL, "statementList");
    /*package*/ static final SContainmentLink ifFalseStatement$psZK = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0xfc092b6b76L, "ifFalseStatement");
    /*package*/ static final SContainmentLink expression$sv_H = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression");
    /*package*/ static final SContainmentLink leftExpression$sEj = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11cL, "leftExpression");
    /*package*/ static final SContainmentLink rightExpression$nvX = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11bL, "rightExpression");
    /*package*/ static final SContainmentLink statement$53DE = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, 0xf8cc6bf961L, "statement");
    /*package*/ static final SContainmentLink statements$q65M = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfc092b6b77L, 0xfc092b6b78L, "statements");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept NotExpression$Pc = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression");
    /*package*/ static final SConcept NotEqualsExpression$aX = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf9e20e303fL, "jetbrains.mps.baseLanguage.structure.NotEqualsExpression");
    /*package*/ static final SConcept EqualsExpression$MF = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b210L, "jetbrains.mps.baseLanguage.structure.EqualsExpression");
    /*package*/ static final SConcept LessThanOrEqualsExpression$jY = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d53a915L, "jetbrains.mps.baseLanguage.structure.LessThanOrEqualsExpression");
    /*package*/ static final SConcept GreaterThanExpression$XD = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced38fcfL, "jetbrains.mps.baseLanguage.structure.GreaterThanExpression");
    /*package*/ static final SConcept LessThanExpression$Li = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced3b82aL, "jetbrains.mps.baseLanguage.structure.LessThanExpression");
    /*package*/ static final SConcept GreaterThanOrEqualsExpression$NV = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d0fac2cL, "jetbrains.mps.baseLanguage.structure.GreaterThanOrEqualsExpression");
    /*package*/ static final SConcept BinaryOperation$W1 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, "jetbrains.mps.baseLanguage.structure.BinaryOperation");
    /*package*/ static final SConcept BlockStatement$u4 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfc092b6b77L, "jetbrains.mps.baseLanguage.structure.BlockStatement");
    /*package*/ static final SConcept IfStatement$Q4 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, "jetbrains.mps.baseLanguage.structure.IfStatement");
    /*package*/ static final SConcept StatementList$m_ = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList");
  }
}
