/********************************************************
* Speaker Verification Implemented Security             *
* CA4 Project                                           *
* Written by: Ronan Crowley (97084603)                  *
*        and  Paul Connolly (97307599)                  *
********************************************************/

import Audio.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class NewUser extends JDialog {
   private JButton b[];
   public JTextField tf[];
   private String toSay[] = {       //Array containing everything to say in each training case
        "ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE",
        "DOE RAY ME FA SO LA TEE DOE",
        "ONE DOE THREE SIX FOUR FOUR ONE FIVE TWO",
        "FOUR TWO FOUR THREE SEVEN SIX ONE FIVE",
        "RAY ME TWO SIX DOE FA ME RAY SIX ONE",
        "TEE THREE SIX SO SEVEN NINE TWO FA",
        "SO FA FOUR SIX SEVEN TWO NINE ME SO",
        "DOE THREE FIVE ME DOE TEE SEVEN EIGHT",
        "NINE TWO SIX ONE EIGHT ME LA ME RAY FIVE TWO",
        "RAY DOE SEVEN FOUR THREE ME LA RAY FA",
        "FA SO LA TWO FOUR SEVEN ONE EIGHT NINE FIVE",
        "RAY FA SO TEE LA TEE ONE SIX EIGHT NINE THREE",
        "FIVE THREE EIGHT SEVEN LA ONE NINE RAY TEE SO",
        "TWO FOUR LA THREE FIVE FA SIX ME",
        "ME FA TWO DOE LA EIGHT SIX RAY SO",
        "RAY EIGHT DOE SEVEN SO LA ONE",
        "LA DOE THREE FIVE FA FOUR EIGHT SO"
    };
   private Container c;
   //Declare a sound handler.....
   private Audio.SoundBite s = null;
   private int numseconds = 10;     //Number of seconds to record for
   private GridLayout grid;
   private String username;
   Icon sv = new ImageIcon("sv.gif");

   /** Constructor
        @param User New User to be created
        @param parent Calling Frame
   **/
   public NewUser(String User, Frame parent) {
    super(parent);
    username = User;

    try {
      //Set configuration for recording @ 22050 kHz
      s = new Audio.SoundBite(false, false, Audio.SampleRate.R_22050, numseconds * Audio.SampleRate.R_22050 .toInt());
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    pack();
  }
     
   /** Component initialization **/
   private void jbInit() throws Exception
   {
      //super( "GridLayout Demo" );
      this.setTitle("Become a New User: "+username);

      grid = new GridLayout( 18, 2 );

      c = getContentPane();
      c.setLayout( grid );

      //Create and add buttons and Textfields.....
      b = new JButton[18];
      tf = new JTextField[18];

      for (int i = 0; i < 17; i++ ) {
         tf[i] = new JTextField ((i+1)+")  "+toSay[i]);
         tf[i].setEditable(false);
         tf[i].setBackground(Color.white);
         c.add( tf[i]);
         b[i] = new JButton ("Record Sample");
         b[i].setToolTipText("Record as many times as you need !");
         c.add( b[i] );
      }

      //Add listeners
      b[0].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record1_actionPerformed(e);
            }
      });
      b[1].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record2_actionPerformed(e);
            }
      });
      b[2].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record3_actionPerformed(e);
            }
      });
      b[3].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record4_actionPerformed(e);
            }
      });
      b[4].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record5_actionPerformed(e);
            }
      });
      b[5].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record6_actionPerformed(e);
            }
      });
      b[6].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record7_actionPerformed(e);
            }
      });
      b[7].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record8_actionPerformed(e);
            }
      });
      b[8].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record9_actionPerformed(e);
            }
      });
      b[9].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record10_actionPerformed(e);
            }
      });
      b[10].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record11_actionPerformed(e);
            }
      });
      b[11].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record12_actionPerformed(e);
            }
      });
      b[12].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record13_actionPerformed(e);
            }
      });
      b[13].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record14_actionPerformed(e);
            }
      });
      b[14].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record15_actionPerformed(e);
            }
      });
      b[15].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record16_actionPerformed(e);
            }
      });
      b[16].addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e) {
            Record17_actionPerformed(e);
            }
      });
      
      //Finished Button
      JButton fin = new JButton("Finished");
      c.add(fin);
      fin.setMnemonic('f');
      fin.addActionListener(new ActionListener()  {
        public void actionPerformed(ActionEvent e){
            fin_actionPerformed(e);
            }
      });

      setSize( 800, 600 );
      show();
   }

    //Record ......
    public void Record1_actionPerformed(ActionEvent e) {recordSample("S0001.wav"); b[0].setText("Recorded");}
    public void Record2_actionPerformed(ActionEvent e) {recordSample("S0002.wav"); b[1].setText("Recorded");}
    public void Record3_actionPerformed(ActionEvent e) {recordSample("S0003.wav"); b[2].setText("Recorded");}
    public void Record4_actionPerformed(ActionEvent e) {recordSample("S0004.wav"); b[3].setText("Recorded");}
    public void Record5_actionPerformed(ActionEvent e) {recordSample("S0005.wav"); b[4].setText("Recorded");}
    public void Record6_actionPerformed(ActionEvent e) {recordSample("S0006.wav"); b[5].setText("Recorded");}
    public void Record7_actionPerformed(ActionEvent e) {recordSample("S0007.wav"); b[6].setText("Recorded");}
    public void Record8_actionPerformed(ActionEvent e) {recordSample("S0008.wav"); b[7].setText("Recorded");}
    public void Record9_actionPerformed(ActionEvent e) {recordSample("S0009.wav"); b[8].setText("Recorded");}
    public void Record10_actionPerformed(ActionEvent e) {recordSample("S0010.wav"); b[9].setText("Recorded");}
    public void Record11_actionPerformed(ActionEvent e) {recordSample("S0011.wav"); b[10].setText("Recorded");}
    public void Record12_actionPerformed(ActionEvent e) {recordSample("S0012.wav"); b[11].setText("Recorded");}
    public void Record13_actionPerformed(ActionEvent e) {recordSample("S0013.wav"); b[12].setText("Recorded");}
    public void Record14_actionPerformed(ActionEvent e) {recordSample("S0014.wav"); b[13].setText("Recorded");}
    public void Record15_actionPerformed(ActionEvent e) {recordSample("S0015.wav"); b[14].setText("Recorded");}
    public void Record16_actionPerformed(ActionEvent e) {recordSample("S0016.wav"); b[15].setText("Recorded");}
    public void Record17_actionPerformed(ActionEvent e) {recordSample("S0017.wav"); b[16].setText("Recorded");}
   
    /** Finised Button **/
    public void fin_actionPerformed(ActionEvent e) {
        //Send files.......
        boolean allDone = true;
        
        //Ensure all files have been recorded before sent !
        for (int i=0; i<17; i++){
            if (b[i].getText().equals("Record Sample")){
                allDone = false;
            }
        }

        if (allDone){
            Sendfile[] transmit = new Sendfile[17];
            
            for (int i=0; i<17; i++){
                transmit[i] = new Sendfile();
                
                //Ensure correct file name being sent.
                String title;
                if (i<9){title = "S000"+(i+1)+".wav";}
                else {title = "S00"+(i+1)+".wav";}
                
                transmit[i].send("136.206.18.129", title);

                // Small pause for port to safely reset port (L114 hardware error)
                int j=0;
                while (j<10000){j++;}

                if (i==16)
                {
                    //Building Model Message
                    JOptionPane.showMessageDialog(this, 
                    "User Model is now being built.\n\nThis will take approx. 2 minutes.", 
                    "Building User Model", 1, sv);
                }
            }
        }
        s.dispose();
        dispose();
    }

    /** Method to record the sample specified
        @param name The WAV file to be created
    **/
    public void recordSample(String name) {
       try {
            System.out.println("\tStarted recording: "+name);

            {
                //From SoundBite class -> Start recording !
                s.recordStart();
                // s.recordWaitFor();
                // or
                s.recordWaitForGet();
            }
            
            //Save WAV !
            s.save(name);
            System.out.println("\tRecording done: "+name+" saved !");

        } catch (Audio.SoundBiteException aud) {
            aud.printStackTrace();
            System.err.println("Audio Exception: " + aud.getMessage());
        }
   }
}