// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cert.crmf;

import org.bouncycastle.asn1.cmp.CertResponse;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.crmf.EncryptedKey;
import org.bouncycastle.asn1.cms.EnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.asn1.cmp.CertOrEncCert;
import org.bouncycastle.asn1.cmp.CMPCertificate;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.cmp.CertifiedKeyPair;
import org.bouncycastle.asn1.cmp.PKIStatusInfo;
import org.bouncycastle.asn1.ASN1Integer;

public class CertificateResponseBuilder
{
    private final ASN1Integer certReqId;
    private final PKIStatusInfo statusInfo;
    private CertifiedKeyPair certKeyPair;
    private ASN1OctetString rspInfo;
    
    public CertificateResponseBuilder(final ASN1Integer certReqId, final PKIStatusInfo statusInfo) {
        this.certReqId = certReqId;
        this.statusInfo = statusInfo;
    }
    
    public CertificateResponseBuilder withCertificate(final X509CertificateHolder x509CertificateHolder) {
        if (this.certKeyPair != null) {
            throw new IllegalStateException("certificate in response already set");
        }
        this.certKeyPair = new CertifiedKeyPair(new CertOrEncCert(new CMPCertificate(x509CertificateHolder.toASN1Structure())));
        return this;
    }
    
    public CertificateResponseBuilder withCertificate(final CMPCertificate cmpCertificate) {
        if (this.certKeyPair != null) {
            throw new IllegalStateException("certificate in response already set");
        }
        this.certKeyPair = new CertifiedKeyPair(new CertOrEncCert(cmpCertificate));
        return this;
    }
    
    public CertificateResponseBuilder withCertificate(final CMSEnvelopedData cmsEnvelopedData) {
        if (this.certKeyPair != null) {
            throw new IllegalStateException("certificate in response already set");
        }
        this.certKeyPair = new CertifiedKeyPair(new CertOrEncCert(new EncryptedKey(EnvelopedData.getInstance(cmsEnvelopedData.toASN1Structure().getContent()))));
        return this;
    }
    
    public CertificateResponseBuilder withResponseInfo(final byte[] array) {
        if (this.rspInfo != null) {
            throw new IllegalStateException("response info already set");
        }
        this.rspInfo = new DEROctetString(array);
        return this;
    }
    
    public CertificateResponse build() {
        return new CertificateResponse(new CertResponse(this.certReqId, this.statusInfo, this.certKeyPair, this.rspInfo));
    }
}
