/*
 * Created on Dec 12, 2003
 * CS 108
 * Section A
 */
package finalproject;

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

/**
 * @author Eric Knibbe
 *
 * Implements the panel where the game's tiles are displayed.
 */
public class GamePanel extends JPanel {
	/**
	 * The scale factor for drawing tile grid lines and tile numbers.
	 */
	public final static int SCALE_FACTOR = 40;

	/**
	 * The Sliding Tile class.
	 */
	private SlidingTile mySlidingTile;

	/**
	 * Constructor for GamePanel. Sets sizes for the game panel according to the game 
	 * size chosen by the user.
	 * @param slidingTile
	 */
	public GamePanel(SlidingTile slidingTile) {
		mySlidingTile = slidingTile;
		setSizes(mySlidingTile.getSize() * SCALE_FACTOR + 1);
	}

	/**
	 * Sets the sizes for the game panel according to the game size chosen by the user.
	 * @param size
	 */
	public void setSizes(int size) {
		setSize(size, size);
		setMinimumSize(new Dimension(size, size));
		setPreferredSize(new Dimension(size, size));
		setMaximumSize(new Dimension(size, size));
	}

	/**
	 * Overridden paint method for the panel. Draws the grid lines and tile numbers.
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		drawGridLines(g);
		drawTiles(g);
	}

	/**
	 * Draws the tile numbers on the game panel using drawtile() at specified 
	 * row and column positions.
	 * @param g
	 */
	private void drawTiles(Graphics g) {
		int count = 0;
		for (int row = 0; row < mySlidingTile.getSize(); row++) {
			for (int col = 0; col < mySlidingTile.getSize(); col++) {
				drawTile(g, row, col, mySlidingTile.getMixedArrayItem(count));
				count++;
			}
		}
	}

	/**
	 * Draws tile numbers. If the tile number equals the game size squared (the blank tile), no number 
	 * is drawn.
	 * @param g
	 * @param row
	 * @param col
	 * @param tileValue
	 */
	private void drawTile(Graphics g, int row, int col, int tileValue) {
		g.setColor(Color.BLACK);
		String tileValueAsString;
		if (tileValue != (mySlidingTile.getSize() * mySlidingTile.getSize())) {
			tileValueAsString = " " + tileValue + " ";
			g.setColor(Color.BLACK);
			g.drawString(
				tileValueAsString,
				((col + 1) * SCALE_FACTOR
					- (SCALE_FACTOR / 2)
					- (SCALE_FACTOR / 4)),
				((row + 1) * SCALE_FACTOR
					- (SCALE_FACTOR / 2)
					+ (SCALE_FACTOR / 6)));
		} else {
//		g.setColor(Color.WHITE);
//		g.fillRect((row * SCALE_FACTOR + 1), (col * SCALE_FACTOR + 1), (SCALE_FACTOR - 2), (SCALE_FACTOR - 2));
			tileValueAsString = "   ";
			g.drawString(
				tileValueAsString,
				((col + 1) * SCALE_FACTOR
					- (SCALE_FACTOR / 2)
					- (SCALE_FACTOR / 4)),
				((row + 1) * SCALE_FACTOR
					- (SCALE_FACTOR / 2)
					+ (SCALE_FACTOR / 6)));
		}
	}

	/**
	 * Draws the grid lines on the game panel. Draws horizonal lines first, the vertical.
	 * @param g
	 */
	private void drawGridLines(Graphics g) {
		g.setColor(Color.BLUE);
		for (int row = 0; row <= mySlidingTile.getSize(); row++) {
			g.drawLine(
				0,
				row * SCALE_FACTOR,
				mySlidingTile.getSize() * SCALE_FACTOR,
				row * SCALE_FACTOR);
		}
		for (int col = 0; col <= mySlidingTile.getSize(); col++) {
			g.drawLine(
				col * SCALE_FACTOR,
				0,
				col * SCALE_FACTOR,
				mySlidingTile.getSize() * SCALE_FACTOR);
		}
	}
}
