package jetbrains.mps.lang.smodel.generator.baseLanguage.template.main;

/*Generated by MPS */

import jetbrains.mps.generator.runtime.Generated;
import jetbrains.mps.generator.runtime.TemplateDeclarationBase;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.smodel.SNodePointer;
import jetbrains.mps.generator.runtime.FragmentResult;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.generator.runtime.TemplateContext;
import jetbrains.mps.generator.runtime.GenerationException;
import jetbrains.mps.generator.runtime.TemplateExecutionEnvironment;
import java.util.Collection;
import org.jetbrains.mps.openapi.model.SNode;
import java.util.Collections;
import jetbrains.mps.generator.template.IfMacroContext;
import org.jetbrains.mps.openapi.model.SNodeAccessUtil;
import jetbrains.mps.generator.template.PropertyMacroContext;
import jetbrains.mps.generator.runtime.TemplateUtil;
import java.util.List;
import java.util.ArrayList;
import jetbrains.mps.generator.template.SourceSubstituteMacroNodesContext;
import jetbrains.mps.generator.impl.reference.RefResolver;
import jetbrains.mps.generator.template.SourceSubstituteMacroNodeContext;
import jetbrains.mps.generator.runtime.ApplySink;
import jetbrains.mps.generator.runtime.MetaObjectContainer;
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;
import org.jetbrains.mps.openapi.language.SContainmentLink;

@Generated
public class Template_reduce_ConceptSwitch_SubconceptCases extends TemplateDeclarationBase {

  public Template_reduce_ConceptSwitch_SubconceptCases() {
    super(new MO());
  }

