package meerkat.voting.gui.ui;

import com.google.protobuf.ByteString;
import meerkat.protobuf.Voting;
import org.slf4j.Logger;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Vladimir Eliezer Tokarev on 9/17/2016.
 * This class contains methods that are used by all the UI's implementations
 */
public class UIUtils {

    /**
     * Convert string into protobuff message answer
     * @param s string represent of an answer for a question
     * @return protobuff message answer
     */
    public static Voting.BallotAnswer translateStringAnswerToProtoBufMessageAnswer(String s) {
        Voting.BallotAnswer.Builder bab = Voting.BallotAnswer.newBuilder();
        StringTokenizer st = new StringTokenizer(s);
        while (st.hasMoreTokens()) {
            bab.addAnswer(Integer.parseInt(st.nextToken()));
        }
        return bab.build();
    }


    /**
     * checks whether the data of the question is only text. This is the only type we can handle in ConsoleUI
     * @param question a ballot question to check
     * @return True if the question data is only text
     */
    public static boolean isQuestionOnlyText(Voting.BallotQuestion question) {
        boolean isText = true;
        if (question.getQuestion().getType() != Voting.UIElementDataType.TEXT
                || question.getDescription().getType() != Voting.UIElementDataType.TEXT) {
            isText = false;
        }
        for (Voting.UIElement answer : question.getAnswerList()) {
            if (answer.getType() != Voting.UIElementDataType.TEXT) {
                isText = false;
            }
        }
        return isText;
    }

    /**
     * asserts that the question data matches the types that we can handle in the ConsoleUI
     * (better and more sophisticated UI will be able to handle more types of data)
     * @param questions list of the questions
     * @param logger logger to which writing logs
     */
    public static void assertQuestionsAreValid (List<Voting.BallotQuestion> questions,Logger logger) {
        for (int index = 0; index < questions.size(); ++index) {
            Voting.BallotQuestion question = questions.get(index);
            if (question.getIsMandatory()) {
                String errorMessage = "askVoterQuestions: question number " + index + " is marked as mandatory";
                logger.error(errorMessage);
                throw new UnsupportedOperationException(errorMessage);
            }
            if (!UIUtils.isQuestionOnlyText(question)) {
                String errorMessage = "askVoterQuestions: question number " + index + " is not only text";
                logger.error(errorMessage);
                throw new UnsupportedOperationException(errorMessage);
            }
        }
    }

    /**
     * Returns the UTF8 decoding of byte-string data
     * @param data string of data
     * @return data converted to ut8 str
     */
    public static String bytesToString(ByteString data) {
        return data.toStringUtf8();
    }

}
