package jetbrains.mps.baseLanguage.methodReferences.behavior;

/*Generated by MPS */

import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.baseLanguage.behavior.IFixableMethodReference__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.baseLanguage.behavior.ResolveUnknownUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import java.util.Objects;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.baseLanguage.behavior.IMemberContainer__BehaviorDescriptor;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SConcept;

public final class ResolveUnknownMethodRefUtil {
  private ResolveUnknownMethodRefUtil() {
  }

  public static _FunctionTypes._return_P0_E0<? extends SNode> resolveMethodRef(final SNode x) {
    if (!(SPropertyOperations.getBoolean(x, PROPS.targetResolved$e43v))) {
      // Resolve target expression
      return resolveMethodRefTarget(x);
    } else {
      // If target expression resolved, try to solve the method
      final SNode targetMethod = Sequence.fromIterable(IFixableMethodReference__BehaviorDescriptor.getAvailableMethodDeclarations_id50EF2fWdwEN.invoke(x, SPropertyOperations.getString(x, PROPS.methodName$oxdi))).first();

      if ((targetMethod != null)) {
        return () -> {
          SNode mRef = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x34d254ec4f4136fL, "jetbrains.mps.baseLanguage.methodReferences.structure.MethodReference"));
          SLinkOperations.setTarget(mRef, LINKS.target$Woec, SLinkOperations.getTarget(x, LINKS.target$Woec));
          SLinkOperations.setTarget(mRef, LINKS.method$8Sfb, targetMethod);
          reattachTypeArguments(x, mRef);
          return mRef;
        };
      }
    }

    return null;
  }

  public static _FunctionTypes._return_P0_E0<? extends SNode> resolveMethodRefTarget(final SNode x) {
    final SNode operand = ResolveUnknownUtil.resolveTokens(x);

    if ((operand == null)) {
      return null;
    }

    if (SNodeOperations.isInstanceOf(operand, CONCEPTS.Classifier$Ix)) {
      final SNode target = ((operand == null) ? null : SNodeOperations.cast(operand, CONCEPTS.Classifier$Ix));

      return () -> {
        SNode mRef;

        // Usual method ref or constructor
        if (!(Objects.equals(SPropertyOperations.getString(x, PROPS.methodName$oxdi), "<init>"))) {
          mRef = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x4b3463a3c4af5feeL, "jetbrains.mps.baseLanguage.methodReferences.structure.UnknownMethodReference"));
          SPropertyOperations.assign(SNodeOperations.cast(mRef, CONCEPTS.UnknownMethodReference$lD), PROPS.methodName$oxdi, SPropertyOperations.getString(x, PROPS.methodName$oxdi));
          SPropertyOperations.assign(SNodeOperations.cast(mRef, CONCEPTS.UnknownMethodReference$lD), PROPS.targetResolved$e43v, true);
        } else {
          // Reference any constructor, auto fixing will then resolve the correct one
          mRef = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x34d254ec4f4136fL, "jetbrains.mps.baseLanguage.methodReferences.structure.MethodReference"));
          SLinkOperations.setTarget(mRef, LINKS.method$8Sfb, SNodeOperations.as(ListSequence.fromList(IMemberContainer__BehaviorDescriptor.getMembers_idhEwJjl2.invoke(target)).findFirst((it) -> SNodeOperations.isInstanceOf(it, CONCEPTS.ConstructorDeclaration$yG)), CONCEPTS.ConstructorDeclaration$yG));
        }

        // Resolve target
        SNode clType = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType"));
        SLinkOperations.setTarget(clType, LINKS.classifier$cxMr, target);

        SLinkOperations.setTarget(mRef, LINKS.target$Woec, SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x6dd7c320c6fc97cdL, "jetbrains.mps.baseLanguage.methodReferences.structure.MethodReferenceTypeTargetExpression")));
        SLinkOperations.setTarget(SNodeOperations.cast(SLinkOperations.getTarget(mRef, LINKS.target$Woec), CONCEPTS.MethodReferenceTypeTargetExpression$a3), LINKS.type$z9_f, clType);

        reattachTypeArguments(x, mRef);
        return mRef;
      };

    } else if (SNodeOperations.isInstanceOf(operand, CONCEPTS.Expression$mB)) {
      // operand is some other expression. it's supposed to be an instance method call then

      return () -> {
        SNode mRef = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x4b3463a3c4af5feeL, "jetbrains.mps.baseLanguage.methodReferences.structure.UnknownMethodReference"));
        SLinkOperations.setTarget(mRef, LINKS.target$Woec, SNodeOperations.cast(operand, CONCEPTS.Expression$mB));
        SPropertyOperations.assign(mRef, PROPS.targetResolved$e43v, true);
        SPropertyOperations.assign(mRef, PROPS.methodName$oxdi, SPropertyOperations.getString(x, PROPS.methodName$oxdi));

        reattachTypeArguments(x, mRef);
        return mRef;
      };

    } else {
      return null;

    }

  }


  public static void reattachTypeArguments(SNode from, SNode to) {
    for (SNode arg : ListSequence.fromList(SLinkOperations.getChildren(from, LINKS.typeParameters$5Tel))) {
      ListSequence.fromList(SLinkOperations.getChildren(to, LINKS.typeParameters$5Tel)).addElement(SNodeOperations.copyNode(arg));
    }
  }

  private static final class PROPS {
    /*package*/ static final SProperty targetResolved$e43v = MetaAdapterFactory.getProperty(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x4b3463a3c4af5feeL, 0xd5ab3252dcc77f8L, "targetResolved");
    /*package*/ static final SProperty methodName$oxdi = MetaAdapterFactory.getProperty(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x4b3463a3c4af5feeL, 0x4b3463a3c4af6583L, "methodName");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink target$Woec = MetaAdapterFactory.getContainmentLink(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x30ab9441ca549e7bL, 0xd5ab3252daa1c07L, "target");
    /*package*/ static final SReferenceLink method$8Sfb = MetaAdapterFactory.getReferenceLink(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x30ab9441ca549e7bL, 0x34d254ec4f413c1L, "method");
    /*package*/ static final SReferenceLink classifier$cxMr = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier");
    /*package*/ static final SContainmentLink type$z9_f = MetaAdapterFactory.getContainmentLink(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x6dd7c320c6fc97cdL, 0x6dd7c320c6fca4b8L, "type");
    /*package*/ static final SContainmentLink typeParameters$5Tel = MetaAdapterFactory.getContainmentLink(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x30ab9441ca549e7bL, 0x2120da84c4733f2eL, "typeParameters");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept Classifier$Ix = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier");
    /*package*/ static final SConcept UnknownMethodReference$lD = MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x4b3463a3c4af5feeL, "jetbrains.mps.baseLanguage.methodReferences.structure.UnknownMethodReference");
    /*package*/ static final SConcept ConstructorDeclaration$yG = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b204L, "jetbrains.mps.baseLanguage.structure.ConstructorDeclaration");
    /*package*/ static final SConcept MethodReferenceTypeTargetExpression$a3 = MetaAdapterFactory.getConcept(0xacfc188dd5d64598L, 0xb3706f4a983f05b2L, 0x6dd7c320c6fc97cdL, "jetbrains.mps.baseLanguage.methodReferences.structure.MethodReferenceTypeTargetExpression");
    /*package*/ static final SConcept Expression$mB = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression");
  }
}
