package jetbrains.mps.build.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 java.util.List;
import jetbrains.mps.vfs.IFile;
import jetbrains.mps.build.util.Context;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.vfs.FileSystem;
import jetbrains.mps.internal.collections.runtime.Sequence;
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.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SConcept;

public final class BuildCompositePath__BehaviorDescriptor extends BaseBHDescriptor {
  private static final SAbstractConcept CONCEPT = MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x779c6e65c01467f1L, "jetbrains.mps.build.structure.BuildCompositePath");

  public static final SMethod<String> getPath_id7usrAn05okK = new SMethodBuilder<String>(new SJavaCompoundTypeImpl(String.class)).name("getPath").modifiers(0, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(8618885170173674800L).languageId(0xb99171f8c50ce5d2L, 0x798100da4f0a421aL).build2();
  public static final SMethod<String> getLastSegment_id1bWeed$oUbg = new SMethodBuilder<String>(new SJavaCompoundTypeImpl(String.class)).name("getLastSegment").modifiers(0, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(1368030936106771152L).languageId(0xb99171f8c50ce5d2L, 0x798100da4f0a421aL).build2();
  public static final SMethod<String> getPathToHead_id3skBIrUiCOV = new SMethodBuilder<String>(new SJavaCompoundTypeImpl(String.class)).name("getPathToHead").modifiers(0, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(3968971886499106107L).languageId(0xb99171f8c50ce5d2L, 0x798100da4f0a421aL).build2();
  public static final SMethod<List<String>> getHeadSuggestions_id4jjtc7X04td = new SMethodBuilder<List<String>>(new SJavaCompoundTypeImpl((Class<List<String>>) ((Class) Object.class))).name("getHeadSuggestions").modifiers(0, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(4959435991187212109L).languageId(0xb99171f8c50ce5d2L, 0x798100da4f0a421aL).build2(SMethodBuilder.createJavaParameter(String.class, ""));
  public static final SMethod<IFile> getFile_idIG8oXsODon = new SMethodBuilder<IFile>(new SJavaCompoundTypeImpl(IFile.class)).name("getFile").modifiers(0, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(841084130032784919L).languageId(0xb99171f8c50ce5d2L, 0x798100da4f0a421aL).build2(SMethodBuilder.createJavaParameter(Context.class, ""));

  private static final List<SMethod<?>> BH_METHODS = Arrays.<SMethod<?>>asList(getPath_id7usrAn05okK, getLastSegment_id1bWeed$oUbg, getPathToHead_id3skBIrUiCOV, getHeadSuggestions_id4jjtc7X04td, getFile_idIG8oXsODon);

  private static void ___init___(@NotNull SNode __thisNode__) {
    SPropertyOperations.set(__thisNode__, PROPS.head$$gC$, "");
  }

  /*package*/ static String getPath_id7usrAn05okK(@NotNull SNode __thisNode__) {
    if ((SLinkOperations.getTarget(__thisNode__, LINKS.tail$$gpz) != null)) {
      return SPropertyOperations.getString(__thisNode__, PROPS.head$$gC$) + "/" + BuildCompositePath__BehaviorDescriptor.getPath_id7usrAn05okK.invoke(SLinkOperations.getTarget(__thisNode__, LINKS.tail$$gpz));
    }
    return SPropertyOperations.getString(__thisNode__, PROPS.head$$gC$);
  }
  /*package*/ static String getLastSegment_id1bWeed$oUbg(@NotNull SNode __thisNode__) {
    SNode last = __thisNode__;
    while ((SLinkOperations.getTarget(last, LINKS.tail$$gpz) != null)) {
      last = SLinkOperations.getTarget(last, LINKS.tail$$gpz);
    }
    return SPropertyOperations.getString(last, PROPS.head$$gC$);
  }
  /*package*/ static String getPathToHead_id3skBIrUiCOV(@NotNull SNode __thisNode__) {
    if (SNodeOperations.isInstanceOf(SNodeOperations.getParent(__thisNode__), CONCEPTS.BuildCompositePath$Eh)) {
      return BuildCompositePath__BehaviorDescriptor.getPathToHead_id3skBIrUiCOV.invoke(SNodeOperations.cast(SNodeOperations.getParent(__thisNode__), CONCEPTS.BuildCompositePath$Eh)) + "/" + SPropertyOperations.getString(__thisNode__, PROPS.head$$gC$);
    }
    return SPropertyOperations.getString(__thisNode__, PROPS.head$$gC$);
  }
  /*package*/ static List<String> getHeadSuggestions_id4jjtc7X04td(@NotNull SNode __thisNode__, String basePath) {
    String relativePath = basePath;
    if (relativePath == null) {
      return ListSequence.fromList(new ArrayList<String>());
    }

    if (SNodeOperations.isInstanceOf(SNodeOperations.getParent(__thisNode__), CONCEPTS.BuildCompositePath$Eh)) {
      relativePath += "/" + BuildCompositePath__BehaviorDescriptor.getPathToHead_id3skBIrUiCOV.invoke(SNodeOperations.cast(SNodeOperations.getParent(__thisNode__), CONCEPTS.BuildCompositePath$Eh));
    }

    IFile file = FileSystem.getInstance().getFile(relativePath);
    if (!(file.exists())) {
      return ListSequence.fromList(new ArrayList<String>());
    }
    List<IFile> children = file.getChildren();
    Iterable<String> names = ListSequence.fromList(children).select((it) -> it.getName());
    if (file.getParent() != null) {
      names = Sequence.fromIterable(names).union(Sequence.fromIterable(Sequence.<String>singleton("..")));
    }
    return Sequence.fromIterable(names).sort((it) -> it, true).toList();
  }
  /*package*/ static IFile getFile_idIG8oXsODon(@NotNull SNode __thisNode__, Context context) {
    String basePath = BuildRelativePath__BehaviorDescriptor.getBasePath_id4jjtc7WZMYz.invoke(SNodeOperations.getNodeAncestor(__thisNode__, CONCEPTS.BuildRelativePath$Kc, false, false), context);
    if ((basePath == null || basePath.length() == 0)) {
      return null;
    }

    basePath += "/" + BuildCompositePath__BehaviorDescriptor.getPathToHead_id3skBIrUiCOV.invoke(__thisNode__);
    return FileSystem.getInstance().getFile(basePath);
  }

  /*package*/ BuildCompositePath__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) ((String) getPath_id7usrAn05okK(node));
      case 1:
        return (T) ((String) getLastSegment_id1bWeed$oUbg(node));
      case 2:
        return (T) ((String) getPathToHead_id3skBIrUiCOV(node));
      case 3:
        return (T) ((List<String>) getHeadSuggestions_id4jjtc7X04td(node, (String) parameters[0]));
      case 4:
        return (T) ((IFile) getFile_idIG8oXsODon(node, (Context) parameters[0]));
      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 final class PROPS {
    /*package*/ static final SProperty head$$gC$ = MetaAdapterFactory.getProperty(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x779c6e65c01467f1L, 0x779c6e65c01467f3L, "head");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink tail$$gpz = MetaAdapterFactory.getContainmentLink(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x779c6e65c01467f1L, 0x779c6e65c01467f2L, "tail");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept BuildCompositePath$Eh = MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x779c6e65c01467f1L, "jetbrains.mps.build.structure.BuildCompositePath");
    /*package*/ static final SConcept BuildRelativePath$Kc = MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x65997a657729f6fbL, "jetbrains.mps.build.structure.BuildRelativePath");
  }
}
