import java.util.ArrayList;
import java.util.Random;

/**
 * Talking Robot Programming Project: <br>
 * The SpeechToTextStub class is a "stub" for a class that would
 * listen for questions from the user and convert them to <code>String</code>
 * objects.  This stub class does not do actual speech-to-text 
 * conversion; it actually just returns questions that were provided
 * to it as test cases.
 * <br> <br>
 * Created: <br>
 *   2 April,  Alyce Brady<br>
 * <br>
 * Modifications: <br>
 *   (date), (your name), Modified to (do what? ....) <br>
 * 
 * @author (your name) (with assistance from)
 * @version (a version number or a date)
 */
public class SpeechToTextStub
{
    // Define constants used throughout this class.
    private final String UNRECOGNIZED_PHRASE = "Unrecognized phrase";

    // Define the instance variables that compose the object's state.
    private ArrayList<String> phrases;
    private Random generator;
    private boolean randomizing;
    private int iterator;

    /** Constructs a Speech-to-Text Stub object. */
    public SpeechToTextStub()
    {
        this.phrases = new ArrayList<String>();
        this.generator = new Random();
        this.randomizing = false;
        this.iterator = 0;
    }

    /** Puts the recognizer in "randomizing" mode, so that every time
     * getPhrase() is called, it returns a random test case.
     */
    public void randomizePhrases()
    {
        this.randomizing = true;
    }

    /** Adds a phrase that the speech-to-text converter will "recognize".
     *      @param phrase        the phrase to add
     */
    public void addTestCase(String phrase)
    {
        this.phrases.add(phrase);
    }

    /** Returns the phrase spoken most recently by the user.  Returns
     * "Unrecognized phrase" if the user spoke a phrase that the
     * speech-to-text converter did not recognize.
     *  (Actually, since this is just a substitute "stub" for a real
     *  speech-to-text converter, it will just return the next test case
     *  or, if randomizing has been turned on, will return a random test
     *  case, or, if all test cases have been returned (and not
     *  randomizing), will return "Unrecognized phrase".
     *      @return  the next phrase
     */
    public String getPhrase()
    {
        // Should we return a random test case?
        if ( randomizing )
        {
            int index = this.generator.nextInt(this.phrases.size());
            return this.phrases.get(index);
        }

        // Otherwise, are we still iterating through test cases?
        else if ( this.iterator < phrases.size() )
        {
            String phraseToReturn = this.phrases.get(this.iterator);
            this.iterator++;
            return phraseToReturn;
        }
        else
        {
            // No more test cases.
            return UNRECOGNIZED_PHRASE;
        }
    }
}
