/**
 * A Rhythm-constructing tool
 **/
import javax.swing.*; // Need this to reach Swing components
import java.awt.*; // Need this to reach FlowLayout
import java.awt.event.*; // Need this for listeners and events

public class RhythmTool extends JFrame {
  
  /* Base of sound that we're creating */
  public SoundElement root;
  /* Sound that we're creating to add in. */
  public SoundElement newSound;
  /* Declare these here so we can reach them inside listeners */
  private JTextField filename;
  private JTextField count;
  int num;
  
  public RhythmTool(){
    super("Rhythm Tool");
    FileChooser.setMediaPath("D:/cs1316/mediasources/"); //CHANGE ME!
    
    root = new SoundElement(new Sound(1)); // Nearly empty sound
    newSound = new SoundElement(new Sound(1)); // Ditto
    
    // Layout for the window overall
    this.getContentPane().setLayout(new BorderLayout());
    
    /* First panel has new sound field */
    JPanel panel1 = new JPanel();
    // Put panel one at the top
    this.getContentPane().add(panel1,BorderLayout.NORTH);
    // Create a space for entering a new sound filename
    filename = new JTextField("soundfilename.wav");
    filename.addActionListener(
      new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        /* When hit return in filename field,
         * create a new sound with that name.
         * Printing is for debugging purposes.
         **/
        newSound = new SoundElement(
                       new Sound(
                        FileChooser.getMediaPath(filename.getText())));
        System.out.println("New sound from "+
                           FileChooser.getMediaPath(filename.getText()));
      }
    }
    );
    panel1.add(filename);
    
    /* Put in another panel with number field 
     * and repeat & weave buttons */
    JPanel panel2 = new JPanel();
    // This layout is for the PANEL, not the WINDOW
    panel2.setLayout(new BorderLayout());
    // Add to MIDDLE of WINDOW
    this.getContentPane().add(panel2,BorderLayout.CENTER);
    // Add a field for arguments for Repeat and Weave
    count = new JTextField("10");
    num = 10; // Default value
    count.addActionListener(
      new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // Here's how we convert a string to a number
        num = Integer.parseInt(count.getText());
      }
    }
    );
    // Add to top of panel
    panel2.add(count,BorderLayout.NORTH);
    
    // Now do the Repeat button
    JButton button1 = new JButton("Repeat");
    button1.addActionListener(
      new ActionListener() { 
        public void actionPerformed(ActionEvent e) {
          // Repeat the number of times specified
           root.repeatNext(newSound,num);
        }
      }
    );
    // Add to RIGHT of PANEL
    panel2.add(button1,BorderLayout.EAST);
    
    // Now do the Weave button
    JButton button2 = new JButton("Weave");
    button2.addActionListener(
      new ActionListener() { 
        public void actionPerformed(ActionEvent e) {
          // We'll weave 10 copies in
          // every num times
           root.weave(newSound,10,num);
        }
      }
    );
    // Add to LEFT of PANEL
    panel2.add(button2,BorderLayout.WEST);
    
    /* Put in another panel with the Play button */
    JPanel panel3 = new JPanel();
    // Put in bottom of WINDOW
    this.getContentPane().add(panel3,BorderLayout.SOUTH);
    JButton button3 = new JButton("Play");
    button3.addActionListener(
      new ActionListener() { 
        // If this gets triggered, play the composed sound
        public void actionPerformed(ActionEvent e) {
           root.playFromMeOn();
        }
      }
    );
     panel3.add(button3); // No layout manager here

      this.pack();
    this.setVisible(true);
  }
  
}
