package lc.esp.eo.sdk.schema;

import lc.mecha.lang.TypedObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Classes extending this class represent EO schema entities which provide user-defined fields.
 *
 * @author Alex Leigh
 * @since mk20 (TOKYO PROTOCOL)
 */
public abstract class FieldBearer extends TypedObject {
    private final boolean isRequired;

    private final HashMap<String, FieldSchema> fields = new HashMap<>();

    public FieldBearer(String type, boolean isRequired) {
        super(type);
        this.isRequired = isRequired;
    }

    public void putField(FieldSchema field) {
        fields.put(field.getName(), field);
    }

    /**
     * Return the names of all the fields in this schema which must be accounted for.
     */

    public Set<String> getRequiredFieldNames() {
        HashSet<String> required = new HashSet<>();
        for(FieldSchema field : fields.values()) {
            if(field.isRequired()) required.add(field.getName());
        }
        return required;
    }

    public boolean isRequired() {
        return isRequired;
    }

    @Override
    public String toString() {
        return "FieldBearer{" +
                "isRequired=" + isRequired +
                ", fields=" + fields +
                '}';
    }
}
