package jetbrains.mps.lang.behavior.generator.template.util;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.baseLanguage.behavior.BaseMethodDeclaration__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.smodel.JavaFriendlyBase64;
import jetbrains.mps.smodel.SNodeId;
import jetbrains.mps.generator.template.TemplateQueryContext;
import java.util.UUID;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.behavior.behavior.ConceptMethodDeclaration__BehaviorDescriptor;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SReferenceLink;

/**
 * TODO need to be moved to the behavior of the ConceptBehavior after MPS project rebuilt
 */
public final class ConceptMethodGenerationHelper {
  private final SNode myMethod;
  private final SNode myBaseMethod;

  public ConceptMethodGenerationHelper(@NotNull SNode method) {
    myMethod = method;
    myBaseMethod = SNodeOperations.cast(BaseMethodDeclaration__BehaviorDescriptor.getBaseOrSelf_idpWw8KNYVCx.invoke(method), CONCEPTS.ConceptMethodDeclaration$N0);
  }

  @NotNull
  public String getGeneratedFieldName() {
    return getGeneratedName() + "_id" + getGeneratedId();
  }

  @NotNull
  public String getGeneratedName() {
    return SPropertyOperations.getString(myBaseMethod, PROPS.name$MnvL);
  }

  @NotNull
  private String getGeneratedId() {
    return new JavaFriendlyBase64().toString(getGeneratedIdLong());
  }

  public long getGeneratedIdLong() {
    return ((SNodeId.Regular) myBaseMethod.getNodeId()).getId();
  }

  public SNode getBaseMethod() {
    return myBaseMethod;
  }

  public long getCompressedLanguageId(TemplateQueryContext ctx) {
    UUID langId = new LanguageIdCalculator(ctx).calcLangId(SLinkOperations.getTarget(ConceptMethodDeclaration__BehaviorDescriptor.getBehaviour_idhP3pnNF.invoke(myBaseMethod), LINKS.concept$u6dL));
    return langId.getLeastSignificantBits() ^ langId.getMostSignificantBits();
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept ConceptMethodDeclaration$N0 = MetaAdapterFactory.getConcept(0xaf65afd8f0dd4942L, 0x87d963a55f2a9db1L, 0x11d4348057eL, "jetbrains.mps.lang.behavior.structure.ConceptMethodDeclaration");
  }

  private static final class PROPS {
    /*package*/ static final SProperty name$MnvL = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink concept$u6dL = MetaAdapterFactory.getReferenceLink(0xaf65afd8f0dd4942L, 0x87d963a55f2a9db1L, 0x11d43447b1aL, 0x11d43447b1fL, "concept");
  }
}
