package meerkat.bulletinboard;

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


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

    private Digest digest;

    public GenericBulletinBoardDigest(Digest digest) {
        this.digest = digest;
    }

    @Override
    public byte[] digest() {
        return digest.digest();
    }

    @Override
    public MessageID digestAsMessageID() {
        return digest.digestAsMessageID();
    }

    @Override
    public void update(Message msg) {
        digest.update(msg);
    }

    @Override
    public void update(byte[] data) {
        digest.update(data);
    }

    @Override
    public void reset() {
        digest.reset();
    }

    @Override
    public GenericBulletinBoardDigest clone() throws CloneNotSupportedException{
        return new GenericBulletinBoardDigest(digest.clone());
    }

    @Override
    public void update(BulletinBoardMessage msg) {
        update(msg.getMsg());
    }

    @Override
    public void update(UnsignedBulletinBoardMessage msg) {

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

        update(msg.getTimestamp());

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

    }

}
