package com.alterdekim.hearthhack.dbf;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.*;

import java.util.List;
import java.util.Optional;

@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CardsDBF {
    @JacksonXmlProperty(isAttribute = true)
    private String name;

    @JsonProperty("SourceFingerprint")
    private String sourceFingerprint;

    @JsonProperty("Column")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<DBFColumn> columns;

    @JsonProperty("Record")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<DBFCard> records;


    public Optional<DBFCard> findWithMiniGuid(String miniGuid) {
        return records.stream()
                .filter(r -> r.getFields().stream().anyMatch(c -> c.getColumn().equals("NOTE_MINI_GUID")))
                .filter(r -> r.getFields().stream().filter(c -> c.getColumn().equals("NOTE_MINI_GUID")).findFirst().get().getVal().equals(miniGuid))
                .findFirst();
    }
}
