package meerkat.voting.controller.callbacks;

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

/**
 * This is quite a special callback. It is not issued in a normal flow of the voting.
 * This callback is made only for a request to the UI to show the voter an error message.
 * Upon approval of the voter, the method onSuccess() of this callback is called, and the voting
 * is reset through a command to the controller's queue
 */
public class ErrorMessageRestartCallback extends ControllerCallback<Integer> {
    protected final static Logger logger = LoggerFactory.getLogger(ErrorMessageRestartCallback.class);

    public ErrorMessageRestartCallback(int requestId,
                                       long ballotSerialNumber,
                                       LinkedBlockingQueue<ControllerCommand> controllerQueue) {
        super(requestId, ballotSerialNumber, controllerQueue);
    }

    @Override
    public void onSuccess(Integer i) {
        enqueueCommand(new RestartVotingCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }

    @Override
    public void onFailure(Throwable t) {
        logger.error("Error message execution initiated a failure: " + t);
        enqueueCommand(new RestartVotingCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }
}
