package jetbrains.mps.baseLanguage.unitTest.execution.tool;

/*Generated by MPS */

import org.jetbrains.annotations.Nullable;
import jetbrains.mps.baseLanguage.unitTest.execution.TestCaseNodeKey;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.baseLanguage.unitTest.execution.TestNodeKey;
import jetbrains.mps.baseLanguage.unitTest.execution.TextTestEvent;
import jetbrains.mps.baseLanguage.unitTest.execution.TestMethodNodeKey;
import jetbrains.mps.baseLanguage.unitTest.execution.TestType;
import java.util.Objects;

public class TestCaseMessageContainer extends MessageContainerBase<MethodMessageContainer> implements TestMessage {
  @Nullable
  private final TestCaseNodeKey myTestCaseNode;

  public TestCaseMessageContainer(@Nullable TestCaseNodeKey testCase, MessageFilter filter) {
    super(filter);
    myTestCaseNode = testCase;
  }

  @Override
  public boolean matches(@NotNull MessageFilter filter) {
    TestNodeKey filterNode = filter.getNode();
    if (filterNode == null || filterNode.getTestCaseNodeKey().equals(myTestCaseNode)) {
      return true;
    }
    return false;
  }

  @NotNull
  @Override
  public MethodMessageContainer createChildMessage(@NotNull TextTestEvent textEvent) {
    TestMethodNodeKey methodNodeKey = null;
    TestNodeKey currentTestNode = textEvent.getCurrentTestNode();
    if (currentTestNode != null && currentTestNode.getType() == TestType.METHOD) {
      methodNodeKey = (TestMethodNodeKey) currentTestNode;
    }
    MethodMessageContainer methodMsg = new MethodMessageContainer(methodNodeKey, getFilter());
    methodMsg.addMessage(textEvent);
    return methodMsg;
  }

  @Override
  public boolean accepts(@NotNull TextTestEvent event) {
    TestCaseNodeKey testCase = null;
    TestNodeKey currentTestNode = event.getCurrentTestNode();
    if (currentTestNode != null) {
      testCase = currentTestNode.getTestCaseNodeKey();
    }
    return Objects.equals(myTestCaseNode, testCase);
  }
}
