import javabook.*;

/**
 * This class is the top-level controller object for managing
 * all other objects in the EggyPeggy program.
 *
 * @author Dr. Caffeine
 *
 */
class EggyPeggy                // Step 3: Generate an Eggy-Peggy String
{

//--------------------------------
//    Data Members
//--------------------------------
    
    /**
     * Text to precede every vowel
     */
    private static final String      EGG = "egg";

    private MainWindow        mainWindow;
    private InputBox          inputBox;
    private OutputBox         outputBox; 
    private ResponseBox       responseBox;
    
    /**
     * Input string to convert
     */
    private String            inputString;
    
    /**
     * Converted Eggy-Peggy string
     */
    private String            eggyPeggyString;

//--------------------------------
//    Constructors
//--------------------------------

    /**
     * Default constructor. 
     * The default window title is "Let’s Play EggyPeggy".
     */
    public EggyPeggy( )
    {
        mainWindow     = new MainWindow  ("Let’s Play EggyPeggy");
        outputBox      = new OutputBox   ( mainWindow );
        responseBox    = new ResponseBox ( mainWindow );
        inputBox       = new InputBox    ( mainWindow );
    
        mainWindow.show();
        outputBox.show();
    }        


//-------------------------------------------------
//        Public Methods:
// 
//            void        start        (        )
//
//------------------------------------------------

    /**
     * Top-level method that calls other private methods
      * to play the Eggy-Peggy games.     
     */
    public void start ( )
    {
        int answer;
        
        describeGame();

        answer = responseBox.prompt("Do you want to play Eggy-Peggy?");

        while ( answer == ResponseBox.YES ) {

            getInput( );
            generateEggyPeggy( );                            
            displayResult( );                            

            answer 
            = responseBox.prompt("Do you want to play another Eggy-Peggy?");
        }
    }

//-------------------------------------------------
//        Private Methods:
// 
//        void     describeGame        (            )
//        void     displayResult       (            )
//        void     generateEggyPeggy   (            )
//        void     getInput            (            )
//
//        boolean  isVowel             (            )
//
//------------------------------------------------
    
    /**
     * Provides a brief explanation of the program to the user.     
     *
     */    
    private void describeGame( )
    {
        outputBox.printLine("Inside describeGame");       //TEMP
    }


    /**
     * Show the input string and the eggy-peggy string generated
     * from the input string.
     *     
     */
    private void displayResult( )
    {
        outputBox.skipLine(3);                    
        outputBox.printLine("Input String:");
        outputBox.printLine("      " +     inputString);
        
        outputBox.skipLine(1);
        outputBox.printLine("Eggy Peggy:");
        outputBox.printLine("      " +     eggyPeggyString);   
    }
    

    /**
     * Generate a new string from the input string by putting the 
     * word "egg" in front of all vowels in the input string. 
     *
     */    
    private void generateEggyPeggy( )
    {
        StringBuffer   outputStrBuf = new StringBuffer("");
        int            length       = inputString.length();
        char           ch;
    
        for (int i = 0; i < length; i++ ) {
            
            ch = inputString.charAt(i);
        
            if ( isVowel(ch) ) {
                outputStrBuf.append( EGG );      //if vowel, append 
            }                                    //"egg" first
    
            outputStrBuf.append( ch );
        }
        
        eggyPeggyString = outputStrBuf.toString();
        
        //outputBox.printLine("Inside generateEggyPeggy");  //TEMP
    }


    /**
     * Read in the input string using InputBox.
     *
     */
    private void getInput( )
    {
        inputString = inputBox.getString("Enter the original sentence:");                                                            //TEMP
    }    
    
    
    /**
     * Returns true if the argument character is a vowel 
     * and false otherwise. 
     *
     * @return true if the parameter is a vowel
     */
    private boolean isVowel( char letter )
    {
        boolean result = false;
    
        if ( letter == 'a' || letter == 'A' ||
             letter == 'e' || letter == 'E' ||
             letter == 'i' || letter == 'I' ||
             letter == 'o' || letter == 'O' ||
             letter == 'u' || letter == 'U'   )     {
             
            result = true;
        }
    
        return result;
    }

}