import constants;
import java.lang.*;
import java.awt.*;
import java.awt.Toolkit;

public class piece extends constants{
	board theboard;
	Graphics g;
	int offsets[][];	
	int color;
	int type;
	int xpos , ypos;
	boolean nomove;
	boolean timetodie;
	boolean turndir;

	int score;

	piece(int type, board myboard, Graphics h){
		turndir = TURN_LEFT;
		timetodie = false;
		nomove = true;
		offsets = new int[4][2];
		theboard = myboard;
		g = h;
		if (type == LINE)
			newPiece(type,5,1);
		else
			newPiece(type,6,1);
	}

	void newPiece(int t, int x, int y){
		type = t;
		xpos = x;
		ypos = y;
		score = 24 + (2 * theboard.level);

		switch (type){

		case LINE:
			offsets[0][X]= -1;
			offsets[0][Y]= 0;
			offsets[1][X]= 0;
			offsets[1][Y]= 0;
			offsets[2][X]= 1;
			offsets[2][Y]= 0;
			offsets[3][X]= 2;
			offsets[3][Y]= 0;
			color = RED;
			turndir = TURN_RIGHT;
			break;
		case T:
			offsets[0][X]= -1;
			offsets[0][Y]= 0;
			offsets[1][X]= 0;
			offsets[1][Y]= 0;
			offsets[2][X]= 1;
			offsets[2][Y]= 0;
			offsets[3][X]= 0; 
			offsets[3][Y]= -1;
			color = YELLOW;
			break;
		case LEFT_S:
			offsets[0][X]= -1;
			offsets[0][Y]= -1;
			offsets[1][X]= 0;
			offsets[1][Y]= -1;
			offsets[2][X]= 0;
			offsets[2][Y]= 0;
			offsets[3][X]= 1;
			offsets[3][Y]= 0;
			color = BLUE;
			break;
		case RIGHT_S:
			offsets[0][X]= -1;
			offsets[0][Y]= 0;
			offsets[1][X]= 0;
			offsets[1][Y]= 0;
			offsets[2][X]= 0;
			offsets[2][Y]= -1;
			offsets[3][X]= 1;
			offsets[3][Y]= -1;
			color = GREEN;
			break;
		case LEFT_L:
			offsets[0][X]= -1;
			offsets[0][Y]= -1;
			offsets[1][X]= -1;
			offsets[1][Y]= 0;
			offsets[2][X]= 0;
			offsets[2][Y]= 0;
			offsets[3][X]= 1;
			offsets[3][Y]= 0;
			color = LTBLUE;
			break;
		case RIGHT_L:		
			offsets[0][X]= -1;
			offsets[0][Y]= 0;
			offsets[1][X]= 0;
			offsets[1][Y]= 0;
			offsets[2][X]= 1;
			offsets[2][Y]= 0;
			offsets[3][X]= 1;
			offsets[3][Y]= -1;
			color = PURPLE;
			break;
		case BOX:
			offsets[0][X]= -1;
			offsets[0][Y]= -1;
			offsets[1][X]= 0;
			offsets[1][Y]= -1;
			offsets[2][X]= 0;
			offsets[2][Y]= 0;
			offsets[3][X]= -1;
			offsets[3][Y]= 0;
			color = ORANGE;
			break;
		}
	}

	boolean rotate(){
		switch (type){
		case BOX:
			return false;
		case LINE:
			if (xpos <= 1 || xpos >= 10 || ypos == 0) return false;
		case LEFT_S:
		case RIGHT_S:
			turn(turndir);
			turndir = turndir ? false : true;
			break;
		case T:
		case LEFT_L:
		case RIGHT_L:
			turn(turndir);
			break;
		}

		return true;
	}

	public void turn(boolean dir){
		int oldoff[][] = new int [4][2];

		for (int i = 0; i< 4; i++) {
			oldoff[i][X] = offsets[i][X];
			oldoff[i][Y] = offsets[i][Y];
			if (dir){
				offsets[i][X] = oldoff[i][Y];
				offsets[i][Y] = -oldoff[i][X];
			}
			else{
				offsets[i][X] = -oldoff[i][Y];
				offsets[i][Y] = oldoff[i][X];
			}
		}

		if (!canMove(ROTATE)) {
			for (int i = 0; i < 4; i++) {
				offsets[i][X] = oldoff[i][X];
				offsets[i][Y] = oldoff[i][Y];
			}
		}		
	}

