package jetbrains.mps.baseLanguage.doubleDispatch.typesystem;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import java.util.Iterator;
import jetbrains.mps.lang.core.behavior.BaseConcept__BehaviorDescriptor;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;

/**
 * This describes a group of related dispatch methods, i.e. which dispatch to each other
 */
public class DispatchGroupDescriptor {
  private String methodName;
  private boolean isStatic;
  private Iterable<SNode> otherParamTypes;
  public DispatchGroupDescriptor(SNode pattern) {
    methodName = SPropertyOperations.getString(pattern, PROPS.name$MnvL);
    isStatic = SNodeOperations.isInstanceOf(pattern, CONCEPTS.StaticMethodDeclaration$FJ);
    otherParamTypes = ListSequence.fromList(SLinkOperations.getChildren(pattern, LINKS.parameter$5xBj)).skip(1).select((it) -> SLinkOperations.getTarget(it, LINKS.type$a1UY));
  }
  @Override
  public boolean equals(Object o) {
    if (!(o instanceof DispatchGroupDescriptor)) {
      return false;
    }

    DispatchGroupDescriptor other = (DispatchGroupDescriptor) o;

    if (isStatic != other.isStatic) {
      return false;
    }
    if (!(methodName.equals(other.methodName))) {
      return false;
    }
    if (Sequence.fromIterable(otherParamTypes).count() != Sequence.fromIterable(other.otherParamTypes).count()) {
      return false;
    }

    {
      Iterator<SNode> ths_it = Sequence.fromIterable(otherParamTypes).iterator();
      Iterator<SNode> oth_it = Sequence.fromIterable(other.otherParamTypes).iterator();
      SNode ths_var;
      SNode oth_var;
      while (ths_it.hasNext() && oth_it.hasNext()) {
        ths_var = ths_it.next();
        oth_var = oth_it.next();
        if (!(typesEqual(ths_var, oth_var))) {
          return false;
        }
      }
    }

    return true;
  }
  @Override
  public int hashCode() {
    return methodName.hashCode() + ((isStatic ? 1 : 0)) + Sequence.fromIterable(otherParamTypes).foldLeft(0, (Integer s, SNode it) -> s + typeHashCode(it));
  }
  private boolean typesEqual(SNode typ1, SNode typ2) {
    {
      final SNode cls = typ1;
      if (SNodeOperations.isInstanceOf(cls, CONCEPTS.ClassifierType$bL)) {
        {
          final SNode cls2 = typ2;
          if (SNodeOperations.isInstanceOf(cls2, CONCEPTS.ClassifierType$bL)) {
            return SLinkOperations.getTarget(cls, LINKS.classifier$cxMr) == SLinkOperations.getTarget(cls2, LINKS.classifier$cxMr);
          }
        }
        return false;
      }
    }
    if (SNodeOperations.isInstanceOf(typ2, CONCEPTS.ClassifierType$bL)) {
      return false;
    }
    return BaseConcept__BehaviorDescriptor.getPresentation_idhEwIMiw.invoke(typ1).equals(BaseConcept__BehaviorDescriptor.getPresentation_idhEwIMiw.invoke(typ2));
  }
  private int typeHashCode(SNode typ) {
    {
      final SNode cls = typ;
      if (SNodeOperations.isInstanceOf(cls, CONCEPTS.ClassifierType$bL)) {
        return SLinkOperations.getTarget(cls, LINKS.classifier$cxMr).hashCode();
      }
    }
    return BaseConcept__BehaviorDescriptor.getPresentation_idhEwIMiw.invoke(typ).hashCode();
  }

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

  private static final class CONCEPTS {
    /*package*/ static final SConcept StaticMethodDeclaration$FJ = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbbebabf0aL, "jetbrains.mps.baseLanguage.structure.StaticMethodDeclaration");
    /*package*/ static final SConcept ClassifierType$bL = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink parameter$5xBj = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b1fcL, 0xf8cc56b1feL, "parameter");
    /*package*/ static final SContainmentLink type$a1UY = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type");
    /*package*/ static final SReferenceLink classifier$cxMr = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier");
  }
}
