package jetbrains.mps.lang.messages.editor;

/*Generated by MPS */

import jetbrains.mps.editor.runtime.cells.AbstractCellAction;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.editor.runtime.deletionApprover.DeletionApproverUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.openapi.editor.selection.SelectionManager;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.openapi.editor.cells.CellAction;
import jetbrains.mps.openapi.editor.cells.CellActionType;
import jetbrains.mps.nodeEditor.cellProviders.AbstractCellListHandler;
import java.util.Objects;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class MacroMessageExpression_actions {

  /*package*/ static AbstractCellAction createAction_DELETE(final SNode node) {
    return new AbstractCellAction() {
      public void execute(EditorContext editorContext) {
        this.execute_internal(editorContext, node);
      }
      public void execute_internal(EditorContext editorContext, SNode node) {
        if (DeletionApproverUtil.approve(editorContext, node)) {
          return;
        }
        if (SNodeOperations.isInstanceOf(SNodeOperations.getPrevSibling(node), CONCEPTS.LiteralMessageExpression$$8) && SNodeOperations.isInstanceOf(SNodeOperations.getNextSibling(node), CONCEPTS.LiteralMessageExpression$$8)) {
          String prevMessage = SPropertyOperations.getString(SNodeOperations.cast(SNodeOperations.getPrevSibling(node), CONCEPTS.LiteralMessageExpression$$8), PROPS.message$umWY);
          String nextMessage = SPropertyOperations.getString(SNodeOperations.cast(SNodeOperations.getNextSibling(node), CONCEPTS.LiteralMessageExpression$$8), PROPS.message$umWY);
          SNode replacement = SNodeOperations.replaceWithAnother(node, createLiteralMessageExpression_jg075k_a0a0c0b0a0(prevMessage + nextMessage));
          SNodeOperations.deleteNode(SNodeOperations.getPrevSibling(replacement));
          SNodeOperations.deleteNode(SNodeOperations.getNextSibling(replacement));
          SelectionUtil.selectLabelCellAnSetCaret(editorContext, replacement, SelectionManager.FIRST_EDITABLE_CELL, prevMessage.length());
        } else if (ListSequence.fromList(SLinkOperations.getChildren(SNodeOperations.cast(SNodeOperations.getParent(node), CONCEPTS.CombinedMessageExpression$6z), LINKS.part$kCpv)).count() == 1) {
          SNodeOperations.deleteNode(SNodeOperations.getParent(node));
        } else {
          SNode toSelect = SNodeOperations.getParent(node);
          if (SNodeOperations.getPrevSibling(node) != null) {
            toSelect = SNodeOperations.getPrevSibling(node);
          }
          SNodeOperations.deleteNode(node);
          SelectionUtil.selectNode(editorContext, toSelect);
        }
      }

    };
  }
  /*package*/ static AbstractCellAction createAction_INSERT(final SNode node) {
    return new AbstractCellAction() {
      public void execute(EditorContext editorContext) {
        this.execute_internal(editorContext, node);
      }
      public void execute_internal(EditorContext editorContext, SNode node) {
        SNodeOperations.insertNextSiblingChild(node, createLiteralMessageExpression_jg075k_a0a0a0b());
      }

    };
  }
  /*package*/ static AbstractCellAction createAction_INSERT_BEFORE(final SNode node) {
    return new AbstractCellAction() {
      public void execute(EditorContext editorContext) {
        this.execute_internal(editorContext, node);
      }
      public void execute_internal(EditorContext editorContext, SNode node) {
        SNodeOperations.insertPrevSiblingChild(node, createLiteralMessageExpression_jg075k_a0a0a0c());
      }

    };
  }

  public static void setCellActions(EditorCell editorCell, SNode node, EditorContext context) {
    CellAction originalDelete = editorCell.getAction(CellActionType.DELETE);
    CellAction originalBackspace = editorCell.getAction(CellActionType.BACKSPACE);

    // set actions that were actually defined
    setDefinedCellActions(editorCell, node, context);

    // If we set a DELETE action but no BACKSPACE action,
    // use the DELETE action for BACKSPACE as well.
    CellAction delete = editorCell.getAction(CellActionType.DELETE);
    CellAction backspace = editorCell.getAction(CellActionType.BACKSPACE);
    if (delete != originalDelete && backspace == originalBackspace) {
      editorCell.setAction(CellActionType.BACKSPACE, delete);
    }
    if (delete != originalDelete) {
      editorCell.putUserObject(AbstractCellListHandler.ELEMENT_CELL_DELETE_SET, OB);
    }
    if (backspace != originalBackspace) {
      editorCell.putUserObject(AbstractCellListHandler.ELEMENT_CELL_BACKSPACE_SET, OB);
    }
  }

  private static final Object OB = new Object();

  public static void setDefinedCellActions(EditorCell editorCell, SNode node, EditorContext context) {
    // set cell actions from all imported action maps

    // set cell actions defined directly in this action map
    editorCell.setAction(CellActionType.DELETE, createAction_DELETE(node));
    editorCell.setAction(CellActionType.INSERT, createAction_INSERT(node));
    editorCell.setAction(CellActionType.INSERT_BEFORE, createAction_INSERT_BEFORE(node));
  }

  public static void setDefinedCellActionsOfType(EditorCell editorCell, SNode node, EditorContext context, CellActionType actionType) {

    // set cell action(s) of the given type from imported action maps

    // set cell action of the given type defined directly in this action map
    if (Objects.equals(actionType, CellActionType.DELETE)) {
      editorCell.setAction(actionType, createAction_DELETE(node));
    }
    if (Objects.equals(actionType, CellActionType.INSERT)) {
      editorCell.setAction(actionType, createAction_INSERT(node));
    }
    if (Objects.equals(actionType, CellActionType.INSERT_BEFORE)) {
      editorCell.setAction(actionType, createAction_INSERT_BEFORE(node));
    }
  }
  private static SNode createLiteralMessageExpression_jg075k_a0a0c0b0a0(String p0) {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
    n0.setProperty(PROPS.message$umWY, p0);
    return n0.getResult();
  }
  private static SNode createLiteralMessageExpression_jg075k_a0a0a0b() {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
    n0.setProperty(PROPS.message$umWY, "");
    return n0.getResult();
  }
  private static SNode createLiteralMessageExpression_jg075k_a0a0a0c() {
    SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
    n0.setProperty(PROPS.message$umWY, "");
    return n0.getResult();
  }

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

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

  private static final class LINKS {
    /*package*/ static final SContainmentLink part$kCpv = MetaAdapterFactory.getContainmentLink(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e40455fL, 0x48f860fc0e404561L, "part");
  }
}
