import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

/*
 * Created on 2006/07/08
 */

public class Field {
	// tB[hÌTCYiPÊF}Xj
	public static final int COL = 12;
	public static final int ROW = 26;

	// }XÌTCY
	public static final int TILE_SIZE = 16;

	// tB[h
	private int[][] field;
	// tB[hÌC[W
	private int[][] fieldImage;

	public Field() {
		field = new int[ROW][COL];
		fieldImage = new int[ROW][COL];

		// tB[hðú»
		init();
	}

	/**
	 * tB[hðú»·é
	 */
	public void init() {
		for (int y = 0; y < ROW; y++) {
			for (int x = 0; x < COL; x++) {
				// ÇðÂ­é
				if (x == 0 || x == COL - 1) {
					field[y][x] = 1;
					fieldImage[y][x] = Block.WALL;
				} else if (y == ROW - 1) {
					field[y][x] = 1;
					fieldImage[y][x] = Block.WALL;
				} else {
					field[y][x] = 0;
				}
			}
		}
	}

	/**
	 * tB[hð`æ
	 */
	public void draw(Graphics g, Image blockImage) {
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, MainPanel.WIDTH, MainPanel.HEIGHT);

		g.setColor(Color.LIGHT_GRAY);
		for (int y = 0; y < ROW; y++) {
			for (int x = 0; x < COL; x++) {
				if (field[y][x] == 1) {
					g.drawImage(blockImage, x * TILE_SIZE, y * TILE_SIZE, x
							* TILE_SIZE + TILE_SIZE, y * TILE_SIZE + TILE_SIZE,
							fieldImage[y][x] * TILE_SIZE, 0, fieldImage[y][x]
									* TILE_SIZE + TILE_SIZE, TILE_SIZE, null);
				}
			}
		}
	}

	/**
	 * ubNðÚ®Å«é©²×é
	 * 
	 * @param newPos
	 *            ubNÌÚ®æÀW
	 * @param block
	 *            ubN
	 * @return Ú®Å«½çtrue
	 */
	public boolean isMovable(Point newPos, int[][] block) {
		// block=1Ì}X·×ÄÉÂ¢ÄÕËµÄ¢é©²×é
		// Çê©1}XÅàÕËµÄ½çÚ®Å«È¢
		for (int i = 0; i < Block.ROW; i++) {
			for (int j = 0; j < Block.COL; j++) {
				if (block[i][j] == 1) { // 4x4àÅubNÌ é}XÌÝ²×é
					if (newPos.y + i < 0) { // »Ì}XªæÊÌã[OÌÆ«
						// ubNÌ é}XªÇÌ é0ñÚÈºÜ½Í
						// COL-1ñÚÈãÉÚ®µæ¤ÆµÄ¢éêÍÚ®Å«È¢
						if (newPos.x + j <= 0 || newPos.x + j >= COL - 1) {
							return false;
						}
					} else if (field[newPos.y + i][newPos.x + j] == 1) { // tB[hàÅ
						// Ú®æÉ·ÅÉubNiÇÜÞjª éêÍÚ®Å«È¢
						return false;
					}
				}
			}
		}

		return true;
	}

	/**
	 * ¿«Á½ubNð{[hÉÅè·é
	 * 
	 * @param pos
	 *            ubNÌÊu
	 * @param block
	 *            ubN
	 */
	public void fixBlock(Point pos, int[][] block, int imageNo) {
		for (int i = 0; i < Block.ROW; i++) {
			for (int j = 0; j < Block.COL; j++) {
				if (block[i][j] == 1) {
                    if (pos.y + i < 0) continue;
					field[pos.y + i][pos.x + j] = 1; // tB[hðubNÅßé
					fieldImage[pos.y + i][pos.x + j] = imageNo;
				}
			}
		}
	}

	/**
	 * ubNª»ëÁ½sðÁ
	 */
	public void deleteLine() {
		for (int y = 0; y < ROW - 1; y++) {
			int count = 0;
			for (int x = 1; x < COL - 1; x++) {
				// ubNª éñÌð¦é
				if (field[y][x] == 1) {
					count++;
				}
			}
			// »ëÁ½sª©Â©Á½I
			if (count == Field.COL - 2) {
				// »ÌsðÁ
				for (int x = 1; x < COL - 1; x++) {
					field[y][x] = 0;
				}
				// »êæèãÌsðÆ·
				for (int ty = y; ty > 0; ty--) {
					for (int tx = 1; tx < COL - 1; tx++) {
						field[ty][tx] = field[ty - 1][tx];
						fieldImage[ty][tx] = fieldImage[ty - 1][tx];
					}
				}
			}
		}
	}
}
