package meerkat.voting.gui.controllersFX;

import javafx.fxml.FXML;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import meerkat.protobuf.Voting;
import meerkat.voting.controller.callbacks.VoterCancelThrowable;
import meerkat.voting.gui.ui.UIUtils;
import meerkat.voting.gui.ui.uicommands.RaceVotingUICommand;

import java.util.ArrayList;
import java.util.List;

//import meerkat.voting.gui.ui.UIUtils;

/**
 * Created by Vladimir Eliezer Tokarev on 8/27/2016.
 * ChannelChoiceController handle the behavior of select channel section screen
 */
public class VotingController {

//    private List<Integer> chanelValue;
//    private int pointer;
//    private boolean lock;

    List<Voting.BallotAnswer> answers = new ArrayList<>();
    List<Voting.BallotQuestion> questions = new ArrayList<>();
    int index = 0;
    RaceVotingUICommand command = (RaceVotingUICommand) VistaNavigator.getCurrentCommand();

    @FXML ToggleGroup answerGroup;
    @FXML private Text question;
    @FXML private Pane questionPane;

    @FXML
    public void initialize() {
        questions = command.getQuestions();
        answerGroup = new ToggleGroup();
        showQuestion();
    }


    @FXML
    private void nextQuestion() {
        if (questions.size()==0) {
            questions = command.getQuestions();
            answerGroup = new ToggleGroup();
        }
        if (answerGroup.getSelectedToggle()!=null) {
            String s = answerGroup.getSelectedToggle().getUserData().toString();
            answers.add(UIUtils.translateStringAnswerToProtoBufMessageAnswer(s));
            index++;
        }
        if (index < questions.size()) {
            showQuestion();
        } else {
            //return answers to main threads
            nextPane();
        }
    }

    private void showQuestion() {
            Voting.BallotQuestion ballotQuestion = questions.get(index);
            question.setText("This is question n. "+index);
            answerGroup.getToggles().clear();
            VBox vbox = VistaNavigator.uiThread.showQuestionInFX(ballotQuestion, answerGroup);
            questionPane.getChildren().clear();
            questionPane.getChildren().addAll(vbox);
    }

    @FXML
    private void goBack() throws VoterCancelThrowable{
        if (index == 0) {
            doCancel();
        } else {
            --index;
            answers.remove(index);
            nextQuestion();
        }
    }

    @FXML
    private void doCancel() throws VoterCancelThrowable{
        // TODO: 6/14/2017 check flow and decide what to do...
        VoterCancelThrowable e = new VoterCancelThrowable();
        command.getCallback().onFailure(e);
        //see what is done in the original (basically need to call the callback of the command onFailure()
    }


    private void nextPane() {
        VistaNavigator.loadVista(VistaNavigator.LOADING);
        VistaNavigator.uiThread.answerVotingScreen(command, answers);
    }


}
