package meerkat.mixer.necessary;

import meerkat.protobuf.BulletinBoardApi.*;

import java.util.List;

/**
 * Created by Arbel Deutsch Peled on 14-Dec-15.
 *
 * ToDo: this should be replaced by AsyncBulletinBoardClient at BB project
 */
public interface AsyncBulletinBoardClient extends BulletinBoardClient {

    public interface ClientCallback<T> {
        void handleCallback(T msg);
        void handleFailure(Throwable t);
    }

    public interface MessageHandler {
        void handleNewMessages(List<BulletinBoardMessage> messageList);
    }

    /**
     * Post a message to the bulletin board in an asynchronous manner
     * @param msg is the message to be posted
     * @param callback is a class containing methods to handle the result of the operation
     * @return a unique message ID for the message, that can be later used to retrieve the batch
     */
    public MessageID postMessage(BulletinBoardMessage msg, ClientCallback<?> callback);

    /**
     * This method allows for sending large messages as a batch to the bulletin board
     * @param signerId is the canonical form for the ID of the sender of this batch
     * @param batchId is a unique (per signer) ID for this batch
     * @param batchChunkList is the (canonically ordered) list of data comprising the batch message
     * @param startPosition is the location (in the batch) of the first entry in batchChunkList (optionally used to continue interrupted post operations)
     * @param callback is a callback function class for handling results of the operation
     * @return a unique message ID for the entire message, that can be later used to retrieve the batch
     */
    public MessageID postBatch(byte[] signerId, int batchId, List<BatchChunk> batchChunkList, int startPosition, ClientCallback<?> callback);

    /**
     * Overloading of the postBatch method in which startPosition is set to the default value 0
     */
    public MessageID postBatch(byte[] signerId, int batchId, List<BatchChunk> batchChunkList, ClientCallback<?> callback);

    /**
     * Check how "safe" a given message is in an asynchronous manner
     * The result of the computation is a rank between 0.0 and 1.0 indicating the fraction of servers containing the message
     * @param id is the unique message identifier for retrieval
     * @param callback is a callback function class for handling results of the operation
     */
    public void getRedundancy(MessageID id, ClientCallback<Float> callback);

    /**
     * Read all messages posted matching the given filter in an asynchronous manner
     * Note that if messages haven't been "fully posted", this might return a different
     * set of messages in different calls. However, messages that are fully posted
     * are guaranteed to be included.
     * @param filterList return only messages that match the filters (null means no filtering).
     * @param callback is a callback function class for handling results of the operation
     */
    public void readMessages(MessageFilterList filterList, ClientCallback<List<BulletinBoardMessage>> callback);

    /**
     * Read a given batch message from the bulletin board
     * @param signerId is the ID of the signer (sender) of the batch message
     * @param batchId is the unique (per signer) ID of the batch
     * @param callback is a callback class for handling the result of the operation
     */
    public void readBatch(byte[] signerId, int batchId, ClientCallback<CompleteBatch> callback);

    /**
     * Subscribes to a notifier that will return any new messages on the server that match the given filters
     * @param filterList defines the set of filters for message retrieval
     * @param messageHandler defines the handler for new messages received
     */
    public void subscribe(MessageFilterList filterList, MessageHandler messageHandler);

}
