package jetbrains.mps.baseLanguage.javadoc.behavior;

/*Generated by MPS */

import jetbrains.mps.core.aspects.behaviour.BaseBHDescriptor;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.core.aspects.behaviour.api.SMethod;
import jetbrains.mps.baseLanguage.javadoc.editor.NodeCaretPair;
import jetbrains.mps.core.aspects.behaviour.SMethodBuilder;
import jetbrains.mps.core.aspects.behaviour.SJavaCompoundTypeImpl;
import jetbrains.mps.core.aspects.behaviour.AccessPrivileges;
import java.util.List;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.core.aspects.behaviour.api.SConstructor;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.core.aspects.behaviour.api.BHMethodNotFoundException;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;

public final class HTMLElement__BehaviorDescriptor extends BaseBHDescriptor {
  private static final SAbstractConcept CONCEPT = MetaAdapterFactory.getConcept(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x5bc4aa08e154b399L, "jetbrains.mps.baseLanguage.javadoc.structure.HTMLElement");

  public static final SMethod<NodeCaretPair> smartDelete_id7PYAiugbmRz = new SMethodBuilder<NodeCaretPair>(new SJavaCompoundTypeImpl(NodeCaretPair.class)).name("smartDelete").modifiers(8, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(9042833497008205283L).languageId(0xbb1b463a8781b786L, 0xf280165065d5424eL).build2(SMethodBuilder.createJavaParameter(Boolean.TYPE, ""));
  public static final SMethod<String> buildCommentText_id7Qt73fl2F3N = new SMethodBuilder<String>(new SJavaCompoundTypeImpl(String.class)).name("buildCommentText").modifiers(8, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(9051421835027722483L).languageId(0xbb1b463a8781b786L, 0xf280165065d5424eL).build2();

  private static final List<SMethod<?>> BH_METHODS = Arrays.<SMethod<?>>asList(smartDelete_id7PYAiugbmRz, buildCommentText_id7Qt73fl2F3N);

  private static void ___init___(@NotNull SNode __thisNode__) {
    SLinkOperations.addNewChild(__thisNode__, LINKS.line$Psfe, null);
  }

  /*package*/ static NodeCaretPair smartDelete_id7PYAiugbmRz(@NotNull SNode __thisNode__, boolean isBeginning) {
    SNode line = SNodeOperations.cast(SNodeOperations.getParent(__thisNode__), CONCEPTS.CommentLine$hJ);
    int index = SNodeOperations.getIndexInParent(__thisNode__);
    Iterable<SNode> lines = SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe);

    SNode nodeToSelect = null;
    int caret = -1;

    if (Sequence.fromIterable(lines).isEmpty()) {
      SNode prev = SNodeOperations.getPrevSibling(__thisNode__);
      if (isNotEmptyString(SPropertyOperations.getString(SNodeOperations.cast(prev, CONCEPTS.TextCommentLinePart$Eb), PROPS.text$aOLd))) {
        caret = SPropertyOperations.getString(SNodeOperations.cast(prev, CONCEPTS.TextCommentLinePart$Eb), PROPS.text$aOLd).length();
      }
      SNodeOperations.deleteNode(__thisNode__);
      CommentLine__BehaviorDescriptor.tryMergeToRight_idooaTF_3fF3.invoke(line, ((int) (index - 1)));
      return new NodeCaretPair(prev, caret);
    } else {
      //  Merging first line
      for (SNode part : ListSequence.fromList(SLinkOperations.getChildren(ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).first(), LINKS.part$QuzQ))) {
        SNodeOperations.insertPrevSiblingChild(__thisNode__, part);
      }
      SNodeOperations.deleteNode(ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).first());
      if (isBeginning) {
        nodeToSelect = ListSequence.fromList(SLinkOperations.getChildren(line, LINKS.part$QuzQ)).getElement(index - 1);
        caret = SPropertyOperations.getString(SNodeOperations.cast(ListSequence.fromList(SLinkOperations.getChildren(line, LINKS.part$QuzQ)).getElement(index - 1), CONCEPTS.TextCommentLinePart$Eb), PROPS.text$aOLd).length();
      }
      CommentLine__BehaviorDescriptor.tryMergeToRight_idooaTF_3fF3.invoke(line, ((int) (index - 1)));

      //  Merging other lines
      SNode lastElementLine = ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).last();
      for (SNode elementLine : ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).reversedList()) {
        SNodeOperations.insertNextSiblingChild(line, elementLine);
      }

      if ((lastElementLine == null)) {
        index = SNodeOperations.getIndexInParent(__thisNode__);
        SNodeOperations.deleteNode(__thisNode__);
        if (!(isBeginning)) {
          nodeToSelect = ListSequence.fromList(SLinkOperations.getChildren(line, LINKS.part$QuzQ)).getElement(index - 1);
          caret = SPropertyOperations.getString(SNodeOperations.cast(ListSequence.fromList(SLinkOperations.getChildren(line, LINKS.part$QuzQ)).getElement(index - 1), CONCEPTS.TextCommentLinePart$Eb), PROPS.text$aOLd).length();
        }
        CommentLine__BehaviorDescriptor.tryMergeToRight_idooaTF_3fF3.invoke(line, ((int) (index - 1)));
      } else {
        //  Merging last line parts
        index = ListSequence.fromList(SLinkOperations.getChildren(lastElementLine, LINKS.part$QuzQ)).count() - 1;
        for (SNode linePart : ListSequence.fromList(SNodeOperations.getNextSiblings(__thisNode__, false))) {
          ListSequence.fromList(SLinkOperations.getChildren(lastElementLine, LINKS.part$QuzQ)).addElement(SNodeOperations.cast(linePart, CONCEPTS.CommentLinePart$DG));
        }
        SNodeOperations.deleteNode(__thisNode__);
        if (!(isBeginning)) {
          nodeToSelect = ListSequence.fromList(SLinkOperations.getChildren(lastElementLine, LINKS.part$QuzQ)).getElement(index);
          caret = SPropertyOperations.getString(SNodeOperations.cast(ListSequence.fromList(SLinkOperations.getChildren(lastElementLine, LINKS.part$QuzQ)).getElement(index), CONCEPTS.TextCommentLinePart$Eb), PROPS.text$aOLd).length();
        }
        CommentLine__BehaviorDescriptor.tryMergeToRight_idooaTF_3fF3.invoke(lastElementLine, ((int) index));
      }
    }
    return new NodeCaretPair(nodeToSelect, caret);
  }
  /*package*/ static String buildCommentText_id7Qt73fl2F3N(@NotNull SNode __thisNode__) {
    if (ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).isNotEmpty()) {
      return "<" + SPropertyOperations.getString(__thisNode__, PROPS.name$Ps0d) + ">" + ListSequence.fromList(SLinkOperations.getChildren(__thisNode__, LINKS.line$Psfe)).select((it) -> (String) CommentLine__BehaviorDescriptor.buildCommentText_id7Qt73fl2z8k.invoke(it)).foldLeft("", (String s, String it) -> s + "\n" + it) + "</" + SPropertyOperations.getString(__thisNode__, PROPS.name$Ps0d) + ">";

    } else {
      return "<" + SPropertyOperations.getString(__thisNode__, PROPS.name$Ps0d) + "/>";
    }
  }

  /*package*/ HTMLElement__BehaviorDescriptor() {
  }

  @Override
  protected void initNode(@NotNull SNode node, @NotNull SConstructor constructor, @Nullable Object[] parameters) {
    ___init___(node);
  }

  @Override
  protected <T> T invokeSpecial0(@NotNull SNode node, @NotNull SMethod<T> method, @Nullable Object[] parameters) {
    int methodIndex = BH_METHODS.indexOf(method);
    if (methodIndex < 0) {
      throw new BHMethodNotFoundException(this, method);
    }
    switch (methodIndex) {
      case 0:
        return (T) ((NodeCaretPair) smartDelete_id7PYAiugbmRz(node, ((boolean) (Boolean) parameters[0])));
      case 1:
        return (T) ((String) buildCommentText_id7Qt73fl2F3N(node));
      default:
        throw new BHMethodNotFoundException(this, method);
    }
  }

  @Override
  protected <T> T invokeSpecial0(@NotNull SAbstractConcept concept, @NotNull SMethod<T> method, @Nullable Object[] parameters) {
    int methodIndex = BH_METHODS.indexOf(method);
    if (methodIndex < 0) {
      throw new BHMethodNotFoundException(this, method);
    }
    switch (methodIndex) {
      default:
        throw new BHMethodNotFoundException(this, method);
    }
  }

  @NotNull
  @Override
  public List<SMethod<?>> getDeclaredMethods() {
    return BH_METHODS;
  }

  @NotNull
  @Override
  public SAbstractConcept getConcept() {
    return CONCEPT;
  }
  private static boolean isNotEmptyString(String str) {
    return str != null && str.length() > 0;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink line$Psfe = MetaAdapterFactory.getContainmentLink(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x5bc4aa08e154b399L, 0x5bc4aa08e154b39bL, "line");
    /*package*/ static final SContainmentLink part$QuzQ = MetaAdapterFactory.getContainmentLink(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x757ba20a4c87f96cL, 0x7c7f5b2f3199028dL, "part");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept CommentLine$hJ = MetaAdapterFactory.getConcept(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x757ba20a4c87f96cL, "jetbrains.mps.baseLanguage.javadoc.structure.CommentLine");
    /*package*/ static final SConcept TextCommentLinePart$Eb = MetaAdapterFactory.getConcept(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x7c7f5b2f31990287L, "jetbrains.mps.baseLanguage.javadoc.structure.TextCommentLinePart");
    /*package*/ static final SConcept CommentLinePart$DG = MetaAdapterFactory.getConcept(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x7c7f5b2f31990286L, "jetbrains.mps.baseLanguage.javadoc.structure.CommentLinePart");
  }

  private static final class PROPS {
    /*package*/ static final SProperty text$aOLd = MetaAdapterFactory.getProperty(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x7c7f5b2f31990287L, 0x7c7f5b2f31990288L, "text");
    /*package*/ static final SProperty name$Ps0d = MetaAdapterFactory.getProperty(0xf280165065d5424eL, 0xbb1b463a8781b786L, 0x5bc4aa08e154b399L, 0x5bc4aa08e154b39aL, "name");
  }
}
