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.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.action.SNodeFactoryOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.openapi.intentions.IntentionDescriptor;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;

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

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

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

  @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 "Swap Ternary Operator Branches";
    }

    @Override
    public void execute(final SNode node, final EditorContext editorContext) {
      boolean unidentifiedConditionExpr = false;
      if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BinaryOperation$W1)) {
        SNode left = SLinkOperations.getTarget(SNodeOperations.cast(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BinaryOperation$W1), LINKS.leftExpression$sEj);
        SNode right = SLinkOperations.getTarget(SNodeOperations.cast(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BinaryOperation$W1), LINKS.rightExpression$nvX);
        if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.EqualsExpression$MF)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.NotEqualsExpression$aX, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NPEEqualsExpression$Ey)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.NPENotEqualsExpression$Ef, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NotEqualsExpression$aX)) {
          SNode eq = SNodeFactoryOperations.createNewNode(CONCEPTS.EqualsExpression$MF, null);
          SLinkOperations.setTarget(eq, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(eq, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, eq);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NPENotEqualsExpression$Ef)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.NPEEqualsExpression$Ey, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.GreaterThanExpression$XD)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.LessThanOrEqualsExpression$jY, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.GreaterThanOrEqualsExpression$NV)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.LessThanExpression$Li, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.LessThanExpression$Li)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.GreaterThanOrEqualsExpression$NV, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.LessThanOrEqualsExpression$jY)) {
          SNode not = SNodeFactoryOperations.createNewNode(CONCEPTS.LessThanExpression$Li, null);
          SLinkOperations.setTarget(not, LINKS.leftExpression$sEj, left);
          SLinkOperations.setTarget(not, LINKS.rightExpression$nvX, right);
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, not);
        } else {
          unidentifiedConditionExpr = true;
        }
      } else {
        if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NotExpression$Pc)) {
          SLinkOperations.setTarget(node, LINKS.condition$nwNI, SLinkOperations.getTarget(SNodeOperations.cast(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NotExpression$Pc), LINKS.expression$sv_H));
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BooleanConstant$n4)) {
          boolean currentValue = SPropertyOperations.getBoolean(SNodeOperations.cast(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BooleanConstant$n4), PROPS.value$5y_M);
          SPropertyOperations.assign(SNodeOperations.cast(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.BooleanConstant$n4), PROPS.value$5y_M, (currentValue ? false : true));
        } else {
          unidentifiedConditionExpr = true;
        }
      }
      if (unidentifiedConditionExpr) {
        SNodeFactoryOperations.replaceWithNewChild(SLinkOperations.getTarget(node, LINKS.condition$nwNI), CONCEPTS.NotExpression$Pc);
      }

      SNode ifTrue = SNodeOperations.copyNode(SLinkOperations.getTarget(node, LINKS.ifTrue$Tg0R));
      SLinkOperations.setTarget(node, LINKS.ifTrue$Tg0R, SNodeOperations.copyNode(SLinkOperations.getTarget(node, LINKS.ifFalse$Wbma)));
      SLinkOperations.setTarget(node, LINKS.ifFalse$Wbma, ifTrue);
    }

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



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

  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink condition$nwNI = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012826fL, "condition");
    /*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 expression$sv_H = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression");
    /*package*/ static final SContainmentLink ifTrue$Tg0R = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012a1c0L, "ifTrue");
    /*package*/ static final SContainmentLink ifFalse$Wbma = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012cedcL, "ifFalse");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept BinaryOperation$W1 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, "jetbrains.mps.baseLanguage.structure.BinaryOperation");
    /*package*/ static final SConcept NotEqualsExpression$aX = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf9e20e303fL, "jetbrains.mps.baseLanguage.structure.NotEqualsExpression");
    /*package*/ static final SConcept NPEEqualsExpression$Ey = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11d47dc0e3bL, "jetbrains.mps.baseLanguage.structure.NPEEqualsExpression");
    /*package*/ static final SConcept NPENotEqualsExpression$Ef = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11d47db1c91L, "jetbrains.mps.baseLanguage.structure.NPENotEqualsExpression");
    /*package*/ static final SConcept EqualsExpression$MF = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b210L, "jetbrains.mps.baseLanguage.structure.EqualsExpression");
    /*package*/ static final SConcept GreaterThanExpression$XD = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced38fcfL, "jetbrains.mps.baseLanguage.structure.GreaterThanExpression");
    /*package*/ static final SConcept LessThanOrEqualsExpression$jY = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d53a915L, "jetbrains.mps.baseLanguage.structure.LessThanOrEqualsExpression");
    /*package*/ static final SConcept GreaterThanOrEqualsExpression$NV = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d0fac2cL, "jetbrains.mps.baseLanguage.structure.GreaterThanOrEqualsExpression");
    /*package*/ static final SConcept LessThanExpression$Li = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced3b82aL, "jetbrains.mps.baseLanguage.structure.LessThanExpression");
    /*package*/ static final SConcept NotExpression$Pc = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression");
    /*package*/ static final SConcept BooleanConstant$n4 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, "jetbrains.mps.baseLanguage.structure.BooleanConstant");
  }

  private static final class PROPS {
    /*package*/ static final SProperty value$5y_M = MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, 0xf8cc56b202L, "value");
  }
}
