import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Random;


public class Speaker extends Thread {

	 public Speaker() {
	 }
	
	 // Overriding "start()"
	 public void start (){
	     System.out.println("Starting thread");
	     // Do whatever start does in Thread, don't forget this!
	     super.start();
	    }
	 
	 public void run() {
	 	MySQL msql;
		String pan = "";
		String gossip = "";
		String voice = "";
        int voice_choice = 0;
		
        //db variables
		String user     = "jennylc";
		String pass     = "pillsy88";
		String host     = "gossip.moustachio.com";
		String database = "robot_gossip";
			
	    // connect to database of server "localhost"
	    //
	    msql = new MySQL( host, database, user, pass, this );
	    
	    if ( msql.connect() )
	    {   
	    	 System.out.println("connect to database"); 

	    	 Random myEntry = new Random();
	         int rand_intro = myEntry.nextInt(2);
	         
	    	 
	         //SELECT * FROM my_table ORDER BY RAND() LIMIT 1
	    	 //
	         //msql.query( "SELECT * FROM `robotgossip` ORDER BY id DESC LIMIT 10" );
	         msql.query( "SELECT * FROM `robotgossip` ORDER BY RAND()  LIMIT 25" );
	         //msql.query( "SELECT * FROM `robotgossip` ORDER BY id DESC" );
	         //println( "number of rows: " + msql.getInt(1) );  

	         while (msql.next())
	         {
	           gossip = msql.getString("gossip"); 
	           
	          //voice_choice++;
	          //voice_choice = rand_intro;
	          rand_intro++;
	          
	          //if (voice_choice%2 == 1){
	          if (rand_intro%2 == 1){
	             //voice = "Cepstral Lawrence";
	             voice = "Bruce";
	             //voice = "Trinoids";
	             //voice = "Victoria"; 
	        	  pan = pan_Direction(1);
	          }
	          else{
	            //voice = "Cepstral Millie";
	            voice = "Vicki";
	            pan = pan_Direction(-1);
	          }
	        
	          String scripty =    "say \" "+ get_Intro() +"\" using \""+voice+"\" \n "; 
	          
	          run_applescript(pan + scripty);
	          
	          //System.out.println(gossip);  
	          
	          //String list1[] = gossip.split(".", 1);
	          gossip = gossip.replaceAll("[.]+", ",");
	          String list1[] = gossip.split(",");
	          
	        
	          

	          for (int i = 0; i < list1.length; i++) {
	        	  System.out.println(list1[i]);
	        	  System.out.println(i);

	        	  String script =    "say \" " +  list1[i] +"\" using \""+voice+"\" \n "; 
	        	  run_applescript(script);
	        
	          }//end for
	          
	           	           
	         }//end while there's entries left
 
	    }//end if connect
	    else
	    {
	        // connection failed !
	    }
	}//end run
	
	 
	private void run_applescript(String whole_script){

        try
			{
     	   Runtime runtime = Runtime.getRuntime();
     	   Process appProcess = runtime.exec("osascript");			

     	   OutputStreamWriter appWriter = new OutputStreamWriter(appProcess.getOutputStream());
			
     	   InputStream es = appProcess.getErrorStream();
     	   InputStream is = appProcess.getInputStream();

     	   appWriter.write(whole_script);
     	   appWriter.close();
			
     	   BufferedReader bres = new BufferedReader(new InputStreamReader(es));
     	   BufferedReader bris = new BufferedReader(new InputStreamReader(is));
			
     	   String bresout;
     	   while ((bresout = bres.readLine()) != null) 
     	   {	System.out.println(bresout);}
			
     	   String brisout;
     	   while ((brisout = bris.readLine()) != null)
     	   {	System.out.println(brisout);}
			}//end try
        
			catch (Exception e)
			{	
				e.printStackTrace();
			}//end catch
		
		
	} 
	
	private String pan_Direction(int pan){
		
		String pan_LorR = "tell application \"System Preferences\"\n " +
		"activate\n " +
		"set current pane to pane \"com.apple.preference.sound\"\n" +
		"reveal (first anchor of current pane whose name is \"output\")\n "+
		"end tell\n" +
		"tell application \"System Events\"\n " +
		"launch \n" +
		"tell process \"System Preferences\" to tell slider 1 of group "+ //don't break
		"1 of tab group 1 of window 1 to set value to "+ pan + " \n" +
		"end tell\n" +
		"quit application \"System Preferences\" \n";
			
		return pan_LorR;
	}
	
	
	private String get_Intro(){
		
		ArrayList intros = new ArrayList();
	     intros.add( "get this.");
	     intros.add( "wow, this is crazy.");
	     intros.add( "I can't believe this, did you hear about it?");
	     intros.add( "ooo here's a bit of juicy stuff ");
	     intros.add( "this certainly signifies the end of humanity ");
	     intros.add( "whoa, this is bananas");
	     intros.add( "celebrities are fucking crazy");
	     intros.add( "I'm a robot and this disgusts even me");
	     //intros.add( "someone needs to be locked up");
	     intros.add( "for the love of god ");
	
         Random myRandom = new Random();
         int rand_intro = myRandom.nextInt(intros.size());
         return (String)intros.get(rand_intro); 
        
	}
	 
}//end thread

