package thb.jeanluc.adventure.command.impl;

import org.junit.jupiter.api.Test;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.io.TestIO;
import thb.jeanluc.adventure.model.Condition;
import thb.jeanluc.adventure.model.Direction;
import thb.jeanluc.adventure.model.ExitLock;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.World;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

class GoCommandPathTest {

    /** kitchen --N--> hallway --N--> library (all lit). Optional lock on kitchen->hallway. */
    private GameContext world(boolean lockHallway, String libraryName) {
        Room kitchen = new Room("kitchen", "Kitchen", "d");
        Room hallway = new Room("hallway", "Hallway", "d");
        Room library = new Room("library", libraryName, "d");
        kitchen.addExit(Direction.NORTH, hallway);
        hallway.addExit(Direction.SOUTH, kitchen);
        hallway.addExit(Direction.NORTH, library);
        library.addExit(Direction.SOUTH, hallway);
        if (lockHallway) {
            kitchen.addExitLock(Direction.NORTH,
                    new ExitLock(List.of(new Condition(Condition.Type.FLAG, "open")), "It's locked."));
        }
        Map<String, Room> rooms = new LinkedHashMap<>();
        rooms.put("kitchen", kitchen);
        rooms.put("hallway", hallway);
        rooms.put("library", library);
        World w = new World(rooms, Map.of(), Map.of(), "t", "w");
        Player p = new Player(kitchen, 0);
        p.getVisitedRoomIds().addAll(List.of("kitchen", "hallway", "library"));
        return new GameContext(w, p, new TestIO());
    }

    private String out(GameContext ctx) {
        return ((TestIO) ctx.getIo()).allOutput();
    }

    @Test
    void goToWalksToVisitedRoomById() {
        GameContext ctx = world(false, "Library");
        new GoCommand().execute(ctx, List.of("library"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("library");
        assertThat(out(ctx)).contains("make your way");
        assertThat(out(ctx)).contains("through the Hallway");
    }

    @Test
    void resolvesByRoomName() {
        // id "library" but type the display name (lower-cased) — still resolves
        GameContext ctx = world(false, "Grand Library");
        new GoCommand().execute(ctx, List.of("grand", "library"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("library");
    }

    @Test
    void unknownPlaceReportsSo() {
        GameContext ctx = world(false, "Library");
        new GoCommand().execute(ctx, List.of("dungeon"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("kitchen");
        assertThat(out(ctx)).contains("don't know any direction or place called 'dungeon'");
    }

    @Test
    void alreadyThere() {
        GameContext ctx = world(false, "Library");
        new GoCommand().execute(ctx, List.of("kitchen"));
        assertThat(out(ctx)).contains("already in the Kitchen");
    }

    @Test
    void noPassablePathReportsSo() {
        GameContext ctx = world(true, "Library"); // kitchen->hallway locked, flag unset
        new GoCommand().execute(ctx, List.of("library"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("kitchen");
        assertThat(out(ctx)).contains("can't find a way there");
    }

    @Test
    void directionalMoveStillWorks() {
        GameContext ctx = world(false, "Library");
        new GoCommand().execute(ctx, List.of("north"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("hallway");
    }

    @Test
    void adjacentWalkUsesShortSummary() {
        GameContext ctx = world(false, "Library");
        new GoCommand().execute(ctx, List.of("hallway"));
        assertThat(ctx.getPlayer().getCurrentRoom().getId()).isEqualTo("hallway");
        assertThat(out(ctx)).contains("make your way from the Kitchen to the Hallway");
        assertThat(out(ctx)).doesNotContain("through");
    }
}
