package jetbrains.mps.build.mps.runner.test.test4;

/*Generated by MPS */

import jetbrains.mps.tool.environment.Environment;
import jetbrains.mps.project.Project;
import java.io.File;
import java.util.List;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.smodel.ModelAccessHelper;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.ide.ThreadUtils;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class MainClass {
  private static final String MPS_PROJECT = "project";
  private static final String MPS_MODEL = "jetbrains.mps.build.mps.runner.test.solution.model";
  private static final String MODEL_CLASSNAME = "TestClass";

  public static void mpsMain(Environment env) {
    System.out.println("Test project opening...");
    final Project project = env.openProject(new File(MPS_PROJECT));
    assert project != null;

    System.out.println("Test model access...");
    final List<SModel> projectModels = new ModelAccessHelper(project.getModelAccess()).runReadAction(() -> ListSequence.fromListWithValues(new ArrayList<SModel>(), project.getProjectModels()));
    assert ListSequence.fromList(projectModels).count() == 1 : "Project models count: " + ListSequence.fromList(projectModels).count();
    final SModel model = new ModelAccessHelper(project.getModelAccess()).runReadAction(() -> ListSequence.fromList(projectModels).findFirst((it) -> MPS_MODEL.equals(it.getName().getValue())));
    assert model != null;
    // check model content
    final SNode node = new ModelAccessHelper(project.getModelAccess()).runReadAction(() -> ListSequence.fromList(SModelOperations.roots(model, CONCEPTS.ClassConcept$bK)).findFirst((it) -> MODEL_CLASSNAME.equals(SPropertyOperations.getString(it, PROPS.name$MnvL))));
    assert node != null;

    // test write access
    final Wrappers._T<SNode> result = new Wrappers._T<SNode>(null);
    ThreadUtils.runInUIThreadAndWait(() -> project.getModelAccess().executeCommand(() -> {
      ListSequence.fromList(SLinkOperations.getChildren(node, LINKS.member$L_2d)).addElement(_quotation_createNode_ea23db_a0a0a0a0a0a0p0e());
      result.value = Sequence.fromIterable(SNodeOperations.ofConcept(SLinkOperations.getChildren(node, LINKS.member$L_2d), CONCEPTS.FieldDeclaration$ie)).findFirst((it) -> "n".equals(SPropertyOperations.getString(it, PROPS.name$MnvL)));
    }));
    assert result.value != null;

    // create resulting file to signal everything is OK
    jetbrains.mps.build.mps.runner.test.test1.MainClass.mpsMain();
  }
  private static SNode _quotation_createNode_ea23db_a0a0a0a0a0a0p0e() {
    SNode quotedNode_1 = null;
    SNode quotedNode_2 = null;
    SNode quotedNode_3 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8c108ca68L, "FieldDeclaration"));
    quotedNode_1 = nb.getResult();
    nb.setProperty(MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), "n");
    SNodeBuilder nb1 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x10af9586f0cL, "PrivateVisibility"));
    quotedNode_2 = nb1.getResult();
    quotedNode_1.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"), quotedNode_2);
    SNodeBuilder nb2 = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf940d22479L, "IntegerType"));
    quotedNode_3 = nb2.getResult();
    quotedNode_1.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type"), quotedNode_3);
    return quotedNode_1;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept ClassConcept$bK = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca66L, "jetbrains.mps.baseLanguage.structure.ClassConcept");
    /*package*/ static final SConcept FieldDeclaration$ie = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca68L, "jetbrains.mps.baseLanguage.structure.FieldDeclaration");
  }

  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 SContainmentLink member$L_2d = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, 0x4a9a46de59132803L, "member");
  }
}
