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.UIUtils;
import meerkat.voting.gui.ui.uicommands.ChannelChoiceUICommand;

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

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

//    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;
    ChannelChoiceUICommand command = (ChannelChoiceUICommand) VistaNavigator.getCurrentCommand();
    String questionText;

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

    @FXML
    public void initialize() {
//        Voting.UIElementDataType.TEXT
        questions = command.getQuestions();
        answerGroup = new ToggleGroup();
        int choiceType = questions.get(index).getQuestion().getTypeValue();
        System.out.println(choiceType);
        switch (choiceType) {
            case 0: showQuestion(); //0==TEXT
                    break;
            case 1: showCodeSelection();
                    break;
//            : showError();
        }
    }

//    @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 showCodeSelection() {
        questionText = "Here we show code pad.";
        question.setText(questionText);
    }

    private void showQuestion() {
            Voting.BallotQuestion ballotQuestion = questions.get(index);
            questionText = "Please answer this question before voting:";
            question.setText(questionText);
            answerGroup.getToggles().clear();
            VBox vbox = VistaNavigator.uiThread.showQuestionInFX(ballotQuestion, answerGroup);
            questionPane.getChildren().clear();
            questionPane.getChildren().addAll(vbox);
    }

    private void showError() {
        questionText = "There was an error reading the channel, please go back to polling station.";
        question.setText(questionText);

    }

    @FXML
    private void nextPane() {
        if (answerGroup.getSelectedToggle()!=null) {
            String s = answerGroup.getSelectedToggle().getUserData().toString();
            answers.add(UIUtils.translateStringAnswerToProtoBufMessageAnswer(s));
        } else {
            warningMsg.setText("(Please give an answer to continue)");
        }
        if (answers.size()!=0) {
            VistaNavigator.loadVista(VistaNavigator.LOADING);
            VistaNavigator.uiThread.answerChannelChoiceScreen(command, answers);
        }
    }


}
