package com.alterdekim.hearthhack.xml;

import com.alterdekim.hearthhack.game.GameTag;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import lombok.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@ToString
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CardsXML {
    @JsonProperty("Entity")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<XMLEntity> entities;


    public Optional<XMLEntity> findEntityByCardId(String cardId) {
        return this.entities.stream().filter(p -> p.getCardId().equals(cardId)).findFirst();
    }

    public List<XMLEntity> findEntitiesByTagVal(GameTag tag, Integer val) {
        return this.entities.stream()
                .filter(p -> p.getTags()
                        .stream()
                        .anyMatch(f -> f.getEnumID().intValue() == tag.getValue().intValue() && f.getValue().intValue() == val.intValue())
                )
                .collect(Collectors.toList());
    }

    public List<XMLEntity> findEntitiesByTagVal(GameTag tag, String val) {
        return this.entities.stream()
                .filter(p -> p.getTags()
                        .stream()
                        .anyMatch(f -> f.getEnumID().intValue() == tag.getValue().intValue() && f.getVal().equals(val))
                )
                .collect(Collectors.toList());
    }
}
