package meerkat.bulletinboard;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardApi.*;
import meerkat.protobuf.Crypto;

import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.CertificateException;


/**
 * Created by Arbel Deutsch Peled on 19-Dec-15.
 * Wrapper class for digesting Batches in a standardized way
 */
public class GenericBulletinBoardSignature implements BulletinBoardSignature {

    private DigitalSignature signer;

    public GenericBulletinBoardSignature(DigitalSignature signer) {
        this.signer = signer;
    }

    @Override
    public void updateContent(BulletinBoardMessage msg) throws SignatureException{
        signer.updateContent(msg.getMsg());
    }

    @Override
    public void updateContent(UnsignedBulletinBoardMessage msg) throws SignatureException{

        for (ByteString tag : msg.getTagList().asByteStringList()){
            updateContent(tag.toByteArray());
        }

        updateContent(msg.getTimestamp());

        if (msg.getDataTypeCase() == UnsignedBulletinBoardMessage.DataTypeCase.DATA){
            updateContent(msg.getData().toByteArray());
        }

    }

    @Override
    public void loadVerificationCertificate(Crypto.SignatureVerificationKey cert) throws CertificateException {
        signer.loadVerificationCertificate(cert);
    }

    @Override
    public ByteString computeCertificateFingerprint(Crypto.SignatureVerificationKey cert) throws CertificateException {
        return signer.computeCertificateFingerprint(cert);
    }

    @Override
    public void loadVerificationCertificates(InputStream certStream) throws CertificateException {
        signer.loadVerificationCertificates(certStream);
    }

    @Override
    public void clearVerificationCertificates() {
        signer.clearVerificationCertificates();
    }

    @Override
    public void updateContent(byte[] data) throws SignatureException {
        signer.updateContent(data);
    }

    @Override
    public void updateContent(Message msg) throws SignatureException {
        signer.updateContent(msg);
    }

    @Override
    public Crypto.Signature sign() throws SignatureException {
        return signer.sign();
    }

    @Override
    public void initVerify(Crypto.Signature sig) throws CertificateException, InvalidKeyException {
        signer.initVerify(sig);
    }

    @Override
    public boolean verify() {
        return signer.verify();
    }

    @Override
    public KeyStore.Builder getPKCS12KeyStoreBuilder(InputStream keyStream, char[] password) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
        return signer.getPKCS12KeyStoreBuilder(keyStream, password);
    }

    @Override
    public void loadSigningCertificate(KeyStore.Builder keyStoreBuilder) throws IOException, CertificateException, UnrecoverableKeyException {
        signer.loadSigningCertificate(keyStoreBuilder);
    }

    @Override
    public ByteString getSignerID() {
        return signer.getSignerID();
    }

    @Override
    public Crypto.SignatureVerificationKey getSignerPublicKey() { return signer.getSignerPublicKey();  }

    @Override
    public void clearSigningKey() {
        signer.clearSigningKey();
    }

}
