package solitaire;

import java.util.LinkedList;
import java.util.List;

final class Foundation {
    private final List<Card> cards;
    private final Suit suit;

    Foundation(Suit suit) {
	cards = new LinkedList<>();
	this.suit = suit;
    }

    @Override
    public String toString() {
	if (cards.isEmpty())
	    return String.format("( %s )", suit);

	return String.format("(%s)", cards.getLast());
    }

    private boolean isValidNextCard(Card nextCard) {
	if (nextCard.getSuit() != suit)
	    return false;

	if (cards.isEmpty())
	    return nextCard.getRank() == Rank.ACE;

	Card lastCard = cards.getLast();

	return nextCard.getRank().isSuccessorOf(lastCard.getRank());
    }

    boolean add(Card card) {
	if (!isValidNextCard(card))
	    return false;

	cards.add(card);
	return true;
    }

    Suit getSuit() {
	return suit;
    }

    boolean isFull() {
	return cards.size() == Rank.RANKS.size();
    }
}
