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 java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.editor.runtime.cells.CellIdManager;
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 StringPlusToFormat_Intention extends AbstractIntentionDescriptor implements IntentionFactory {
  private Collection<IntentionExecutable> myCachedExecutable;

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

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

  @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 "Use 'format' operation";
    }

    @Override
    public void execute(final SNode node, final EditorContext editorContext) {
      SNode pe = node;
      do {
        if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.StringLiteral$xu)) {
          break;
        }
        // expect null if not PE
        pe = SNodeOperations.as(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.PlusExpression$k0);
      } while (pe != null);
      if (pe == null) {
        return;
      }

      // cut PE out of node tree to avoid notifications
      SNode de = SNodeOperations.replaceWithNewChild(node, CONCEPTS.DotExpression$yW);
      StringBuilder sb = new StringBuilder();
      List<SNode> args = ListSequence.fromList(new ArrayList<>());
      SNode prevPE = null;
      do {
        if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.StringLiteral$xu)) {
          sb.append(SPropertyOperations.getString(SNodeOperations.cast(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.StringLiteral$xu), PROPS.value$w7MM));
          if (SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX) != prevPE) {
            sb.append("%s");
            ListSequence.fromList(args).addElement(SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX));
          }
        } else if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX), CONCEPTS.StringLiteral$xu)) {
          if (SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj) != prevPE) {
            sb.append("%s");
            ListSequence.fromList(args).addElement(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj));
          }
          sb.append(SPropertyOperations.getString(SNodeOperations.cast(SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX), CONCEPTS.StringLiteral$xu), PROPS.value$w7MM));
        } else if (SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj) == prevPE) {
          sb.append("%s");
          ListSequence.fromList(args).addElement(SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX));
        } else if (SLinkOperations.getTarget(pe, LINKS.rightExpression$nvX) == prevPE) {
          sb.append("%s");
          ListSequence.fromList(args).addElement(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj));
        }
        prevPE = pe;
        pe = SNodeOperations.as(SNodeOperations.getParent(pe), CONCEPTS.PlusExpression$k0);
      } while (pe != null);
      SNode sl = SLinkOperations.setNewChild(de, LINKS.operand$w6IR, CONCEPTS.StringLiteral$xu);
      SPropertyOperations.assign(sl, PROPS.value$w7MM, sb.toString());
      SNode fo = SLinkOperations.setNewChild(de, LINKS.operation$gs9E, CONCEPTS.FormatOperation$63);
      ListSequence.fromList(SLinkOperations.getChildren(fo, LINKS.arguments$3huj)).addSequence(ListSequence.fromList(args));
      // selection is not really necessary, I just try to avoid extra editor state save that
      // leads to an extra empty undo action. Didn't help, though.
      SelectionUtil.selectLabelCellAnSetCaret(editorContext, sl, "*" + CellIdManager.createPropertyId("value"), sb.length());
    }

    @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) {
      // "string1" + anything + "string2" +...
      SNode pe = node;
      do {
        if (SNodeOperations.isInstanceOf(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.StringLiteral$xu)) {
          break;
        }
        // expect null if not PE
        pe = SNodeOperations.as(SLinkOperations.getTarget(pe, LINKS.leftExpression$sEj), CONCEPTS.PlusExpression$k0);
      } while (pe != null);
      return pe != null;
    }


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

  }

  private static final class LINKS {
    /*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 operand$w6IR = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, 0x116b46a4416L, "operand");
    /*package*/ static final SContainmentLink operation$gs9E = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, 0x116b46b36c4L, "operation");
    /*package*/ static final SContainmentLink arguments$3huj = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x67e388d6eb788475L, 0x67e388d6eb788650L, "arguments");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept StringLiteral$xu = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf93d565d10L, "jetbrains.mps.baseLanguage.structure.StringLiteral");
    /*package*/ static final SConcept PlusExpression$k0 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7fbL, "jetbrains.mps.baseLanguage.structure.PlusExpression");
    /*package*/ static final SConcept DotExpression$yW = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, "jetbrains.mps.baseLanguage.structure.DotExpression");
    /*package*/ static final SConcept FormatOperation$63 = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x67e388d6eb788475L, "jetbrains.mps.baseLanguage.structure.FormatOperation");
  }

  private static final class PROPS {
    /*package*/ static final SProperty value$w7MM = MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf93d565d10L, 0xf93d565d11L, "value");
  }
}
