// Auction Applet 

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

// parameters 
// id = user's email or other id
// port = auction socket
// bgcolor = background color (hex)

public class AuctionApplet extends Applet implements Runnable, ActionListener
  {
// User interface elements
  Label hb= new Label("High Bid: ");
  Label hbval = new Label("00.00");
  Label hbidder = new Label("by:");
  Label hbidderval = new Label("                            ");
  Label bidlbl = new Label("Your bid: $");
  TextField bid = new TextField(" 1.00");
  Button bidnow=new Button("Bid");
  Label tleft = new Label("Time Left:");
  Label tleftval = new Label("?????");

// Sockets and streams
  Socket sock;
  PrintWriter pw;
  BufferedReader br;
  String response;
 
  boolean auctionactive=true;
  
// helper: get parameter with default value
  private String getDefParam(String name,String def)
    {
    String rv=getParameter(name);
    if (rv==null) rv=def;
    return rv;
    }

// helper: get integer parameter (with default value)
  private int getIntParam(String name,int radix,int defv)
    {
    int rv=defv;
    String p=getParameter(name);
    if (p!=null)
	try {
	   rv=Integer.parseInt(p,radix);
	    }
 	   catch (Exception e)
	    {
	    }
     return rv;
     }

// parse status response
  private synchronized void status()
    {
     try {
         response=br.readLine();
	 }
     catch (Exception e) {  response=null; }
     if (response==null)
	{
 	tleftval.setText("Error");
	}
     else
	{
 	int seconds;
	String timer;
	float highbid;
	String highbidder;
	String sep;
	int n,n1;
	java.text.NumberFormat fmt=
          java.text.NumberFormat.getCurrencyInstance();
	n=response.indexOf(':');
	seconds=Integer.parseInt(response.substring(0,n));
	if (seconds==0) auctionactive=false;
	n1=response.indexOf(':',n+1);
	highbid=Float.valueOf(response.substring(n+1,n1)).floatValue();
	highbidder=response.substring(n1+1);
	sep=":";
	if (seconds%60<10) sep=sep+"0";
        timer = Integer.toString(seconds/60)
          +sep+Integer.toString(seconds%60);
	tleftval.setText(timer);
	hbval.setText(fmt.format(highbid));
	hbidderval.setText(highbidder);
	}
   }

// get started
  public void init()
   {
   setLayout(new FlowLayout(FlowLayout.LEFT));
   setBackground(new Color(getIntParam("bgcolor",16,0xFFFFFF)));
// add elements
   add(hb);
   add(hbval);
   add(hbidder);
   add(hbidderval);
   add(bidlbl);
   add(bid);
   add(bidnow);
   add(tleft);
   add(tleftval);
   bidnow.addActionListener(this);
   bid.setText(getDefParam("StartBid","1.00"));
// open up socket
   try 
     {
// Important: you must run this via
//   http://somehost to get the following to work
// if you are running from a file, you should
//   hardcode your machine name here!
     sock = new Socket(getDocumentBase().getHost(),
         getIntParam("port",10,0));
     pw=new PrintWriter(sock.getOutputStream());
     br=new BufferedReader(new    
         InputStreamReader(sock.getInputStream()));
     new Thread(this).start();  // start status update thread   
     }
   catch (Exception e)
     {
     tleftval.setText("Net Error");
     }
   }

// button pushed?
  public void actionPerformed(ActionEvent e)
   {
   if (auctionactive)
     {
// make bid!
     pw.print("B"+getParameter("ID")+":"+bid.getText()+"\n");
     status();
     }
   }

// thread to wait for 1 second and ask for status
  public void run()
   { 
   while (auctionactive) 
     {
     pw.print("S\n");
     pw.flush();
     status();
     try {
       Thread.sleep(1000);
       } catch (Exception e) { }
     }
  try
    {
    sock.close(); 
    }
  catch (Exception e) { } 
   }


 public void destroy()
  {
  try
    {
    sock.close(); 
    }
  catch (Exception e) { } 
  }
}

