package thb.jeanluc.adventure.game;

import org.junit.jupiter.api.Test;
import thb.jeanluc.adventure.io.TestIO;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.item.PlainItem;
import thb.jeanluc.adventure.model.item.SwitchableItem;

import static org.assertj.core.api.Assertions.assertThat;

class LightTest {
    private GameContext ctx(Player p) {
        return new GameContext(null, p, new TestIO());
    }

    private SwitchableItem lamp(boolean on) {
        return SwitchableItem.builder().id("lamp").name("Lamp").description("d")
                .state(on).onText("on").offText("off").light(true).build();
    }

    @Test
    void litRoomIsAlwaysLit() {
        Room bright = new Room("r", "R", "d"); // dark defaults to false
        assertThat(Light.isLit(ctx(new Player(bright, 0)), bright)).isTrue();
    }

    @Test
    void darkRoomNeedsActiveLight() {
        Room dark = new Room("r", "R", "d");
        dark.setDark(true);
        Player p = new Player(dark, 0);
        GameContext ctx = ctx(p);
        assertThat(Light.isLit(ctx, dark)).isFalse();

        p.addItem(lamp(false));
        assertThat(Light.isLit(ctx, dark)).isFalse();   // lamp off

        p.removeItem("lamp");
        p.addItem(lamp(true));
        assertThat(Light.isLit(ctx, dark)).isTrue();    // lamp on, carried
        assertThat(Light.carryingLight(ctx)).isTrue();
    }

    @Test
    void litLampLyingInRoomLightsIt() {
        Room dark = new Room("r", "R", "d");
        dark.setDark(true);
        dark.addItem(lamp(true));
        Player p = new Player(dark, 0);
        assertThat(Light.isLit(ctx(p), dark)).isTrue();
        assertThat(Light.carryingLight(ctx(p))).isFalse(); // not carried
    }

    @Test
    void nonLightItemDoesNotLight() {
        Room dark = new Room("r", "R", "d");
        dark.setDark(true);
        Player p = new Player(dark, 0);
        p.addItem(PlainItem.builder().id("rock").name("Rock").description("d").build());
        assertThat(Light.isLit(ctx(p), dark)).isFalse();
    }
}
