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 Finalize request to the output-device.
 * When finalizing (either cast or audit) is done,
 * the callback's onSuccess() method is called to register a new command to the controller to restart the voting process
 */
public class OutputDeviceFinalizeCallback extends ControllerCallback<Void> {
    protected final static Logger logger = LoggerFactory.getLogger(OutputDeviceFinalizeCallback.class);
    protected final UIElement outputDeviceFailureMessage;

    public OutputDeviceFinalizeCallback(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 finalize success");
        enqueueCommand(new RestartVotingCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }

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