package meerkat.voting.gui.panels.select_candidate_by_picture;

import com.google.common.util.concurrent.FutureCallback;
import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;
import meerkat.protobuf.BallotQuestionUiElement;
import meerkat.voting.gui.managment.TwoWayNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Vladimir Eliezer Tokarev on 8/27/2016.
 * SelectCandidateNameController handle the behavior of select by picture screen
 */
public class SelectCandidateByPictureController extends TwoWayNode implements FutureCallback<BallotQuestionUiElement.UIAnswer> {

    private final Logger logger = LoggerFactory.getLogger(SelectCandidateByPictureController.class);

    @FXML
    private void GetToSelectByName(MouseEvent mousePressed){
        this.currentStage.close();
        this.currentStage.setScene(this.previous.GetCurrentScene());
        this.previous.UpdateNode();
        this.currentStage.show();
        this.logger.debug("Jumping to select candidate by name panel.");
    }

    @FXML
    private void GetToBallotSummary(MouseEvent mousePressed){
        this.currentStage.close();
        this.currentStage.setScene(this.next.GetCurrentScene());
        this.next.UpdateNode();
        this.currentStage.show();
        this.logger.debug("Jumping to ballot summary panel.");
    }

    /**
     * Updates the voter options according to the VotingBoothConfiguration
     */
    private void UpdateVoterOptions(){
        PicturesAnswersUpdater updater = new PicturesAnswersUpdater(this.currentStage);
        updater.SetImageUpdate(this);
        updater.UpdateAnswers(this.config.NameSelectionByPictureQuestion);
        this.logger.debug("Created pictures answers updater and updated the answers.");
    }

    @Override
    public void UpdateNode() {
        this.UpdateVoterOptions();
    }

    @Override
    public void onSuccess(BallotQuestionUiElement.UIAnswer result) {
        this.votersBallot.VotersImageSelection = result;
    }

    @Override
    public void onFailure(Throwable t) {
        // log about failure in updating proccess
    }
}
