package jetbrains.mps.lang.text.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.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.action.SNodeFactoryOperations;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.openapi.editor.selection.SelectionManager;
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 org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SInterfaceConcept;

public class Word_ActionMap {

  /*package*/ static AbstractCellAction createAction_PASTE(final SNode node) {
    return new AbstractCellAction() {
      public void execute(EditorContext editorContext) {
        this.execute_internal(editorContext, node);
      }
      public void execute_internal(EditorContext editorContext, SNode node) {
        PasteHandler.paste(editorContext, node);
      }

    };
  }
  /*package*/ static AbstractCellAction createAction_BACKSPACE(final SNode node) {
    return new AbstractCellAction() {
      public void execute(EditorContext editorContext) {
        this.execute_internal(editorContext, node);
      }
      public void execute_internal(EditorContext editorContext, SNode node) {
        // Remove a bullet line
        if ((SNodeOperations.getPrevSibling(node) == null) && !(SConceptOperations.isExactly(SNodeOperations.asSConcept(SNodeOperations.getConcept(SNodeOperations.getParent(node))), CONCEPTS.Line$yC)) && ((SNodeOperations.getPrevSibling(SNodeOperations.getParent(node)) == null) || (SNodeOperations.as(SNodeOperations.getNextSibling(SNodeOperations.getParent(node)), CONCEPTS.IndentedPoint$BF) == null))) {
          SNode l = SNodeFactoryOperations.replaceWithNewChild(SNodeOperations.getParent(node), CONCEPTS.Line$yC);
          SelectionUtil.selectLabelCellAnSetCaret(editorContext, l, SelectionManager.FIRST_EDITABLE_CELL, 0);
        } else {
          TextDeleteStrategyFactory.createDeleteStrategy(node, editorContext, false).execute();
        }
      }

    };
  }
  /*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) {
        TextDeleteStrategyFactory.createDeleteStrategy(node, editorContext, true).execute();
      }

    };
  }
  /*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) {
        NewElementStrategyFactory.createNewLineStrategy(SNodeOperations.cast(node, CONCEPTS.Word$Dn), editorContext, true, false).execute();
      }

    };
  }
  /*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) {
        NewElementStrategyFactory.createNewLineStrategy(SNodeOperations.cast(node, CONCEPTS.Word$Dn), editorContext, true, false).execute();
      }

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

    };
  }

  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.PASTE, createAction_PASTE(node));
    editorCell.setAction(CellActionType.BACKSPACE, createAction_BACKSPACE(node));
    editorCell.setAction(CellActionType.DELETE, createAction_DELETE(node));
    editorCell.setAction(CellActionType.INSERT, createAction_INSERT(node));
    editorCell.setAction(CellActionType.INSERT_BEFORE, createAction_INSERT_BEFORE(node));
    editorCell.setAction(CellActionType.COMMENT, createAction_COMMENT(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.PASTE)) {
      editorCell.setAction(actionType, createAction_PASTE(node));
    }
    if (Objects.equals(actionType, CellActionType.BACKSPACE)) {
      editorCell.setAction(actionType, createAction_BACKSPACE(node));
    }
    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));
    }
    if (Objects.equals(actionType, CellActionType.COMMENT)) {
      editorCell.setAction(actionType, createAction_COMMENT(node));
    }
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept Line$yC = MetaAdapterFactory.getConcept(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x2331694e561af166L, "jetbrains.mps.lang.text.structure.Line");
    /*package*/ static final SInterfaceConcept IndentedPoint$BF = MetaAdapterFactory.getInterfaceConcept(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x46ded40cf13ae6c4L, "jetbrains.mps.lang.text.structure.IndentedPoint");
    /*package*/ static final SConcept Word$Dn = MetaAdapterFactory.getConcept(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x229012ddae35f04L, "jetbrains.mps.lang.text.structure.Word");
  }
}
