package meerkat.voting.controller.callbacks;

import meerkat.protobuf.Voting.UIElement;
import meerkat.voting.controller.commands.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * A controller callback for the Commit request to the output-device.
 * When committing is done, the callback's onSuccess() method is called to register a new ChooseFinalizeOptionCommand
 * to the controller
 */
public class OutputDeviceCommitCallback extends ControllerCallback<Void> {
    protected final static Logger logger = LoggerFactory.getLogger(OutputDeviceCommitCallback.class);
    protected final UIElement outputDeviceFailureMessage;

    public OutputDeviceCommitCallback(int requestId,
                                      long ballotSerialNumber,
                                      LinkedBlockingQueue<ControllerCommand> controllerQueue,
                                      UIElement outputDeviceFailureMessage) {
        super(requestId, ballotSerialNumber, controllerQueue);
        this.outputDeviceFailureMessage = outputDeviceFailureMessage;
    }

    @Override
    public void onSuccess(Void v) {
        logger.debug("callback for output device commit success");
        enqueueCommand(new ChooseFinalizeOptionCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }

    @Override
    public void onFailure(Throwable t) {
        logger.error("OutputDeviceCommitCallback got a failure: " + t);
        enqueueCommand(new ReportErrorCommand(getRequestIdentifier(),
                getBallotSerialNumber(),
                outputDeviceFailureMessage));
    }
}
