package jetbrains.mps.baselanguage.unitTest.execution.launcher;

/*Generated by MPS */

import org.jetbrains.annotations.Nullable;
import jetbrains.mps.tool.common.ScriptData;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import org.jdom.Element;
import jetbrains.mps.internal.collections.runtime.ListSequence;

/**
 * Set of test executor arguments to get serialized for inter-process communication
 */
public class ExecutorScript {
  @Nullable
  private ScriptData myEnvironmentStartupData;
  protected final List<TestRecord> myTests = new ArrayList<TestRecord>();

  public Collection<TestRecord> getTests() {
    return myTests;
  }

  public ScriptData addStartupArguments() {
    myEnvironmentStartupData = new ScriptData();
    return myEnvironmentStartupData;
  }

  @Nullable
  public ScriptData getStartupArguments() {
    return myEnvironmentStartupData;
  }

  public void write(Element root) {
    for (TestRecord r : ListSequence.fromList(myTests)) {
      Element module = new Element("module");
      module.setAttribute("ptr", r.myTestModule);
      for (int i = 0; i < r.myTestQualifiedName.size(); i++) {
        Element elem = new Element("test");
        elem.setAttribute("fqn", r.myTestQualifiedName.get(i));
        elem.setAttribute("node", r.myTestNode.get(i));
        elem.setAttribute("isTestCase", r.isTestCase.get(i));
        module.addContent(elem);
      }
      root.addContent(module);
    }
    if (myEnvironmentStartupData != null) {
      myEnvironmentStartupData.write(root);
    }
  }

  public void read(Element root) {
    for (Element me : ListSequence.fromList(root.getChildren("module"))) {
      TestRecord tr = new TestRecord(me.getAttributeValue("ptr"));
      myTests.add(tr);
      for (Element te : ListSequence.fromList(me.getChildren("test"))) {
        tr.add(te.getAttributeValue("fqn"), te.getAttributeValue("node"), te.getAttributeValue("isTestCase"));
      }
    }
    myEnvironmentStartupData = new ScriptData();
    myEnvironmentStartupData.read(root);
  }

  public static final class TestRecord {
    public final String myTestModule;
    public final List<String> myTestNode = new ArrayList<String>();
    public final List<String> myTestQualifiedName = new ArrayList<String>();
    public final List<String> isTestCase = new ArrayList<String>();

    public TestRecord(String testModule) {
      myTestModule = testModule;
    }

    public void add(String fqName, String testNodePointer, String isTestCase0) {
      myTestQualifiedName.add(fqName);
      myTestNode.add(testNodePointer);
      isTestCase.add(isTestCase0 + "");
    }
  }

}
