package thb.jeanluc.adventure.loader.dto;

import thb.jeanluc.adventure.loader.WorldLoadException;
import thb.jeanluc.adventure.model.Effect;

import java.util.ArrayList;
import java.util.List;

/** YAML effect: exactly one of the fields is set. */
public record EffectDto(String setFlag, String clearFlag, String giveItem, String removeItem,
                        String say, String startQuest) {

    /** Backward-compatible constructor without startQuest. */
    public EffectDto(String setFlag, String clearFlag, String giveItem, String removeItem, String say) {
        this(setFlag, clearFlag, giveItem, removeItem, say, null);
    }

    public Effect toModel() {
        if (setFlag != null) {
            return new Effect(Effect.Type.SET_FLAG, setFlag);
        }
        if (clearFlag != null) {
            return new Effect(Effect.Type.CLEAR_FLAG, clearFlag);
        }
        if (giveItem != null) {
            return new Effect(Effect.Type.GIVE_ITEM, giveItem);
        }
        if (removeItem != null) {
            return new Effect(Effect.Type.REMOVE_ITEM, removeItem);
        }
        if (say != null) {
            return new Effect(Effect.Type.SAY, say);
        }
        if (startQuest != null) {
            return new Effect(Effect.Type.START_QUEST, startQuest);
        }
        throw new WorldLoadException(
                "Effect must set one of setFlag/clearFlag/giveItem/removeItem/say/startQuest");
    }

    public static List<Effect> toModelList(List<EffectDto> dtos) {
        List<Effect> out = new ArrayList<>();
        if (dtos != null) {
            for (EffectDto d : dtos) {
                out.add(d.toModel());
            }
        }
        return out;
    }
}
