package org.jivesoftware.smackx.ikey.record;

import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
import org.jxmpp.jid.EntityBareJid;
import org.pgpainless.key.OpenPgpV4Fingerprint;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class IkeyRecord implements Serializable {

    @Getter
    @Setter
    private PGPPublicKeyRing superordinate;

    @Getter
    private final List<IkeySubordinateRecord> subordinates = new ArrayList<>();

    @Getter
    @Setter
    private Date timestamp;

    @Getter
    @Setter
    private EntityBareJid jid;

    @Getter
    @Setter
    private OpenPgpTrustStore.Trust trust = OpenPgpTrustStore.Trust.undecided;

    public IkeyRecord() {

    }

    public IkeyRecord(EntityBareJid jid, Date timestamp, PGPPublicKeyRing superordinate, List<IkeySubordinateRecord> subordinates, OpenPgpTrustStore.Trust trust) {
        this.jid = jid;
        this.timestamp = timestamp;
        this.superordinate = superordinate;
        this.subordinates.addAll(subordinates);
        this.trust = trust;
    }

    public boolean hasSubordinate(OpenPgpV4Fingerprint fingerprint) {
        for (IkeySubordinateRecord subordinateRecord : subordinates) {
            if (subordinateRecord.getType().equals(OxSubordinateRecord.TYPE)) {
                OxSubordinateRecord record = (OxSubordinateRecord) subordinateRecord;
                if (record.getOxFingerprint().equals(fingerprint)) {
                    return true;
                }
            }
        }
        return false;
    }
}
