package jetbrains.mps.lang.messages.editor;

/*Generated by MPS */

import jetbrains.mps.openapi.editor.EditorContext;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.openapi.editor.selection.Selection;
import jetbrains.mps.openapi.editor.selection.SingularSelection;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.openapi.editor.cells.EditorCell_Label;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.openapi.editor.selection.SelectionManager;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SConcept;

public final class LiteralMessageSplitHelper {
  private final EditorContext myEditorContext;
  private final SNode myMessage;

  public LiteralMessageSplitHelper(EditorContext editorContext, SNode message) {
    myEditorContext = editorContext;
    myMessage = message;
  }

  public void insertMacro(SNode target) {
    SNode macro = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e41e4a2L, "jetbrains.mps.lang.messages.structure.MacroMessageExpression"));
    if ((target != null)) {
      SLinkOperations.setTarget(macro, LINKS.defRef$oUPH, SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xea3159bff48e4720L, 0xbde286dba75f0d34L, 0x6530303593574311L, "jetbrains.mps.lang.context.defs.structure.TypedDefReference")));
      SLinkOperations.setTarget(SLinkOperations.getTarget(macro, LINKS.defRef$oUPH), LINKS.declaration$xU6a, target);
    }
    insertMessageExpr(macro);
  }

  public void insertMessageExpr(SNode messageExpression) {
    Selection selection = myEditorContext.getSelectionManager().getSelection();
    if (selection instanceof SingularSelection) {
      EditorCell selectedCell = ((SingularSelection) selection).getEditorCell();
      if (selectedCell instanceof EditorCell_Label) {
        int splitPosition = ((EditorCell_Label) selectedCell).getCaretPosition();
        String messageToSplit = SPropertyOperations.getString(myMessage, PROPS.message$umWY);
        List<SNode> messages = ListSequence.fromList(new ArrayList<SNode>());
        if (splitPosition > 0) {
          ListSequence.fromList(messages).addElement(createLiteralMessageExpression_fsei94_a0a0a3a1a1a7(messageToSplit.substring(0, splitPosition)));
        }
        if (messageExpression != null) {
          ListSequence.fromList(messages).addElement(messageExpression);
        }
        if (messageToSplit != null && splitPosition < messageToSplit.length()) {
          ListSequence.fromList(messages).addElement(createLiteralMessageExpression_fsei94_a0a0a5a1a1a7(messageToSplit.substring(splitPosition)));
        }
        if (SNodeOperations.hasRole(myMessage, LINKS.part$kCpv)) {
          SLinkOperations.getChildren(SNodeOperations.cast(SNodeOperations.getParent(myMessage), CONCEPTS.CombinedMessageExpression$6z), LINKS.part$kCpv).addAll(SNodeOperations.getIndexInParent(myMessage), messages);
          SNodeOperations.deleteNode(myMessage);
        } else {
          SNodeOperations.replaceWithAnother(myMessage, createCombinedMessageExpression_fsei94_a0a0a0g0b0b0h(messages));
        }
        if ((messageExpression != null)) {
          SelectionUtil.selectCell(myEditorContext, messageExpression, SelectionManager.FIRST_EDITABLE_CELL);
        }
      }
    }
  }
  private static SNode createLiteralMessageExpression_fsei94_a0a0a3a1a1a7(String p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
    n0.setProperty(PROPS.message$umWY, p0);
    return n0.getResult();
  }
  private static SNode createLiteralMessageExpression_fsei94_a0a0a5a1a1a7(String p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
    n0.setProperty(PROPS.message$umWY, p0);
    return n0.getResult();
  }
  private static SNode createCombinedMessageExpression_fsei94_a0a0a0g0b0b0h(Iterable<? extends SNode> p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.CombinedMessageExpression$6z);
    n0.forChild(LINKS.part$kCpv).initNodeList(p0, CONCEPTS.MessageExpression$zD);
    return n0.getResult();
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink defRef$oUPH = MetaAdapterFactory.getContainmentLink(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e41e4a2L, 0x25b197ac2c49e208L, "defRef");
    /*package*/ static final SReferenceLink declaration$xU6a = MetaAdapterFactory.getReferenceLink(0xea3159bff48e4720L, 0xbde286dba75f0d34L, 0x6530303593574311L, 0x6530303593578e5eL, "declaration");
    /*package*/ static final SContainmentLink part$kCpv = MetaAdapterFactory.getContainmentLink(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e40455fL, 0x48f860fc0e404561L, "part");
  }

  private static final class PROPS {
    /*package*/ static final SProperty message$umWY = MetaAdapterFactory.getProperty(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e362dc5L, 0x48f860fc0e362dc6L, "message");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept CombinedMessageExpression$6z = MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e40455fL, "jetbrains.mps.lang.messages.structure.CombinedMessageExpression");
    /*package*/ static final SConcept LiteralMessageExpression$$8 = MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e362dc5L, "jetbrains.mps.lang.messages.structure.LiteralMessageExpression");
    /*package*/ static final SConcept MessageExpression$zD = MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e362dc4L, "jetbrains.mps.lang.messages.structure.MessageExpression");
  }
}
