import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Date;

/** 
 * A panel to provide a simple language tutor.
 * @author matth3wbishop<at>yahoo!<dot>com
 */

public class TutorPanel extends Panel implements ActionListener
{
  //--------------------------------------------
  //-- private DownloadManager soundManager;
  //--------------------------------------------
  private DataFile dataFile;
  //--------------------------------------------
  private Button btnPlaySound;
  //--------------------------------------------
  private Button btnShowTranslation;
  //--------------------------------------------
  private Button btnShowDataFile;
  //--------------------------------------------
  private TextArea txtMessages;
  //--------------------------------------------
  private Label lblMessages;
  //--------------------------------------------
  //--------------------------------------------
  //--------------------------------------------
  private StringBuffer messages;
  //--------------------------------------------
  private Font displayFont;
  //--------------------------------------------
  private Date startTime;
  //--------------------------------------------
  private Date constructTime;
  //--------------------------------------------
  private Date stopTime;
  //--------------------------------------------
  private Date destroyDate;     
  //--------------------------------------------
  Color backgroundColour;

  //--------------------------------------------
  /** construct a new panel */
  public TutorPanel() 
  {
     super();

     this.dataFile = new DataFile();
     //-- this.soundManager = new DownloadManager();

     this.setBackground(new Color(0, 33, 0));

     this.btnPlaySound = new Button("play");
     this.btnPlaySound.addActionListener(this);
     
     this.btnShowTranslation = new Button("show trans");
     this.btnShowTranslation.addActionListener(this);

     this.btnShowDataFile = new Button("show data");
     this.btnShowDataFile.addActionListener(this);

     this.txtMessages = new TextArea("");
     this.txtMessages.setEditable(false);
     
     //this.commandField = new TextField();
     //this.commandField.setEditable(true);
     //this.add(commandField);
     //this.setLayout(new BorderLayout());

     this.add(this.btnPlaySound);
     this.add(this.btnShowTranslation);
     this.add(this.btnShowDataFile);
     this.add(this.txtMessages);
     
     //this.add(this.lblMessages);
  } //-- constructor


  public TutorPanel(String sDataFile) throws Exception
  {
     this();
     this.setDataFile(sDataFile);

  } //-- constructor (reader)

  //--------------------------------------------
  /** sets the datafile used by the language tutor */
  public void setDataFile(String sUrl) throws Exception
  {
    URL urlDataFile = new URL(sUrl);
    BufferedReader brDataFile = new BufferedReader(
      new InputStreamReader(urlDataFile.openStream()));
    this.dataFile = new DataFile(brDataFile);
  }

  //--------------------------------------------
  public void actionPerformed(ActionEvent e) 
  {
    Object source = e.getSource();
    if (source == this.btnShowTranslation)
    {  
      this.txtMessages.setText("show translation " + (new Date()));    
    }

    if (source == this.btnPlaySound)
    {  
      this.txtMessages.setText("play sound " + (new Date()));    
    }
    
    if (source == this.btnShowDataFile)
    {  
      StringBuffer sbMessage = new StringBuffer("");
      sbMessage.append("show data file " + (new Date()));
      sbMessage.append(this.dataFile.toString());
      sbMessage.append(this.dataFile.printReport());
      this.txtMessages.setText(sbMessage.toString());
      
    }
    //btnPlaySound.setLabel("Clicked");
  } //-- method: actionPerformed

  //--------------------------------------------
  /** A main method to do some testing */
  public static void main(String[] args) throws Exception
  {
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("usage: java TutorPanel dataFileUrl");
    //sbUsageMessage.append(NEWLINE); 

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }


    Frame frame = new Frame("Testing TutorPanel");
    TutorPanel panTest = new TutorPanel();
    panTest.setDataFile(args[0]);
    frame.add("Center", panTest);
    frame.pack();
    frame.show();
      
  } //-- main()    
} //-- class: TutorPanel

