package meerkat.voting.controller.callbacks;

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


/**
 * A controller callback for the StartSession request to the UI.
 * Upon approval of the voter, it registers a new ChannelChoiceCommand to the controller queue (which
 * then starts the channel choice process)
 */
public class NewVoterCallback extends ControllerCallback<Void> {
    protected final static Logger logger = LoggerFactory.getLogger(NewVoterCallback.class);

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

    @Override
    public void onSuccess(Void v) {
        logger.debug("callback for new voting returned success");
        enqueueCommand(new ChannelChoiceCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }

    @Override
    public void onFailure(Throwable t) {
        logger.error("New voting session got a failure: " + t);
        enqueueCommand(new RestartVotingCommand(getRequestIdentifier(), getBallotSerialNumber()));
    }
}
