from random import randrange, shuffle

from server.model.card import Card
from server.model.color import Color
from server.model.value import Value


class Deck:
    def __init__(self, cards=None):
        if cards is None:
            cards = [Card(value=v, color=c) for v in Value for c in Color]
        else:
            print("Deck init with cards:", cards, len(cards))
        self.cards = cards
        self.defausse = []

    def __len__(self):
        return len(self.cards)

    def random_card(self) -> Card:
        if not self.cards:
            self.reset()

        card = self.cards.pop(randrange(len(self.cards)))
        self.defausse.append(card)
        return card

    def reset(self):
        self.cards.extend(self.defausse)
        self.defausse.clear()
        shuffle(self.cards)