	void movePiece(int dir){
		
		int xoff = 0;
		int yoff = 0;

		switch (dir){
		case DROP:
		case MOVE_DOWN:
			xoff = 0;
			yoff = 1;
			break;
		case MOVE_LEFT:
			xoff = -1;
			yoff = 0;
			break;
		case MOVE_RIGHT:
			xoff = 1;
			yoff = 0;
			break;
		case ROTATE:
			rotate();
			break;
		}
		xpos += xoff;
		ypos += yoff;
	}

	void erasePiece(){
		switch (type){
		case LINE:
		case BOX:
			for (int i = 0; i < 4; i ++){
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].setBox(EMPTY);
  				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].paintBox(theboard.emboss,theboard.drawgrid,g);
			}
		case LEFT_S:
		case RIGHT_S:
		case LEFT_L:
		case RIGHT_L:
		case T:
			for (int i = 0; i < 4; i ++){
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].setBox(EMPTY);
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].paintBox(theboard.emboss,theboard.drawgrid,g);
			}
		}
		Toolkit t=java.awt.Toolkit.getDefaultToolkit();
		t.sync();
	}

	void drawPiece(){
		switch (type){
		case LINE:
		case BOX:
			for (int i = 0; i < 4; i ++){
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].setBox(color);
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].paintBox(theboard.emboss,theboard.drawgrid,g);
			}
		case LEFT_S:
		case RIGHT_S:
		case LEFT_L:
		case RIGHT_L:
		case T:
			for (int i = 0; i < 4; i ++){
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].setBox(color);
				theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].paintBox(theboard.emboss,theboard.drawgrid,g);
			}
		}
		Toolkit t=java.awt.Toolkit.getDefaultToolkit();
		t.sync();

	}

	boolean canMove(int dir){
		int xoff = 0; 
		int yoff = 0;

		boolean checkend = false;
		boolean move = true;

		switch (dir){
		 
		case MOVE_DOWN:
		case DROP:
			checkend = true;
			xoff = 0;
			yoff = 1;
			break;
		case MOVE_LEFT:
			xoff = -1;
			yoff = 0;
			break;
		case MOVE_RIGHT:
			xoff = 1;
			yoff = 0;
			break;
		case ROTATE:        //intentionally empty
			break;
		}

		for (int i = 0; i < 4; i++){
			if (theboard.grid[ypos+offsets[i][Y]+yoff][xpos+offsets[i][X]+xoff].solid){
				move = false;
			}
		}

		if (checkend && move){
			nomove = false;
		}

		if (!move && nomove && checkend){
			timetodie = true;
		}
		return move;
	}

	int move(int dir){
		int thescore = 0;

		if (canMove(dir)){
			erasePiece();
			movePiece(dir);
			drawPiece();
			if (dir == MOVE_DOWN){
				score --;
			}
			return 0;
		}

		else if (dir == MOVE_DOWN){
			drawPiece();
			makeSolid();
			thescore = score + theboard.deleteRows(this);
			return thescore;
		}
		else return 0;
	}

	int drop(){
		int thescore = 0;

		while (canMove(MOVE_DOWN)){
			erasePiece();
			movePiece(MOVE_DOWN);
			drawPiece();
		}

//		drawPiece();
		makeSolid();
		thescore = score + theboard.deleteRows(this);
		return thescore;
	}

	void makeSolid() {
		for (int i = 0; i < 4; i++) {
			theboard.grid[ypos+offsets[i][Y]][xpos+offsets[i][X]].solid = true;

		}
	}	

	void copyPiece(piece p){
		turndir = TURN_LEFT;
		type = p.type;
		nomove = true;
		xpos = 6;
		ypos = 1;

		if (type == LINE){
			turndir = TURN_RIGHT;
		}

		color = p.color;
		score = 24 + (2 * theboard.level);

		for (int i = 0; i < 4; i ++){
			offsets[i][X] = p.offsets[i][X];
			offsets[i][Y] = p.offsets[i][Y];
		}		
	}			
}