package thb.jeanluc.adventure.model.item;

import lombok.Getter;
import lombok.experimental.SuperBuilder;
import thb.jeanluc.adventure.game.Effects;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.model.Effect;

import java.util.List;

/**
 * Item with two boolean states (on / off). Each {@code use} toggles the
 * state and prints the corresponding transition message.
 */
@Getter
@SuperBuilder
public class SwitchableItem extends Item {

    /** Text written when the item transitions to on. */
    private final String onText;

    /** Text written when the item transitions to off. */
    private final String offText;

    /**
     * Current on/off state. Seeded by the builder from the YAML
     * {@code initialState} field via the item factory.
     */
    private boolean state;

    /** Effects applied when this item transitions to on; null = none. */
    private final List<Effect> effects;

    /**
     * Toggles the state and writes the corresponding transition text.
     *
     * @param ctx active game context
     */
    @Override
    public void use(GameContext ctx) {
        state = !state;
        ctx.getIo().write(state ? onText : offText);
        if (state) {
            Effects.applyAll(effects, ctx);
        }
    }

    /**
     * Convenience predicate for the current state.
     *
     * @return {@code true} if the item is currently on
     */
    public boolean isOn() {
        return state;
    }

    /**
     * Directly sets the on/off state without running effects or printing.
     * Used only when restoring a saved game.
     *
     * @param state the state to restore
     */
    public void setState(boolean state) {
        this.state = state;
    }
}
