package com.alterdekim.hearthhack.util;

import com.alterdekim.PegasusGame;
import com.alterdekim.hearthhack.game.GameTag;
import com.alterdekim.hearthhack.game.TagZone;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;


public class GameDeck {
    private final List<PegasusGame.PowerHistoryEntity.Builder> deck;

    public GameDeck() {
        this.deck = new ArrayList<>();
    }

    public PegasusGame.PowerHistoryEntity.Builder getCard(Integer i) {
        return this.deck.get(i);
    }

    public Optional<PegasusGame.PowerHistoryEntity.Builder> getByEntityId(Integer i) { return this.deck.stream().filter(c -> c.getEntity() == i).findFirst(); }

    public Integer getSize() {
        return this.deck.size();
    }

    public void addCard(PegasusGame.PowerHistoryEntity.Builder b) {
        this.deck.add(b);
    }

    public void updateCard(PegasusGame.PowerHistoryEntity.Builder b, Integer i) {
        deck.set(i, b);
    }

    public List<PegasusGame.PowerHistoryEntity.Builder> getHandCards() {
        return getCardsByTag(GameTag.ZONE, TagZone.HAND.ordinal());
    }

    public List<PegasusGame.PowerHistoryEntity.Builder> getCardsByTag(GameTag tag, Integer val) {
        return deck
                .stream()
                .filter(c -> c.getTagsList()
                        .stream()
                        .anyMatch(f -> f.getName() == tag.getValue() && f.getValue() == val)
                )
                .collect(Collectors.toList());
    }

    public void shuffleDeck() {
        Collections.shuffle(deck);
    }
}
