import java.awt.*;
import javax.swing.*;

public class BoardPanel extends JPanel
{
    RobboFrame RF;
    private Dimension Dim;
    private Graphics Gr;
    private Image image;
    int CLICKED_x = 0;
    int CLICKED_y = 0;

    public BoardPanel (RobboFrame ROBBO_FRAME)
    {
	super ();
	RF = ROBBO_FRAME;
    }

    public Dimension getPreferredSize ()
    {
	return new Dimension (400, 400);
    }

    public Dimension getMinimumSize ()
    {
	return new Dimension (400, 400);
    }

    public void paintComponent (Graphics G)
    {
	Color LIGHT = Color.white;
	Color DARK = Color.cyan;
	Color GRAY = new Color (180, 180, 180);
	BoardPosition BP = RF.BOARD_POSITION;
	int SIZE = RF.SIZE;
	int OFF_SET = RF.SIZE;
	G.setColor (GRAY);
	G.fillRect(0, 0, 1000, 1000);
	for (int i = 0; i < 8; i++)
	    for (int j = 0; j < 8; j++)
		{
		    if ((i + j) % 2 == 0)
			G.setColor (LIGHT);
		    else
			G.setColor (DARK);
		    G.fillRect (SIZE * i + OFF_SET, SIZE * j + OFF_SET, SIZE, SIZE);
		}
	G.setColor (Color.black);
	G.drawLine (OFF_SET - 1, OFF_SET - 1, OFF_SET - 1, OFF_SET + SIZE * 8);
	G.drawLine (OFF_SET - 1, OFF_SET - 1, OFF_SET + SIZE * 8, OFF_SET - 1);
	G.drawLine (OFF_SET + SIZE * 8, OFF_SET + SIZE * 8,
		    OFF_SET + SIZE * 8, OFF_SET - 1);
	G.drawLine (OFF_SET + SIZE * 8, OFF_SET + SIZE * 8,
		    OFF_SET - 1, OFF_SET + SIZE * 8);
	if (BP.WTM)
	    {
		G.setColor (Color.white);
	    }
	else
	    {
		G.setColor (new Color (160, 160, 160));
	    }
	G.fillArc (3, 5 * SIZE + 3, SIZE - 7, SIZE - 7, 0, 360);
	if (!BP.WTM)
	    {
		G.setColor (Color.black);
	    }
	else
	    {
		G.setColor (new Color (160, 160, 160));
	    }
	G.fillArc (3, 4 * SIZE + 3, SIZE - 7, SIZE - 7, 0, 360);
	G.setColor (Color.yellow);
	G.fillArc (9 * SIZE + 3, 3, SIZE - 7, SIZE - 7, 0, 360);

	G.setColor (new Color (175, 75, 125));
	G.setFont (new Font ("Serif", Font.BOLD, 35));
	char buffer[] = new char[1];
	buffer[0] = 'M';
	G.drawChars (buffer, 0, 1, 9 * SIZE + 5, SIZE + 35);
	G.setColor (new Color (175, 85, 85));
	buffer[0] = 'I';
	G.drawChars (buffer, 0, 1, 9 * SIZE + 14, 2 * SIZE + 35);
	G.setColor (new Color (65, 105, 205));
	G.setFont (new Font ("SansSerif", Font.BOLD, 35));
	buffer[0] = 'C';
	G.drawChars (buffer, 0, 1, 8, 35);

	if (BP.EnPassant != 0)
	    {
		G.setColor (Color.orange);
		if (BP.WTM)
		    G.fillArc (BP.EnPassant * SIZE + 8, 3 * SIZE + 8, SIZE - 17, SIZE - 17, 0, 360);
		else
		    G.fillArc (BP.EnPassant * SIZE + 8, 6 * SIZE + 8, SIZE - 17, SIZE - 17, 0, 360);
	    }
	G.setColor (Color.orange);
	G.fillArc (8, 6 * SIZE + 8, SIZE - 17, SIZE - 17, 0, 360);
	String S = new String("");
	G.setFont (RF.chess_font);
	G.setColor (Color.black);
	for (int i = 0; i <= 9; i++)
	    for (int j = 0; j <= 9; j++)
		{
		    switch (BP.AT[i][j])
			{
			case 1: buffer[0] = 80 + 32; break;
			case 2: buffer[0] = 78 + 32; break;
			case 3: buffer[0] = 66 + 32; break;
			case 4: buffer[0] = 82 + 32; break;
			case 5: buffer[0] = 81 + 32; break;
			case 6: buffer[0] = 75 + 32; break;
			case -1: buffer[0] = 79 + 32; break;
			case -2: buffer[0] = 77 + 32; break;
			case -3: buffer[0] = 86 + 32; break;
			case -4: buffer[0] = 84 + 32; break;
			case -5: buffer[0] = 87 + 32; break;
			case -6: buffer[0] = 76 + 32; break;
			case 0: default: continue;
			}
		    int horz, vert;
		    if (!RF.REVERSE)
			{
			    horz = SIZE * i;
			    vert = SIZE * (9 - j);
			}
		    else
			{
			    horz = SIZE * (9 - i);
			    vert = SIZE * j;
			}
		    G.drawChars (buffer, 0, 1, horz, vert + OFF_SET);
		}
	if (CLICKED_x != 0 || CLICKED_y != 0)
	    {
		G.setColor (Color.red);
		int x = OFF_SET + SIZE * CLICKED_x - 1;
		int y = OFF_SET + SIZE * (9 - CLICKED_y) - 1;
		G.drawLine (x, y, x - SIZE + 1, y);
		G.drawLine (x, y, x, y - SIZE + 1);
		G.drawLine (x - SIZE + 1, y - SIZE + 1, x - SIZE + 1, y);
		G.drawLine (x - SIZE + 1, y - SIZE + 1, x, y - SIZE + 1);
	    }
	G.setColor (new Color (32, 32, 255));
	if (RF.MOVE_LIST.LENGTH > 0)
	    {
		G.drawLine (SIZE - 5, 9 * SIZE + SIZE / 2, 5, 9 * SIZE + SIZE / 2);
		G.drawLine (5, 9 * SIZE + SIZE / 2, SIZE / 2, 9 * SIZE + 7);
		G.drawLine (5, 9 * SIZE + SIZE / 2, SIZE / 2, 10 * SIZE - 7);
	    }
	if (RF.MOVE_LIST.LENGTH < RF.MOVE_LIST.MAX_MOVE)
	    {
		G.drawLine (10 * SIZE - 5, 9 * SIZE + SIZE / 2, 9 * SIZE + 5, 9 * SIZE + SIZE / 2);
		G.drawLine (10 * SIZE - 5, 9 * SIZE + SIZE / 2, 9 * SIZE + SIZE / 2, 9 * SIZE + 7);
		G.drawLine (10 * SIZE - 5, 9 * SIZE + SIZE / 2, 9 * SIZE + SIZE / 2, 10 * SIZE - 7);
	    }

    }
}


