package eu.jonahbauer.wizard.core.model.deck;

import eu.jonahbauer.wizard.common.model.Card;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.*;

public final class Deck {
    private final ArrayList<Card> cards = new ArrayList<>();
    private int next = 0;

    public Deck(Collection<Card> cards) {
        this.cards.addAll(cards);
    }

    public void shuffle(Random random) {
        Collections.shuffle(cards, random);
        next = 0;
    }

    @Unmodifiable
    public List<Card> draw(int n) {
        if (remaining() < n) {
            throw new NoSuchElementException("Not enough cards left.");
        }

        return List.copyOf(cards.subList(next, next += n));
    }

    @Nullable
    public Card draw() {
        if (next >= cards.size()) {
            return null;
        }
        return cards.get(next++);
    }

    public int size() {
        return this.cards.size();
    }

    public int remaining() {
        return size() - next;
    }
}
