package uk.gov.register.presentation;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Iterables;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;

public class EntryView {
    private final int serialNumber;
    private final String hash;
    private final String registerName;
    private final Map<String, FieldValue> entryMap;

    public EntryView(int serialNumber, String hash, String registerName, Map<String, FieldValue> entryMap) {
        this.hash = hash;
        this.registerName = registerName;
        this.entryMap = new TreeMap<>(entryMap); // TreeMap so we get fields in sorted order
        this.serialNumber = serialNumber;
    }


    @JsonProperty("entry")
    public Map<String, FieldValue> getContent() {
        return entryMap;
    }

    @JsonProperty("hash")
    public String getHash() {
        return hash;
    }

    public Optional<FieldValue> getField(String fieldName) {
        return Optional.ofNullable(entryMap.get(fieldName));
    }

    @JsonProperty("serial-number")
    public int getSerialNumber() {
        return serialNumber;
    }

    @SuppressWarnings("unused, used from html templates")
    @JsonIgnore
    public Iterable<Map.Entry<String, FieldValue>> getNonPrimaryFields() {
        return Iterables.filter(entryMap.entrySet(), e -> !e.getKey().equals(registerName));
    }

    @SuppressWarnings("unused, used from html templates")
    public String primaryKey() {
        return entryMap.get(registerName).value();
    }

    @SuppressWarnings("unused, used from html templates")
    public String registerName() {
        return registerName;
    }

    public Set<String> allFields() {
        // TODO: this should return all possible fields, not just all present fields
        return entryMap.keySet();
    }
}



@@ -236,30 +236,8 @@
 al;%0A
-import java.util.Set;%0A
 impo
@@ -1720,171 +1720,8 @@
    %7D
-%0A%0A    public Set%3CString%3E allFields() %7B%0A        // TODO: this should return all possible fields, not just all present fields%0A        return entryMap.keySet();%0A    %7D
 %0A%7D%0A%0A

remove now-unused allFields() method