//package codeapplet;

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.HeadlessException;
import javax.swing.JOptionPane;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Curt Hill
 * @version 1.1
 */

public class MOOApplet extends Applet {
  final boolean debug = false;
  final String home = "http://euler.vcsu.edu:7000/";

  public MOOApplet() throws HeadlessException {
  }

  public boolean PhoneHome(){
    // Get parameters
    if(debug)
      System.out.println("MOOApplet is attempting to phone home");
    String object;
    object = getParameter("objectnum");
    if(object==null){
      System.out.println("No object number specified.");
      return false;
      }
    String command;
    command = getParameter("command");
    if(command==null){
      System.out.println("No command given.");
      return false;
      }
    String player;
    player = getParameter("player");
    if(player==null){
      System.out.println("No player given.");
      return false;
      }
   String myurl = home+object+"/"+command+"?"+player;
   if(debug)
     System.out.println("Attempting at URL: "+myurl);
   HttpURLConnection moo;
   try {
      URL url = new URL(myurl);
      moo = (HttpURLConnection)url.openConnection();
      moo.setRequestMethod("GET");
      int rc = moo.getResponseCode();
      if(debug){
          System.out.println("Connection request code: " + rc +
                             ", text for this is: " + moo.getResponseMessage());
          System.out.println(moo.getURL());
          } // debug
      moo.disconnect();  
      }
    catch(MalformedURLException e){
        System.out.println("Bad URL");
        JOptionPane.showMessageDialog(this, "This applet could not connect to the MOO with your results - notify instructor. URL: "+myurl,
                                      "Error - Bad URL",
                                      JOptionPane.ERROR_MESSAGE);
        return false;
       }
    catch(SocketException e){
           // This is actually expected
           if(debug)
             System.out.println("Socket Exception caught");
           else
             System.out.println("Expected exception handled.");  
           }
    catch(IOException e){
      System.out.println("Some other I/O problem: "+e.getMessage());
      System.out.println(" The exception class is "+e.getClass().getName());
      JOptionPane.showMessageDialog(this, "This applet could not connect to the MOO with your results - notify instructor. Message: "+e.getMessage(),
                                    "Error - IOException",
                                    JOptionPane.ERROR_MESSAGE);
      return false; 
       }
    catch(Exception e){
      System.out.println("Some other problem: "+e.getMessage());
      System.out.println(" The exception class is "+e.getClass().getName());
      JOptionPane.showMessageDialog(this, "This applet could not connect to the MOO with your results - notify instructor. Message: "+e.getMessage(),
                                    "Error - Exception",
                                    JOptionPane.ERROR_MESSAGE);
      return false;
      }
  System.out.println("Success has been recorded");
  return true;
  }

  public boolean PhoneHome(String command, String parms){
    // Get parameters
    if(debug)
      System.out.println("MOOApplet is attempting parameterized phone home "+command+" ; "+parms);
    String object;
    object = getParameter("objectnum");
    if(object==null){
      System.out.println("No object number specified.");
      return false;
      }
    String player;
    player = getParameter("player");
    if(player==null){
      System.out.println("No player given.");
      return false;
      }
   String myurl = home+object+"/"+command+"?"+player+parms;
   if(debug)
     System.out.println("Attempting at URL: "+myurl);
   HttpURLConnection moo;
   try {
      URL url = new URL(myurl);
      moo = (HttpURLConnection)url.openConnection();
      moo.setRequestMethod("GET");
      int rc = moo.getResponseCode(); 
      if(debug){
        System.out.println("Connection request code: "+rc+", text for this is: "+moo.getResponseMessage());
        System.out.println(moo.getURL());
        }
      }
    catch(MalformedURLException e){
        System.out.println("Bad URL");
        JOptionPane.showMessageDialog(this, "This applet could not connect to the MOO with your results - notify instructor. URL: "+myurl,
                                      "Error - Bad URL",
                                      JOptionPane.ERROR_MESSAGE);
        return false;
       }
    catch(EOFException e){
      // This is actually expected
      if(debug)
        System.out.println("EOF caught");
      }
    catch(SocketException e){
        // This is actually expected
        if(debug)
          System.out.println("Socket Exception caught");
        }
    catch(IOException e){
      // Find the actual exception
      System.out.println("Some other I/O problem. Message: "+e.getMessage());
      System.out.println(" The exception class is "+e.getClass().getName());
      JOptionPane.showMessageDialog(this, "This applet could not connect to the MOO with your results - notify instructor. Message: "+e.getMessage(),
                                    "Error - IOException",
                                    JOptionPane.ERROR_MESSAGE);
      return false;
     }
  return true;
  }


}
