package thb.jeanluc.adventure.game;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/** Runtime quest progress: current stage per active quest, plus completed ids. */
public class QuestLog {

    private final Map<String, Integer> stageIndex = new LinkedHashMap<>();
    private final Set<String> completed = new LinkedHashSet<>();

    public void start(String id) {
        if (!completed.contains(id)) {
            stageIndex.putIfAbsent(id, 0);
        }
    }

    public boolean isActive(String id) {
        return stageIndex.containsKey(id);
    }

    public boolean isCompleted(String id) {
        return completed.contains(id);
    }

    public int stageIndex(String id) {
        return stageIndex.getOrDefault(id, 0);
    }

    public void advance(String id) {
        stageIndex.merge(id, 1, Integer::sum);
    }

    public void complete(String id) {
        stageIndex.remove(id);
        completed.add(id);
    }

    public Set<String> active() {
        return Collections.unmodifiableSet(stageIndex.keySet());
    }

    public Set<String> completed() {
        return Collections.unmodifiableSet(completed);
    }

    /** Replaces all progress with the given stage map and completed set (load). */
    public void restore(Map<String, Integer> stages, Collection<String> completedIds) {
        stageIndex.clear();
        stageIndex.putAll(stages);
        completed.clear();
        completed.addAll(completedIds);
    }
}
