package jetbrains.mps.console.ideCommands.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.core.aspects.behaviour.SMethodBuilder;
import jetbrains.mps.core.aspects.behaviour.SJavaCompoundTypeImpl;
import jetbrains.mps.core.aspects.behaviour.AccessPrivileges;
import jetbrains.mps.console.tool.ConsoleContext;
import jetbrains.mps.console.tool.ConsoleStream;
import java.util.List;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.model.SReference;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.project.Project;
import jetbrains.mps.ide.findusages.view.UsagesViewTool;
import jetbrains.mps.ide.project.ProjectHelper;
import jetbrains.mps.ide.findusages.model.SearchResult;
import java.util.ArrayList;
import jetbrains.mps.ide.findusages.model.SearchResults;
import java.util.Collections;
import jetbrains.mps.ide.findusages.view.treeholder.treeview.NodeRepresentatorBase;
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;

public final class ShowBrokenReferences__BehaviorDescriptor extends BaseBHDescriptor {
  private static final SAbstractConcept CONCEPT = MetaAdapterFactory.getConcept(0xa5e4de5346a344daL, 0xaab368fdf1c34ed0L, 0x1cf75b72b0ac828cL, "jetbrains.mps.console.ideCommands.structure.ShowBrokenReferences");

  public static final SMethod<Void> doExecute_id2SpVAIqougW = new SMethodBuilder<Void>(new SJavaCompoundTypeImpl(Void.class)).name("doExecute").modifiers(8, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(3321948346081469500L).languageId(0xb306d4d17f64c375L, 0xde1ad86d6e504a02L).build2(SMethodBuilder.createJavaParameter(ConsoleContext.class, ""), SMethodBuilder.createJavaParameter(ConsoleStream.class, ""));

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

  private static void ___init___(@NotNull SNode __thisNode__) {
  }

  /*package*/ static void doExecute_id2SpVAIqougW(@NotNull SNode __thisNode__, final ConsoleContext context, ConsoleStream console) {
    final List<SReference> brokenReferences = Sequence.fromIterable(INodeSetReference__BehaviorDescriptor.getNodes_id4x3U0fq41hN.invoke(SLinkOperations.getTarget(__thisNode__, LINKS.target$s$Bi), context)).translate((it) -> SNodeOperations.getReferences(it)).where((it) -> jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(it) == null).toList();
    if (ListSequence.fromList(brokenReferences).count() == 0) {
      console.addText("no broken references");
    } else {
      console.addClosure(() -> {
        Project project = context.getProject();
        final UsagesViewTool tool = check_5hdxhn_a0b0a0a0a0b0a(ProjectHelper.toIdeaProject(project));
        assert tool != null;
        project.getRepository().getModelAccess().runReadAction(() -> {
          final List<SearchResult> res = ListSequence.fromList(new ArrayList<SearchResult>());
          ListSequence.fromList(brokenReferences).visitAll((it) -> ListSequence.fromList(res).addElement(new SearchResult<SReference>(it, it.getSourceNode())));
          SearchResults sr = new SearchResults(Collections.emptyList(), res);
          tool.show(sr, "No results to show", new NodeRepresentatorBase<SReference>() {
            @Override
            public String getAdditionalInfo(SReference r) {
              return "model id = " + r.getTargetSModelReference() + "; node  id = " + r.getTargetNodeId();
            }
          });
        });
      }, ListSequence.fromList(brokenReferences).count() + " broken references");
    }
  }

  /*package*/ ShowBrokenReferences__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:
        doExecute_id2SpVAIqougW(node, (ConsoleContext) parameters[0], (ConsoleStream) parameters[1]);
        return null;
      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 UsagesViewTool check_5hdxhn_a0b0a0a0a0b0a(com.intellij.openapi.project.Project checkedDotOperand) {
    if (null != checkedDotOperand) {
      return checkedDotOperand.getComponent(UsagesViewTool.class);
    }
    return null;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink target$s$Bi = MetaAdapterFactory.getContainmentLink(0xa5e4de5346a344daL, 0xaab368fdf1c34ed0L, 0x1cf75b72b0ac828cL, 0x1cf75b72b0ac828dL, "target");
  }
}
