package meerkat.voting.gui.panels.select_candidate_name;

import com.google.common.util.concurrent.FutureCallback;
import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;
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 name screen
 */
public class SelectCandidateNameController extends TwoWayNode implements FutureCallback<String>{
    private final Logger logger = LoggerFactory.getLogger(SelectCandidateNameController.class);

    @FXML
    private void GetToSelectChannel(MouseEvent mousePressed){
        this.currentStage.close();
        this.currentStage.setScene(this.previous.GetCurrentScene());
        this.previous.UpdateNode();
        this.currentStage.show();
        this.logger.debug("Jumping to straigth select channel panel.");
    }

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

    /**
     * Updates the voter options according to the VotingBoothConfiguration
     */
    private void UpdateVoterOptions(){
        StringsAnswersUpdater updater = new StringsAnswersUpdater(this.currentStage);
        updater.UpdateAnswers(this.config.NameSelectionQuestion);
        updater.SetUpdateAnswers(this);
        this.logger.debug("Created strings answer updater and updated the answers.");
    }

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

    @Override
    public void onSuccess(String result) {
       this.votersBallot.VotersNameSelection = result;
    }

    @Override
    public void onFailure(Throwable t) {
        // Write log that the string answers updater failed to update the answer
    }
}
