/** (C) 1999 World Xiangqi League, Confidential, All Rights Reserved */

import java.awt.*;
import java.util.*;

class Tables extends Dialog {
  private static Tables tables;
  private static Club club;
  private static GamePanel gamePanel;
  private List list;

  // Called once by Club.buildGUI().
  public static void initialize(Club c, GamePanel gp) {
    club = c;
    gamePanel = gp;
  }

  // User Club->Join Table else clicked Goto toolbar button.
  public static void dialog() {
    if (tables==null)
      tables = new Tables();	// create new dialog
    else
      tables.list.clear();	// handle same as Update button
    club.send("tables");
    club.state("Requesting table list.");
  }

  // Handle @ table info broadcast.
  public static void processTable(String info) {
    if ("@".equals(info)) {	// end of table list
      if (tables!=null)
	tables.setTitle(Club.TITLE);
      else
	club.state("OK.");
      return;
    }
    if ( info==null || info.length()<6 || (info.indexOf("* - *")>=0) )
      return;

    if (tables==null || tables.list==null)
      club.appendLine(info);
    else
      tables.list.addItem(info,0);
      //tables.list.layout();
      //tables.layout();
  }

  Tables() {
    super((Frame)club,"Wait...",/*modal=*/false);
    this.list = new List(15,false); // rows shown, !multiselect
    list.setForeground(Color.black);    
    list.setBackground(Color.white);
    
    FormLayout layout = new FormLayout(this,2,3);
    //         component,            pos, span,fill,stretch,anchor    
    layout.add(list,                 1,1, 2,1, 'B', 1.0,1.0, 'C');
    layout.add(new Button("Play"),   1,2, 1,1, 'H', 0.5,0.0, 'C');
    layout.add(new Button("Observe"),2,2, 1,1, 'H', 0.5,0.0, 'C');
    layout.add(new Button("Close"),  1,3, 1,1, 'H', 0.5,0.0, 'C');
    layout.add(new Button("Update"), 2,3, 1,1, 'H', 0.5,0.0, 'C');
    open();
  }

  public void open() {		// Cf ./Stickup.java
    pack();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    move( (d.width-size().width)/2, (d.height-size().height)/2 );
    resize(300,size().height);
				     
    Window w = this;		// show() not implemented for N3 Dialog
    //w.pack();			// hack attempt fix for 640x480? layout bug
    w.show();			// thus use Window show method
				// else create dummy Frame to be Dialog parent
    repaint();
  }

  public boolean handleEvent(Event event) {
    if (event.id==Event.WINDOW_DESTROY || event.arg=="Close") {
      byebye();
      return true;
    }
    if (event.id!=Event.ACTION_EVENT)
      return super.handleEvent(event);
    if (event.arg=="Play") {
      joinTable("join");
      return true;
    }
    if (event.arg=="Observe") {
      joinTable("observe");
      return true;
    }
    if (event.arg=="Update") {
      list.clear();
      tables.setTitle("Wait...");
      club.send("tables");
      club.state("Requesting table list.");
      return true;
    }
    return super.handleEvent(event);
  }

  private void joinTable(String command) {
    String label = list.getSelectedItem();
    if (label==null) {
      club.state("Select table then click Play or Observe.");
      return;
    }
    if (!gamePanel.canLeave())
      return;
    // label: 1 paul karl
    command += " " + label.substring(0,label.indexOf(' '));
    club.state("Request "+command);
    if (command.equals("observe"))
      gamePanel.hide();		// until setup received
    club.send(command);
    byebye();
  }

  private void byebye() {
    hide();    // close the window
    dispose(); // destroy its resources
    removeTable();
  }
  private static void removeTable() {
    tables = null;
  }
}
