package jetbrains.mps.baseLanguage.editor;

/*Generated by MPS */

import jetbrains.mps.openapi.editor.EditorContext;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.text.editor.WordRangeSelection;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.ide.datatransfer.CopyPasteUtil;
import java.util.List;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.nodeEditor.cells.EditorCell_Label;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;

public class SingleAndMultiLineCommentUtil {

  public static void copySLCIntoClipboard(EditorContext editorContext, SNode comment) {
    WordRangeSelection selection = new WordRangeSelection(editorContext.getEditorComponent(), ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(comment, LINKS.line$9eiT), LINKS.elements$_j45)).first(), ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(comment, LINKS.line$9eiT), LINKS.elements$_j45)).last(), true);
    CopyPasteUtil.copyNodesAndTextToClipboard((List<SNode>) List.of(comment), null, "// " + selection.buildTextualRepresentationOfSelectedCells());
  }
  public static void copyMLCIntoClipboard(EditorContext editorContext, SNode comment) {
    WordRangeSelection selection = new WordRangeSelection(editorContext.getEditorComponent(), ListSequence.fromList(SLinkOperations.getChildren(ListSequence.fromList(SLinkOperations.getChildren(comment, LINKS.lines$lpTr)).first(), LINKS.elements$_j45)).first(), ListSequence.fromList(SLinkOperations.getChildren(ListSequence.fromList(SLinkOperations.getChildren(comment, LINKS.lines$lpTr)).last(), LINKS.elements$_j45)).last(), true);
    CopyPasteUtil.copyNodesAndTextToClipboard((List<SNode>) List.of(comment), null, "/*\n" + selection.buildTextualRepresentationOfSelectedCells() + "*/ ");
  }

  /**
   * 
   * @deprecated 
   */
  @Deprecated
  public static void divideSingleLineCommentText(SNode node, EditorContext editorContext) {

    SNode firstComment = SNodeOperations.as(SNodeOperations.getParent(node), CONCEPTS.SingleLineComment$Kw);
    if ((firstComment == null)) {
      return;
    }

    EditorCell_Label editorCell = ((EditorCell_Label) editorContext.getSelectedCell());
    int caretPosition = editorCell.getCaretPosition();
    String text = editorCell.getText();
    String leftPart = text.substring(0, caretPosition);
    String rightPart = text.substring(caretPosition);

    int indexInParent = SNodeOperations.getIndexInParent(node);
    if ((rightPart != null && rightPart.length() > 0) || ListSequence.fromList(SLinkOperations.getChildren(firstComment, LINKS.commentPart$Ib1g)).count() > indexInParent + 1) {
      SNode secondComment = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3aL, "jetbrains.mps.baseLanguage.structure.SingleLineComment"));
      SNodeOperations.insertNextSiblingChild(firstComment, secondComment);
      if ((rightPart != null && rightPart.length() > 0)) {
        SNode secondTextPart = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3dL, "jetbrains.mps.baseLanguage.structure.TextCommentPart"));
        ListSequence.fromList(SLinkOperations.getChildren(secondComment, LINKS.commentPart$Ib1g)).addElement(secondTextPart);

        SPropertyOperations.assign(node, PROPS.text$ag2i, leftPart);
        SPropertyOperations.assign(secondTextPart, PROPS.text$ag2i, rightPart);
      }

      while (ListSequence.fromList(SLinkOperations.getChildren(firstComment, LINKS.commentPart$Ib1g)).count() > indexInParent + 1) {
        SNode part = ListSequence.fromList(SLinkOperations.getChildren(firstComment, LINKS.commentPart$Ib1g)).getElement(indexInParent + 1);
        SNodeOperations.deleteNode(part);
        ListSequence.fromList(SLinkOperations.getChildren(secondComment, LINKS.commentPart$Ib1g)).addElement(part);
      }
      if (isEmptyString(SPropertyOperations.getString(node, PROPS.text$ag2i)) && SNodeOperations.getIndexInParent(node) != 0) {
        SNodeOperations.deleteNode(node);
      }

      editorContext.selectWRTFocusPolicy(ListSequence.fromList(SLinkOperations.getChildren(secondComment, LINKS.commentPart$Ib1g)).first());
      ((EditorCell_Label) editorContext.getSelectedCell()).setCaretPosition(0);
    } else {
      SNode stmt = SNodeOperations.insertNextSiblingChild(firstComment, SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b215L, "jetbrains.mps.baseLanguage.structure.Statement")));
      editorContext.selectWRTFocusPolicy(stmt);

    }


  }


  private static boolean isEmptyString(String str) {
    return str == null || str.isEmpty();
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink line$9eiT = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3aL, 0x73f69d82391da738L, "line");
    /*package*/ static final SContainmentLink elements$_j45 = MetaAdapterFactory.getContainmentLink(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x2331694e561af166L, 0x2331694e561af167L, "elements");
    /*package*/ static final SContainmentLink lines$lpTr = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x1809ed668dda555fL, 0x1809ed668ddac789L, "lines");
    /*package*/ static final SContainmentLink commentPart$Ib1g = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3aL, 0x57d533a7af16ff73L, "commentPart");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept SingleLineComment$Kw = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3aL, "jetbrains.mps.baseLanguage.structure.SingleLineComment");
  }

  private static final class PROPS {
    /*package*/ static final SProperty text$ag2i = MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x57d533a7af15ed3dL, 0x57d533a7af15ed3eL, "text");
  }
}
