import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.LinkedList;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * Implememts a simple applet for playing log files generated by
 * an Othello tournament.  For each pair of AIs, the applet will request the
 * log file from a websever at a predefined location.
 *
 * <p>The constant {@link #LOG_DIR} is used as the default location to look
 * for log files.  Log files must be named as "blackAI vs. whiteAI.log" (the
 * spaces will be escaped when the HTTP request is made).
 *
 * <p>Potential improvements:  Instead of {@link #LOG_DIR}, use an applet
 * parameter.  Also, error reporting could be more informative.
 *
 * <p>$Id: LogFilePlayerApplet.java,v 1.6 2004/03/17 08:30:36 emre Exp $
 *
 * @author Brian Emre Aydemir (emre@cs.caltech.edu)
 * @see LogFilePlayer
 **/
public class LogFilePlayerApplet extends JApplet
{
   /** The default base URL to look for log files under. **/
   public final static String LOG_DIR =
      "http://www.ugcs.caltech.edu//~cs2//log";

   /** The text field that contains the name of the black AI. **/
   protected JTextField black;

   /** The text field that contains the name of the white AI. **/
   protected JTextField white;

   /** The text area that displays status information. **/
   protected JTextField status;

   /** The base URL to use when looking for log files. **/
   protected String logDir = "http://www.ugcs.caltech.edu//~cs2//log";

   /////////////////////////////////////////////////////////////////////////
   // CONSTRUCTORS.

   /**
    * Constructs this applet.
    **/
   public LogFilePlayerApplet()
   {
      // Figure out where to look for log files.

      //logDir = LOG_DIR;

      // Construct the display components.

      getContentPane().setLayout(new BorderLayout());

      JPanel main = new JPanel(new GridLayout(2, 2));
      black = new JTextField(15);
      white = new JTextField(15);
      main.add(new JLabel("Black AI: "));
      main.add(black);
      main.add(new JLabel("White AI: "));
      main.add(white);
      getContentPane().add(main, BorderLayout.CENTER);

      JPanel info = new JPanel(new GridLayout(2, 1));
      JButton action = new JButton("Display the game");
      status = new JTextField(30);
      status.setEditable(false);
      status.setText("Awaiting orders..Is this one running");
      info.add(action);
      info.add(status);
      getContentPane().add(info, BorderLayout.PAGE_END);

      // Set what to do when the button is clicked.

      action.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e)
         {
            String blackAI = black.getText();
            String whiteAI = white.getText();
            LinkedList lines = new LinkedList();

            // Retrieve the log file.
	    String temp = null;
            try
            {
               URL url = new URL
		   //   (logDir + "/" + blackAI + " vs. " + whiteAI+ ".log");
	       (logDir + "/" + blackAI.replaceAll(" ", "%20") + "%20vs.%20" + whiteAI.replaceAll(" ", "%20") + ".log");
               BufferedReader reader =
                  new BufferedReader(new InputStreamReader(url.openStream()));
	       temp = "" + url;

               String line = reader.readLine();
               while (line != null)
               {
                  lines.addLast(line);
                  line = reader.readLine();
               }

               reader.close();
            }
            catch (IOException exn)
            {
               status.setText("I/O error.  Check your AI names? ");
               return;
            }

            // Play the log file now.

            JFrame frame = new JFrame(blackAI + " vs. " + whiteAI);
            LogFilePlayer display =
               new LogFilePlayer(lines, blackAI, whiteAI);
            frame.setContentPane(display);
            frame.setDefaultCloseOperation
               (WindowConstants.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.show();

            status.setText("Opening " + blackAI + " vs. " + whiteAI + ".");
         }
      } );
   }

}
