import SliderBox;
import java.awt.*;

final class SliderTile 
{
	SliderBox puzzle;
	int left,top,right,bottom;

	SliderTile(SliderBox box, int l, int t, int r, int b) {
		puzzle = box; left = l; top = t; right = r; bottom = b;
	}

	void rescale(int oldx, int oldy, int newx, int newy) {
		left = left/oldx * newx;
		top = top/oldy * newy;
		right = right/oldx * newx;
		bottom = bottom/oldy * newy;
	}

	void paint(Graphics g) {
		int tilecolor, i;
		if (puzzle.game.winner) tilecolor = puzzle.randomColor();
		else tilecolor = puzzle.tilecolor;
		g.setColor(new Color(tilecolor));
		for (i = 0; i<puzzle.bevel; i++){
			g.draw3DRect(left+i,top+i,right-left-1-i-i,bottom-top-1-i-i,true);
		}
		g.fill3DRect(left+i,top+i,right-left-i-i,bottom-top-i-i,true);
	}

	boolean contains(int x, int y) {
		return ( x >= left && x < right &&
			 y >= top && y < bottom );
		}

	int moveleft(int dx, SliderTile[] tile) {
		if (left == 0) return 0;
		if (dx < -left) dx = -left;
		for (int i = 0; i < 10; i++) {
			if (left < tile[i].right) continue;
			if (top >= tile[i].bottom) continue;
			if (bottom <= tile[i].top) continue;
			int limit = tile[i].right - left;
			if (dx < limit) {
				dx = limit;
				if (dx == 0) return 0;
			}
		}
		left += dx;
		right += dx;
		return dx;
	}

	int moveright(int dx, int width, SliderTile[] tile) {
		int limit = width - right;
		if (limit == 0) return 0;
		if (limit < dx) dx = limit;
		for (int i = 0; i < 10; i++) {
			if (right > tile[i].left) continue;
			if (top >= tile[i].bottom) continue;
			if (bottom <= tile[i].top) continue;
			limit = tile[i].left - right;
			if (dx > limit) {
				dx = limit;
				if (dx == 0) return 0;
			}
		}
		left += dx;
		right += dx;
		return dx;
		}

	int moveup(int dy, SliderTile[] tile) {
		if (top == 0) return 0;
		if (dy < -top) dy = -top;
		for (int i = 0; i < 10; i++) {
			if (top < tile[i].bottom) continue;
			if (left >= tile[i].right) continue;
			if (right <= tile[i].left) continue;
			int limit = tile[i].bottom - top;
			if (dy < limit) {
				dy = limit;
				if (dy == 0) return 0;
			}
		}
		top += dy;
		bottom += dy;
		return dy;
		}

	int movedown(int dy, int height, SliderTile[] tile) {
		int limit = height - bottom;
		if (limit == 0) return 0;
		if (limit < dy) dy = limit;
		for (int i = 0; i < 10; i++) {
			if (bottom > tile[i].top) continue;
			if (left >= tile[i].right) continue;
			if (right <= tile[i].left) continue;
			limit = tile[i].top - bottom;
			if (dy > limit) {
				dy = limit;
				if (dy == 0) return 0;
			}
		}
		top += dy;
		bottom += dy;

		return dy;
		}

}


