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

import java.util.*;

/**
 * A view of a Can't Stop Game as a <CODE>JPanel</CODE>.
 *
 * @author Jim Glenn
 * @version 0.1 11/18/2008
 */

public class CantStopPanel extends JPanel implements Observer
{
    /**
     * The model of the game this panel draws.
     */

    private CantStopGame model;

    /**
     * Creates a panel to display the given game.
     */

    public CantStopPanel(CantStopGame g)
    {
	model = g;
	setBackground(Color.LIGHT_GRAY);
    }

    /**
     * Paints this panel.  The panel will be painted according to
     * the state of the model this panel is associated with.
     */

    public void paint(Graphics g)
    {
	g.clearRect(0, 0, getWidth(), getHeight());

	// compute the size of the board so we fill 80% of the smaller dimension

	int size = Math.min(getWidth(), getHeight()) * 4 / 5;

	// compute some important coordinates

	int sideLength = (int)(size / (1 + Math.sqrt(2)));
	int midX = getWidth() / 2;
	int midY = getHeight() / 2;
	int topY = midY - size / 2;
	int bottomY = topY + size - 1;
	int leftX = midX - size / 2;
	int rightX = leftX + size - 1;

	// draw the board with an outline (should see about drawing a thicker
	// outline)

	Polygon octagon = new Polygon();
	octagon.addPoint(midX - sideLength / 2, topY);
	octagon.addPoint(midX + sideLength / 2, topY);
	octagon.addPoint(rightX, midY - sideLength / 2);
	octagon.addPoint(rightX, midY + sideLength / 2);
	octagon.addPoint(midX + sideLength / 2, bottomY);
	octagon.addPoint(midX - sideLength / 2, bottomY);
	octagon.addPoint(leftX, midY + sideLength / 2);
	octagon.addPoint(leftX, midY - sideLength / 2);

	g.setColor(Color.RED.darker());
	g.fillPolygon(octagon);
	g.setColor(Color.RED);
	g.drawPolygon(octagon);

	// figure out size of the squares from the size of the board
	// (we're assuming that since the bounding box is a square everything
	// will work out so when we pick a size for the squares based
	// on the vertical size of a row that will work horizontally too

	int columnSpacing = (int)(size / 11.5); // left edge to left edge
	int rowSpacing = size / 16; // top to top
	int squareSize = rowSpacing * 4 / 5; // fill 80% of space for tow

	CantStopBoard board = model.getBoard();

	for (int c = 2; c <= 12; c++)
	    {
		// compute left edge of current column and top edge of
		// bottom row in that column

		int columnX = midX - squareSize / 2 - (7 - c) * columnSpacing;
		int columnY = midY - squareSize / 2 + ((board.getColumnLength(c) + 1) / 2 - 1) * rowSpacing;
	    
		// draw all the spaces in the current row

		for (int space = 1; space <= board.getColumnLength(c); space++)
		    {
			if (board.getColoredMarkerPosition(c) == space)
			    g.setColor(Color.GREEN);
			else if (board.getNeutralMarkerPosition(c) == space)
			    g.setColor(Color.WHITE);
			else
			    g.setColor(Color.GRAY);

			g.fillRect(columnX, columnY - (space - 1) * rowSpacing,
				   squareSize, squareSize);
		    }
		g.setColor(Color.WHITE);
		g.drawString("" + c, columnX, columnY + rowSpacing * 3 / 2);
	    }

	// draw the dice in the lower left corner

	g.setColor(Color.WHITE);
	g.fillRect(0, getHeight() - 30, 50, 25);
	g.setColor(Color.BLACK);
	g.setFont(new Font("SansSerif", Font.BOLD, 20));
	g.drawString(model.getDice(), 0, getHeight() - 5);

	// draw the turn in the lower right corner
	// (maybe use FontMetrics to get this exactly right)

	g.drawString("Turn " + model.countTurns(), getWidth() - 150, getHeight() - 5);
    }

    /**
     * Repaints this view.
     */

    public void update(Observable obs, Object o)
    {
	repaint();
    }
}

