/* -*- c++ -*- */
import java.applet.*;
import java.lang.*;
import java.awt.*;
import java.net.*;

public class WebChess extends java.applet.Applet {
  Applet app;
  Image board;
  Image board2;
  int ew;
  int eh;
  int pw;
  int ph;
  String game;
  String layout;
  String move;
  String rows;
  String cols;
  int mark_x;
  int mark_y;
  boolean mark_it;
  Color mark_color;
  int jump_x;
  int jump_y;
  boolean jump_it;
  Color jump_color;
  boolean need_to_build_board;

  public void blit(Image i, Graphics g, int sx, int sy, int dx, int dy, int w, int h) {
    Graphics gc = g.create(dx, dy, w, h);
    gc.drawImage(i, -sx, -sy, this);
  }

  public void init() {
    board = getImage(getCodeBase(), getParameter("board"));
    ew = Integer.parseInt(getParameter("ew"));
    eh = Integer.parseInt(getParameter("eh"));
    pw = Integer.parseInt(getParameter("pw"));
    ph = Integer.parseInt(getParameter("ph"));
    game = getParameter("game");
    layout = getParameter("layout");
    rows = "87654321";
    cols = "abcdefgh";
    mark_it = false;
    mark_color = Color.blue;
    jump_it = false;
    jump_color = Color.red;

    move = "";
    mark_it = false;
    need_to_build_board = true;

    setBackground(black);

    prepareImage(board, this);

    getAppletContext().showStatus("Wait");
  }

  public void build_board() {
    board2 = createImage(ew+pw*8, eh+ph*8);
    Graphics g = board2.getGraphics();
    g.setColor(getBackground());
    g.fillRect(0,0,ew+pw*8, eh+ph*8);

    int i, x, y;

    blit(board, g, 0, 0, 0, 0, ew, 8*ph+eh);
    blit(board, g, ew, 8*ph, ew, 8*ph, 8*pw, eh);

    for (i=0; i<64; i++)
    {
      x = i % 8;
      y = i / 8;
      int sx=0, sy=0;
      char p = layout.charAt(i);
      if (p == 'R') {
	sx = 0; sy = 0; }
      if (p == 'N') {
	sx = 1; sy = 0; }
      if (p == 'B') {
	sx = 2; sy = 0; }
      if (p == 'Q') {
	sx = 3; sy = 0; }
      if (p == 'K') {
	sx = 4; sy = 0; }
      if (p == 'P') {
	sx = 5; sy = 0; }
      if (p == '.') {
	sx = 6; sy = 0; }
      if (p == 'r') {
	sx = 0; sy = 2; }
      if (p == 'n') {
	sx = 1; sy = 2; }
      if (p == 'b') {
	sx = 2; sy = 2; }
      if (p == 'q') {
	sx = 3; sy = 2; }
      if (p == 'k') {
	sx = 4; sy = 2; }
      if (p == 'p') {
	sx = 5; sy = 2; }
      if (((x+y) % 2) == 0) {
	sy ++; }
      blit(board, g, ew+sx*pw, sy*ph, ew+x*pw, y*ph, pw, ph);
    }
  }

  public void update(Graphics g) {
    paint(g);
  }

  public void paint(Graphics g) {
    if (need_to_build_board) {
      build_board();
      need_to_build_board = false;
    }
    g.drawImage(board2, 0, 0, this);
    if (mark_it) {
      g.setColor(mark_color);
      g.drawRect(mark_x, mark_y, pw-1, ph-1);
    }
    if (jump_it) {
      g.setColor(jump_color);
      g.drawRect(jump_x, jump_y, pw-1, ph-1);
      g.drawLine(jump_x, jump_y, jump_x+pw-1, jump_y+ph-1);
      g.drawLine(jump_x, jump_y+ph-1, jump_x+pw-1, jump_y);
    }
  }

  public boolean mouseUp(Event evt, int x, int y) {

    if (jump_it) {
      return true;
    }

    int c = (x-ew)/pw;
    int r = y/ph;

    if (x < ew || y > 8*ph) {
      getAppletContext().showStatus("");
      return true;
    }

    String cell = cols.substring(c,c+1) + rows.substring(r,r+1);

    if (move.length() == 0) {
      move = cell;
      mark_it = true;
      mark_x = ew + c * pw;
      mark_y = r * ph;
      repaint();
    } else if (move.equals(cell)) {
      move = "";
      mark_it = false;
      repaint();
    } else {
      move += cell;
      try {
	jump_it = true;
	jump_x = ew + c * pw;
	jump_y = r * ph;
	repaint();
	getAppletContext().showDocument(new URL(getCodeBase() + "move.cgi?game=" + game + "&move=" + move));
	move += "  Wait...";
      }
      catch (Exception e) {
      }
    }
    getAppletContext().showStatus(move);
    return true;
  }

  public boolean mouseMove(Event evt, int x, int y) {
    int c = (x-ew)/pw;
    int r = y/ph;

    if (jump_it) {
      getAppletContext().showStatus(move);
      return true;
    }

    if (x < ew || y > 8*ph) {
      getAppletContext().showStatus("");
      return true;
    }

    String cell = cols.substring(c,c+1) + rows.substring(r,r+1);
    String status;

    if (move.length() == 0) {
      status = "Move from " + cell;
    } else if (move.equals(cell)) {
      status = "Cancel Move";
    } else {
      status = "Move from " + move + " to " + cell;
    }
    getAppletContext().showStatus(status);
    return true;
  }

  public boolean imageUpdate(Image img, int info, int x, int y, int w, int h) {
    if ((info & ALLBITS) != 0) {
	need_to_build_board = true;
	repaint();
	return false;
      }
    return true;
  }
}