package Lab5;

/*
 * *****************************************************************************
 * file name   : Lab5Template.java
 * author      : Philip Matuskiewicz (pjm35@buffalo.edu)
 * description : A complete implementation of Lab 4 to build on
 * 
 * This code is over-commented for your benefit only.  I would never
 * recommend using as many comments as this code contains in a
 * non-academic Java application.
 * *****************************************************************************
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

//uncomment the next import to get voice working (provided the buildpath is set up correctly)
//import com.sun.speech.freetts.*; // imports the Voice and VoiceManager classes

public class Lab5Template extends JFrame implements ActionListener{
	//where is the file that we should load?
	public String flocation = "D:/eclipseworkspace/Lab5/src/Lab5/phrases.txt";
	
	//DECLARATION OF JBUTTONS
	public String Characters[] = { 
			//ALPHABET
			"A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z", 
			
			//10 JBUTTONS WITH COMMON WORDS
			"HELLO", "GOODBYE", "I", "MY", "YOU", "HE", "SHE",
			"WHAT", "JUST", "FOOD", "DRINK", 
			
			//SPECIAL BUTTONS WITH SPECIAL FUNCTIONS
			"SPACE", "BACKSPACE", "CLEAR", "EXIT", "SPEAK", "REPEAT"};
	public JButton butts[] = new JButton[Characters.length];
	
	//DECLARATION OF COMMON PHRASES AS AN ARRAY	
	public String commonPhrases[] = 
			{
			//first element empty so first JComboBox selection can't
			//be the first item that is already selected
			""
			//,"I am Hungry", "I want to watch TV", "I am full", 
			//"I need to brush my teeth", "How is the weather today",
			//"I want to relax", "I want to sleep", "I am fine", 
			//"I don't feel well", "I feel cold"
			};
			
	
//Uncomment the next line to make the voice work	
//	public Voice helloVoice;
	
	public JTextField TextField = new JTextField(25);

	//last spoken phrase
	public String lastSpoke = "";
	
	public JComboBox comboBox = new JComboBox(commonPhrases);

	/*
	 * Make a new timer and set its delay to 20000 milliseconds (20 seconds)
	 * and set its 2nd parameter to an ActionListener (this class implements one) 
	 * */
	public Timer _timer = new Timer(20000, this);
	
	//constructor
	public Lab5Template() {

/*		
//  uncomment this entire block to make voice work
		//  The following block makes the voice work properly
		
		String voiceName = "kevin16";
		VoiceManager voiceManager = VoiceManager.getInstance();
		helloVoice = voiceManager.getVoice(voiceName);
		helloVoice.allocate();
*/	    
		
		//Set up the frame
		this.setSize(650, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		//Read in the Phrases to the comboBox
		try {
			String lineOfText = "";
			
			File f = new File (flocation);
			FileReader fileRead = new FileReader( f );
			BufferedReader textInput = new BufferedReader( fileRead );

			while (textInput.ready()) {
				lineOfText = textInput.readLine();
				comboBox.addItem(lineOfText);
		    }
			
			textInput.close( );
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		
		//add the TextBox / ComboBoxes first
		comboBox.addActionListener( this );
		this.add(comboBox);
		this.add(TextField);
		
		
		/**
		 * initialize every button in the JButton Array from Characters[]
		 * and then put an action listener on them 
		 * (pointing to the actionperformed method below in this class)
		 *
		 */
		for (int x = 0; x < butts.length; x++) {
			butts[x] = new JButton(Characters[x]);
			add(butts[x]);
			butts[x].addActionListener(this);
		}

		//make everything visible now that everything has been added
		//to this class (that extends JFrame)
		this.setVisible(true);
		
		//start the timer
		_timer.start();
		
	}

	/**
	 * This method is ran everytime an action is performed 
	 * on the application involving buttons or the JComboBox
	 * 
	 * e.getSource() is the Object
	 * e.getActionCommand() is the string command from the Object
	 * --likely the text that is on the JButton in this application
	 */
	public void actionPerformed(ActionEvent e) {
		
		//something happened to the JComboBox
		if (e.getSource() == comboBox){
			TextField.setText((String) comboBox.getSelectedItem());
		}
		
		//the timer ticked
		else if(e.getSource() == _timer){
			System.out.println("Time is up!  Bye!");
			System.exit(0);
		}
		
		//a JButton was pressed
		else{
			if (e.getActionCommand() == "SPACE"){
				TextField.setText(TextField.getText() + " ");
			}
			
			else if (e.getActionCommand() == "BACKSPACE"){
				try{
					TextField.setText(TextField.getText().substring(0, TextField.getText().length()-1));
				}catch (Exception except){
					//do nothing, but there was nothing to erase!
				}
			}
			
			else if (e.getActionCommand() == "CLEAR"){
				TextField.setText("");
			}
			
			else if (e.getActionCommand() == "REPEAT"){
//uncomment the following to get voice to work				
//				helloVoice.speak(lastSpoke);
				System.out.println("Speaking:" + lastSpoke);
			}
			
			else if (e.getActionCommand() == "SPEAK"){
//uncomment the following to get voice to work				
//				helloVoice.speak(TextField.getText());
				System.out.println("Speaking:" + TextField.getText());
				lastSpoke = TextField.getText();
				TextField.setText("");
			}
			
			else if (e.getActionCommand() == "EXIT"){
				System.out.println("Exiting...");
//Uncomment this following line to get voice to work
//				helloVoice.deallocate();
				System.exit(0);
			} 
			//catch the rest of the JButtons
			else {
				TextField.setText(TextField.getText() + e.getActionCommand());
			}
		}//end else for if source of action is comboBox
		
		
		//restart the timer because something happened
		_timer.restart();
	}

	/**
	 * Start the application
	 * */
	public static void main(String[] args) {
		new Lab5Template();
	}
	
} // end class