  public SNodeReference getTemplateNode() {
    return new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "75056552358298205");
  }


  protected FragmentResult applyPart0(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    Collection<SNode> tlist1 = Collections.emptyList();
    if (QueriesGenerated.ifMacro_Condition_104_0(new IfMacroContext(context, ifMacroRef))) {
      final SNode tnode2 = environment.createOutputNode(myConcepts[0]);
      {
        TemplateContext context1 = context.subContext();
        {
          final SNode tnode3 = environment.createOutputNode(myConcepts[1]);
          {
            environment.nodeCopied(context1, tnode3, "tpl/r:00000000-0000-4000-0000-011c89590303/3328175781349271983");
            SNodeAccessUtil.setPropertyValue(tnode3, myProperties[0], QueriesGenerated.propertyMacro_GetValue_104_0(new PropertyMacroContext(context1, "noneMatched", propertyMacro)));
            TemplateContext context2 = context1.subContext();
            {
              final SNode tnode4 = environment.createOutputNode(myConcepts[2]);
              environment.aggregate(tnode3, myAggregationLinks[0], tnode4);
            }
            {
              final SNode tnode5 = environment.createOutputNode(myConcepts[3]);
              tnode5.setProperty(myProperties[1], "true");
              environment.aggregate(tnode3, myAggregationLinks[1], tnode5);
            }
          }
          environment.aggregate(tnode2, myAggregationLinks[2], tnode3);
        }
      }
      tlist1 = TemplateUtil.singletonList(tnode2);
    }
    FragmentResult rv = listFragment(3, tlist1);
    return rv;
  }
  protected FragmentResult applyPart1(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    final List<SNode> tlist1 = new ArrayList<SNode>();
    final Iterable<SNode> loopList1 = QueriesGenerated.sourceNodesQuery_104_1(new SourceSubstituteMacroNodesContext(context, loopMacroRef));
    for (SNode itnode1 : loopList1) {
      if (itnode1 == null) {
        continue;
      }
      TemplateContext context1 = context.subContext(itnode1);
      final SNode tnode2 = environment.createOutputNode(myConcepts[4]);
      {
        TemplateContext context2 = context1.subContext();
        {
          final SNode tnode3 = environment.createOutputNode(myConcepts[5]);
          {
            TemplateContext context3 = context2.subContext();
            {
              final SNode tnode4 = environment.createOutputNode(myConcepts[6]);
              {
                TemplateContext context4 = context3.subContext();
                {
                  final SNode tnode5 = environment.createOutputNode(myConcepts[7]);
                  {
                    TemplateContext context5 = context4.subContext();
                    {
                      final SNode tnode6 = environment.createOutputNode(myConcepts[3]);
                      tnode6.setProperty(myProperties[1], "false");
                      environment.aggregate(tnode5, myAggregationLinks[4], tnode6);
                    }
                    {
                      final SNode tnode7 = environment.createOutputNode(myConcepts[8]);
                      environment.resolveInTemplateLater(tnode7, myAssociationLinks[0], templateNode, "tpl/r:00000000-0000-4000-0000-011c89590303/3328175781349271983", "noneMatched", context5);
                      environment.aggregate(tnode5, myAggregationLinks[5], tnode7);
                    }
                  }
                  environment.aggregate(tnode4, myAggregationLinks[6], tnode5);
                }
              }
              environment.aggregate(tnode3, myAggregationLinks[3], tnode4);
            }
            {
              Collection<SNode> tlist8 = null;
              final Iterable<SNode> copyListInput8 = QueriesGenerated.sourceNodesQuery_104_0(new SourceSubstituteMacroNodesContext(context3, copySrcListMacro));
              tlist8 = environment.copyNodes(copyListInput8, copySrcListMacro, "tpl/r:00000000-0000-4000-0000-011c89590303/75056552358317703", context3);
              environment.aggregate(tnode3, myAggregationLinks[3], tlist8);
            }
          }
          environment.aggregate(tnode2, myAggregationLinks[7], tnode3);
        }
        {
          final SNode tnode9 = environment.createOutputNode(myConcepts[9]);
          {
            TemplateContext context6 = context2.subContext();
            {
              final SNode tnode10 = environment.createOutputNode(myConcepts[8]);
              environment.resolveInTemplateLater(tnode10, myAssociationLinks[0], templateNode1, "tpl/r:00000000-0000-4000-0000-011c89590303/3328175781349271983", "noneMatched", context6);
              environment.aggregate(tnode9, myAggregationLinks[8], tnode10);
            }
            {
              final SNode tnode11 = environment.createOutputNode(myConcepts[10]);
              {
                environment.associate(tnode11, myAssociationLinks[1], "r:c3548bac-30eb-4a2a-937c-0111d5697309(jetbrains.mps.lang.smodel.generator.smodelAdapter)", "1461238478128512590");
                environment.associate(tnode11, myAssociationLinks[2], "r:c3548bac-30eb-4a2a-937c-0111d5697309(jetbrains.mps.lang.smodel.generator.smodelAdapter)", "6599163591527270726");
                TemplateContext context7 = context6.subContext();
                {
                  final SNode tnode12 = environment.createOutputNode(myConcepts[8]);
                  environment.resolve(new RefResolver(tnode12, myAssociationLinks[0], context7, new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "4217968694108143881"), "cncpt") {
                    @Override
                    public Object resolve() {
                      return QueriesGenerated.referenceMacro_GetReferent_104_0(createQueryContext());
                    }
                  });
                  environment.aggregate(tnode11, myAggregationLinks[9], tnode12);
                }
                {
                  Collection<SNode> tlist13 = null;
                  SNode callInputNode13 = QueriesGenerated.sourceNodeQuery_104_0(new SourceSubstituteMacroNodeContext(context7, callMacro));
                  TemplateContext context8 = context7;
                  context8 = context8.subContext(null, callInputNode13);
                  if (callInputNode13 != null) {
                    tlist13 = environment.callSite(new Template_reduce_ConceptDeclaration2SAbstractConcept(), new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "633481135935145332")).apply(context8);
                  } else {
                    tlist13 = Collections.emptyList();
                  }
                  environment.aggregate(tnode11, myAggregationLinks[9], tlist13);
                }
              }
              environment.aggregate(tnode9, myAggregationLinks[10], tnode11);
            }
          }
          environment.aggregate(tnode2, myAggregationLinks[11], tnode9);
        }
      }
      tlist1.add(tnode2);
    }
    FragmentResult rv = listFragment(3, tlist1);
    return rv;
  }
  protected FragmentResult applyPart2(@NotNull final TemplateContext context) throws GenerationException {
    final TemplateExecutionEnvironment environment = context.getEnvironment();
    Collection<SNode> tlist1 = Collections.emptyList();
    if (QueriesGenerated.ifMacro_Condition_104_1(new IfMacroContext(context, ifMacroRef2))) {
      Collection<SNode> tlist2 = Collections.emptyList();
      if (QueriesGenerated.ifMacro_Condition_104_2(new IfMacroContext(context, ifMacroRef1))) {
        final SNode tnode3 = environment.createOutputNode(myConcepts[4]);
        {
          TemplateContext context1 = context.subContext();
          {
            final SNode tnode4 = environment.createOutputNode(myConcepts[5]);
            {
              TemplateContext context2 = context1.subContext();
              {
                Collection<SNode> tlist5 = null;
                final Iterable<SNode> copyListInput5 = QueriesGenerated.sourceNodesQuery_104_2(new SourceSubstituteMacroNodesContext(context2, copySrcListMacro1));
                tlist5 = environment.copyNodes(copyListInput5, copySrcListMacro1, "tpl/r:00000000-0000-4000-0000-011c89590303/75056552358330360", context2);
                environment.aggregate(tnode4, myAggregationLinks[3], tlist5);
              }
            }
            environment.aggregate(tnode3, myAggregationLinks[7], tnode4);
          }
          {
            final SNode tnode6 = environment.createOutputNode(myConcepts[8]);
            environment.resolveInTemplateLater(tnode6, myAssociationLinks[0], templateNode2, "tpl/r:00000000-0000-4000-0000-011c89590303/3328175781349271983", "noneMatched", context1);
            environment.aggregate(tnode3, myAggregationLinks[11], tnode6);
          }
        }
        tlist2 = TemplateUtil.singletonList(tnode3);
      } else {
        Collection<SNode> tlist7 = null;
        final Iterable<SNode> copyListInput7 = QueriesGenerated.sourceNodesQuery_104_3(new SourceSubstituteMacroNodesContext(context, copySrcListMacro2));
        tlist7 = environment.copyNodes(copyListInput7, copySrcListMacro2, "tpl/r:00000000-0000-4000-0000-011c89590303/3328175781349667302", context);
        tlist2 = tlist7;
      }
      tlist1 = tlist2;
    }
    FragmentResult rv = listFragment(3, tlist1);
    return rv;
  }
  @Override
  public void apply(TemplateContext context, ApplySink sink) throws GenerationException {
    applyPart0(context).reportTo(sink);
    applyPart1(context).reportTo(sink);
    applyPart2(context).reportTo(sink);
  }

  /*package*/ static final class MO implements MetaObjectContainer {
    @Override
    public SConcept[] concepts() {
      SConcept[] rv = new SConcept[11];
      rv[0] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc67c7f0L, "LocalVariableDeclarationStatement");
      rv[1] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc67c7efL, "LocalVariableDeclaration");
      rv[2] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf940d6513eL, "BooleanType");
      rv[3] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b201L, "BooleanConstant");
      rv[4] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b217L, "IfStatement");
      rv[5] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b200L, "StatementList");
      rv[6] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b213L, "ExpressionStatement");
      rv[7] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8c77f1e96L, "AssignmentExpression");
      rv[8] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8c77f1e98L, "VariableReference");
      rv[9] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xfb7c3070eeL, "AndExpression");
      rv[10] = MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xfbbebabf09L, "StaticMethodCall");
      return rv;
    }

    @Override
    public SProperty[] properties() {
      SProperty[] rv = new SProperty[2];
      rv[0] = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
      rv[1] = MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, 0xf8cc56b202L, "value");
      return rv;
    }

    @Override
    public SReferenceLink[] associations() {
      SReferenceLink[] rv = new SReferenceLink[3];
      rv[0] = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, 0xf8cc6bf960L, "variableDeclaration");
      rv[1] = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301adL, "baseMethodDeclaration");
      rv[2] = MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbbebabf09L, 0x10a7588b546L, "classConcept");
      return rv;
    }

    @Override
    public SContainmentLink[] aggregations() {
      SContainmentLink[] rv = new SContainmentLink[12];
      rv[0] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type");
      rv[1] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer");
      rv[2] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration");
      rv[3] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, 0xf8cc6bf961L, "statement");
      rv[4] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11b0d00332cL, 0xf8c77f1e99L, "rValue");
      rv[5] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11b0d00332cL, 0xf8c77f1e97L, "lValue");
      rv[6] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b213L, 0xf8cc56b214L, "expression");
      rv[7] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0xf8cc56b219L, "ifTrue");
      rv[8] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11cL, "leftExpression");
      rv[9] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301aeL, "actualArgument");
      rv[10] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11bL, "rightExpression");
      rv[11] = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b217L, 0xf8cc56b218L, "condition");
      return rv;
    }
  }
  private static final SNodePointer propertyMacro = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349275249");
  private static final SNodePointer ifMacroRef = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349628289");
  private static final SNodePointer loopMacroRef = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "75056552358317728");
  private static final SNodePointer templateNode = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349282728");
  private static final SNodePointer copySrcListMacro = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "75056552358317719");
  private static final SNodePointer templateNode1 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349305535");
  private static final SNodePointer callMacro = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "633481135935145332");
  private static final SNodePointer copySrcListMacro1 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "75056552358330361");
  private static final SNodePointer templateNode2 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349291857");
  private static final SNodePointer ifMacroRef1 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349660452");
  private static final SNodePointer copySrcListMacro2 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349667303");
  private static final SNodePointer ifMacroRef2 = new SNodePointer("r:00000000-0000-4000-0000-011c89590303(jetbrains.mps.lang.smodel.generator.baseLanguage.template.main@generator)", "3328175781349293231");
}
