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

import eu.jonahbauer.wizard.common.model.Card;
import eu.jonahbauer.wizard.core.util.Pair;
import lombok.Getter;

import java.util.List;
import java.util.UUID;

@Getter
public class ColoredCard extends GameCard {
    private final double value;
    private final Card.Suit suit;

    public ColoredCard(Card card, double value, Card.Suit suit) {
        super(card);
        this.value = value;
        this.suit = suit;
    }

    @Override
    public void play(UUID player, List<Card> hand, List<Pair<UUID, Card>> stack) {
        Card.Suit suit = CardUtils.getTrickSuit(stack);
        if (this.suit != suit && canFollowSuit(hand, suit)) {
            throw new IllegalArgumentException("Must follow suit.");
        }

        super.play(player, hand, stack);
    }

    @Override
    public Card.Suit getTrumpSuit() {
        return getSuit();
    }

    @Override
    public Card.Suit getTrickSuit() {
        return getSuit();
    }

    @Override
    public boolean isPlayable(List<Card> hand, List<Pair<UUID, Card>> stack) {
        Card.Suit suit = CardUtils.getTrickSuit(stack);
        if (this.suit != suit && canFollowSuit(hand, suit)) {
            return false;
        }

        return super.isPlayable(hand, stack);
    }

    private boolean canFollowSuit(List<Card> hand, Card.Suit suit) {
        return hand.stream()
                   .map(GameCards::get)
                   .anyMatch(card -> card instanceof ColoredCard coloredCard && coloredCard.getSuit() == suit);
    }
}
