package jetbrains.mps.baseLanguage.util.plugin.refactorings;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.baseLanguage.behavior.Classifier__BehaviorDescriptor;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.scope.Scope;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.model.SNodeAccessUtil;
import org.jetbrains.mps.openapi.language.SConcept;

public class IntroduceConstantRefactoring extends IntroduceVariableRefactoring {
  protected SNode targetClass;
  public IntroduceConstantRefactoring(SNode targetClass) {
    this.targetClass = targetClass;
  }

  public IntroduceConstantRefactoring() {
  }
  @Override
  public SNode doRefactoring() {
    SNode newDeclaration = _quotation_createNode_2jj2z2_a0a0e(myVisibilityLevel.getNode(), SNodeOperations.copyNode(getExpressionType()), SNodeOperations.copyNode(getExpression()), getName());
    Classifier__BehaviorDescriptor.insertInBestPlace_id7exmRT6rEUA.invoke(SNodeOperations.cast(this.myContainer, CONCEPTS.ClassConcept$bK), newDeclaration);
    replaceNode(getExpression(), newDeclaration);
    if (this.myIsReplacingAll) {
      for (SNode duplicate : ListSequence.fromList(this.myDuplicates)) {
        replaceNode(duplicate, newDeclaration);
      }
    }
    return newDeclaration;
  }
  @Override
  protected SNode findContainer(SNode node) {
    if (targetClass != null) {
      return targetClass;
    }

    SNode container = SNodeOperations.getNodeAncestor(node, CONCEPTS.ClassConcept$bK, false, false);
    while ((SNodeOperations.getNodeAncestor(container, CONCEPTS.ClassConcept$bK, false, false) != null)) {
      container = SNodeOperations.getNodeAncestor(container, CONCEPTS.ClassConcept$bK, false, false);
    }
    return container;
  }
  @Override
  public void replaceNode(SNode node, SNode declaration) {
    if (Scope.getScope(Scope.parent(node), node, CONCEPTS.VariableDeclaration$Y0).contains(declaration)) {
      SNodeOperations.replaceWithAnother(node, _quotation_createNode_2jj2z2_a0a0a0a6(declaration));
    } else {
      SNodeOperations.replaceWithAnother(node, _quotation_createNode_2jj2z2_a0a0a0a0g(this.myContainer, declaration));
    }
  }
  public static boolean isApplicable(SNode node) {
    return SNodeOperations.isInstanceOf(node, CONCEPTS.Expression$mB) && (SNodeOperations.getNodeAncestor(node, CONCEPTS.ClassConcept$bK, false, false) != null);
  }
  private static SNode _quotation_createNode_2jj2z2_a0a0e(Object parameter_1, Object parameter_2, Object parameter_3, Object parameter_4) {
    SNode quotedNode_5 = null;
    SNode quotedNode_6 = null;
    SNode quotedNode_7 = null;
    SNode quotedNode_8 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf93c84351fL, "StaticFieldDeclaration"));
    quotedNode_5 = nb.getResult();
    nb.setProperty(MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0x111f9e9f00cL, "isFinal"), "true");
    SNodeAccessUtil.setPropertyValue(quotedNode_5, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), (String) parameter_4);
    quotedNode_6 = (SNode) parameter_1;
    if (quotedNode_6 != null) {
      quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"), SNodeOperations.copyIfNecessary(quotedNode_6));
    }
    quotedNode_7 = (SNode) parameter_2;
    if (quotedNode_7 != null) {
      quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type"), SNodeOperations.copyIfNecessary(quotedNode_7));
    }
    quotedNode_8 = (SNode) parameter_3;
    if (quotedNode_8 != null) {
      quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer"), SNodeOperations.copyIfNecessary(quotedNode_8));
    }
    return quotedNode_5;
  }
  private static SNode _quotation_createNode_2jj2z2_a0a0a0a6(Object parameter_1) {
    SNode quotedNode_2 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8c77f1e98L, "VariableReference"));
    quotedNode_2 = nb.getResult();
    SNodeAccessUtil.setReferenceTarget(quotedNode_2, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, 0xf8cc6bf960L, "variableDeclaration"), (SNode) parameter_1);
    return quotedNode_2;
  }
  private static SNode _quotation_createNode_2jj2z2_a0a0a0a0g(Object parameter_1, Object parameter_2) {
    SNode quotedNode_3 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf940c80846L, "StaticFieldReference"));
    quotedNode_3 = nb.getResult();
    SNodeAccessUtil.setReferenceTarget(quotedNode_3, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf940c80846L, 0x10a75869f9bL, "classifier"), (SNode) parameter_1);
    SNodeAccessUtil.setReferenceTarget(quotedNode_3, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, 0xf8cc6bf960L, "variableDeclaration"), (SNode) parameter_2);
    return quotedNode_3;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept ClassConcept$bK = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca66L, "jetbrains.mps.baseLanguage.structure.ClassConcept");
    /*package*/ static final SConcept VariableDeclaration$Y0 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, "jetbrains.mps.baseLanguage.structure.VariableDeclaration");
    /*package*/ static final SConcept Expression$mB = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression");
  }
}
