package thb.jeanluc.adventure.game;

import thb.jeanluc.adventure.io.GameIO;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.World;

/**
 * Bundle of everything a command needs: the active {@link GameSession}
 * (world, player, flags, quests, turn) and the IO channel. Getters delegate
 * to the session so existing command code keeps working unchanged.
 */
public class GameContext {

    private final GameSession session;
    private final GameIO io;

    /** Silent autosave hook, wired by the app; no-op by default (tests). */
    private Runnable saveCallback = () -> { };

    public GameContext(GameSession session, GameIO io) {
        this.session = session;
        this.io = io;
    }

    /** Convenience for tests/legacy callers: wraps a fresh single-use session. */
    public GameContext(World world, Player player, GameIO io) {
        this(new GameSession(world, player, "session"), io);
    }

    public GameSession getSession() {
        return session;
    }

    public World getWorld() {
        return session.getWorld();
    }

    public Player getPlayer() {
        return session.getPlayer();
    }

    public GameState getState() {
        return session.getState();
    }

    public QuestLog getQuestLog() {
        return session.getQuestLog();
    }

    public GameIO getIo() {
        return io;
    }

    /** Sets the silent autosave hook (called by interval/quest autosave). */
    public void setSaveCallback(Runnable saveCallback) {
        this.saveCallback = saveCallback;
    }

    /** Runs the silent autosave hook. */
    public void save() {
        saveCallback.run();
    }
}
