package net.wizardsoflua.tests;

import org.junit.Test;
import org.junit.runner.RunWith;

import net.minecraft.util.math.BlockPos;
import net.wizardsoflua.testenv.MinecraftJUnitRunner;
import net.wizardsoflua.testenv.WolTestBase;
import net.wizardsoflua.testenv.event.ServerLog4jEvent;

@RunWith(MinecraftJUnitRunner.class)
public class LivingDeathEventTest extends WolTestBase {

  // /test net.wizardsoflua.tests.LivingDeathEventTest test_cause_is_outOfWorld
  @Test
  public void test_cause_is_outOfWorld() throws Exception {
    // Given:
    BlockPos pos = mc().getWorldSpawnPoint();
    mc().executeCommand("/summon minecraft:pig %s %s %s {CustomName:testpig,NoAI:1}", pos.getX(),
        pos.getY(), pos.getZ());
    mc().clearEvents();

    // When:
    mc().executeCommand(
        "/lua q=Events.connect('LivingDeathEvent'); e=q:next(); print(e.cause)");
    mc().executeCommand("/kill @e[type=pig,name=testpig]");

    // Then:
    ServerLog4jEvent act1 = mc().waitFor(ServerLog4jEvent.class);
    assertThat(act1.getMessage()).isEqualTo("Killed testpig");
    ServerLog4jEvent act2 = mc().waitFor(ServerLog4jEvent.class);
    assertThat(act2.getMessage()).isEqualTo("outOfWorld");
  }

  // /test net.wizardsoflua.tests.LivingDeathEventTest test_entity_is_testpig
  @Test
  public void test_entity_is_testpig() throws Exception {
    // Given:
    BlockPos pos = mc().getWorldSpawnPoint();
    mc().executeCommand("/summon minecraft:pig %s %s %s {CustomName:testpig,NoAI:1}", pos.getX(),
        pos.getY(), pos.getZ());
    mc().clearEvents();

    // When:
    mc().executeCommand(
        "/lua q=Events.connect('LivingDeathEvent'); e=q:next(); print(e.entity.name)");
    mc().executeCommand("/kill @e[type=pig,name=testpig]");

    // Then:
    ServerLog4jEvent act1 = mc().waitFor(ServerLog4jEvent.class);
    assertThat(act1.getMessage()).isEqualTo("Killed testpig");
    ServerLog4jEvent act2 = mc().waitFor(ServerLog4jEvent.class);
    assertThat(act2.getMessage()).isEqualTo("testpig");
  }

  // /test net.wizardsoflua.tests.LivingDeathEventTest test_entity_is_player
  @Test
  public void test_entity_is_player() {
    // Given:

    // When:
    try {
      mc().executeCommand(
          "/lua q=Events.connect('LivingDeathEvent'); e=q:next(); print(e.entity.name)");
      mc().executeCommand("/kill @a[name=testplayer]");

      // Then:
      ServerLog4jEvent act1 = mc().waitFor(ServerLog4jEvent.class);
      assertThat(act1.getMessage()).startsWith("testplayer fell out of the world");
      ServerLog4jEvent act2 = mc().waitFor(ServerLog4jEvent.class);
      assertThat(act2.getMessage()).startsWith("Killed testplayer");
      ServerLog4jEvent act3 = mc().waitFor(ServerLog4jEvent.class);
      assertThat(act3.getMessage()).isEqualTo("testplayer");

    } finally {
      mc().player().respawn();
    }
  }

}

@@ -771,33 +771,24 @@
 cuteCommand(
-%0A        
 %22/lua q=Even
@@ -2136,16 +2136,65 @@
 / Given:
+%0A    String playerName = mc().player().getName();
 %0A%0A    //
@@ -2372,20 +2372,24 @@
 ame=
-test
+%25s%5D%22, 
 player
-%5D%22
+Name
 );%0A%0A
@@ -2518,27 +2518,30 @@
 rtsWith(
-%22test
 player
+Name + %22
  fell ou
@@ -2681,27 +2681,30 @@
 %22Killed 
-test
+%22 + 
 player
-%22
+Name
 );%0A     
@@ -2816,20 +2816,18 @@
 lTo(
-%22test
 player
-%22
+Name
 );%0A%0A

Fix failing LivingDeathEventTest