#include "Tetris.h"

Tetris::Tetris(void)
{
	Generate_Grid();
	score = 0;
	line_count = 0;


	//Piece related vars

		dropingPeice = true;
		currentHeight = 0;
		drop_duration = 250.0f;	//in miliseconds
		delta_t = 0.1f;				//in miliseconds
		drop_counter = 0.0f;
	// setting up 

	int v1 = rand() % 2;  
	switch (v1)
	{
		case 1:
			current_piece = tet_piece::Block;
			printf("Case 1");
			break;
		case 2:
			printf("Case 2");
			current_piece = tet_piece::Line;
			break;
		default:
			printf("Default case");
			break;
	}
}

Tetris::~Tetris(void)
{
}

void Tetris::Generate_Grid(){

	for(int i=0;i<10;i++){
		for(int j=0;j<22;j++){
			Grid_index_filled[i][j] = false;
			Grid_Positions[i][j].x = i;
			Grid_Positions[i][j].y = j;
			Grid_Positions[i][j].z = 0.0f;
		}
	}

	Generate_Piece();
}

void Tetris::Generate_Piece(){

	currentHeight = 0;
	srand (time(NULL));
	int v1 = rand() % 5;  
	switch (v1)
	{
		case 1:
			current_piece = tet_piece::Block;

			current_block_info.x_1 = 4;	current_block_info.y_1 = 0;
			current_block_info.x_2 = 4; current_block_info.y_2 = 1;
			current_block_info.x_3 = 5; current_block_info.y_3 = 0;
			current_block_info.x_4 = 5; current_block_info.y_4 = 1;

			Grid_index_filled[4][0] = true;
			Grid_index_filled[4][1] = true;
			Grid_index_filled[5][0] = true;
			Grid_index_filled[5][1] = true;
			break;
		case 2:
			current_piece = tet_piece::Block;

			current_block_info.x_1 = 4;	current_block_info.y_1 = 0;
			current_block_info.x_2 = 4; current_block_info.y_2 = 1;
			current_block_info.x_3 = 5; current_block_info.y_3 = 0;
			current_block_info.x_4 = 5; current_block_info.y_4 = 1;

			Grid_index_filled[3][0] = true;
			Grid_index_filled[4][1] = true;
			Grid_index_filled[5][2] = true;
			Grid_index_filled[6][3] = true;
			break;
		case 3:
			current_piece = tet_piece::Block;

			current_block_info.x_1 = 4;	current_block_info.y_1 = 0;
			current_block_info.x_2 = 4; current_block_info.y_2 = 1;
			current_block_info.x_3 = 5; current_block_info.y_3 = 0;
			current_block_info.x_4 = 5; current_block_info.y_4 = 1;

			Grid_index_filled[1][0] = true;
			Grid_index_filled[2][1] = true;
			Grid_index_filled[3][0] = true;
			Grid_index_filled[4][1] = true;
			break;
		case 4:
			current_piece = tet_piece::Block;

			current_block_info.x_1 = 4;	current_block_info.y_1 = 0;
			current_block_info.x_2 = 4; current_block_info.y_2 = 1;
			current_block_info.x_3 = 5; current_block_info.y_3 = 0;
			current_block_info.x_4 = 5; current_block_info.y_4 = 1;

			Grid_index_filled[6][0] = true;
			Grid_index_filled[7][1] = true;
			Grid_index_filled[8][2] = true;
			Grid_index_filled[9][3] = true;
			break;

		default:
			

			current_block_info.x_1 = 4;	current_block_info.y_1 = 0;
			current_block_info.x_2 = 4; current_block_info.y_2 = 1;
			current_block_info.x_3 = 5; current_block_info.y_3 = 0;
			current_block_info.x_4 = 5; current_block_info.y_4 = 1;

			Grid_index_filled[3][0] = true;
			Grid_index_filled[4][0] = true;
			Grid_index_filled[5][0] = true;
			Grid_index_filled[6][0] = true;
			break;
	}
}

void Tetris::DrawGrid(){
	for(int i=0;i<10;i++){
		for(int j=0;j<22;j++){
			if(Grid_index_filled[i][j]){
				glPushMatrix();
					int v1 = rand() % 2;  
					int v2 = rand() % 2;  
					int v3 = rand() % 2;  
					glColor3f(v1,v2,3);
					glTranslatef(Grid_Positions[i][j].x,Grid_Positions[i][j].y,Grid_Positions[i][j].z);
					glutSolidCube(1.0f);
				glPopMatrix();
			}else if(!Grid_index_filled[i][j]){
				glPushMatrix();
					glColor3f(0,0,1);
					glTranslatef(Grid_Positions[i][j].x,Grid_Positions[i][j].y,Grid_Positions[i][j].z);
					glutWireCube(1.0f);
				glPopMatrix();
			}
		}
	}


}

void Tetris::UpdateGrid(){
	drop_counter += delta_t/drop_duration;
	//cout <<"counting: " << drop_counter << "\n";
	if(drop_counter >= 1.0f){
		drop_counter = 0.0f;
		//cout <<"reset: " << drop_counter << "\n";

		bool Grid_index_just_moved[10][22];
		for(int i=0;i<10;i++){
			for(int j=0;j<22;j++){
				Grid_index_just_moved[i][j] = false;
			}
		}

		for(int i=0;i<10;i++){
			for(int j=0;j<22;j++){
				if(Grid_index_filled[i][j] && j != 21 && !Grid_index_just_moved[i][j]){
					if(!Grid_index_filled[i][j+1]){
						Grid_index_filled[i][j+1] = true;
						Grid_index_filled[i][j] = false;
						Grid_index_just_moved[i][j+1] = true;
					}
				}
			}
		}

		currentHeight +=1;
		if(currentHeight >= 22){
			CheckForLine();
			Generate_Piece();
		}
	}

}

void Tetris::CheckForLine(){
	int block_count = 0;

	for(int j=0;j<10;j++){
		if(Grid_index_filled[j][21]){
			block_count +=1;
		}
	}
	if(block_count>=9){
		for(int j=0;j<10;j++){
			if(Grid_index_filled[j][21]){
				Grid_index_filled[j][21] = false;
			}
		}
		line_count +=1;
		cout<<"Current Line Count: " << line_count <<"\n";
	
	}
}

void Tetris::Update_timer(float temp_time){
	delta_t = temp_time;
}


void Tetris::MoveLeft(){
	for(int i=0;i<10;i++){
		for(int j=0;j<22;j++){
			if(Grid_index_filled[i][j] && i != 0){
				if(!Grid_index_filled[i-1][j]){
					Grid_index_filled[i-1][j] = true;
					Grid_index_filled[i][j] = false;
					//Grid_index_just_moved[i][j+1] = true;
				}
			}
		}
	}

}

void Tetris::MoveRight(){
	for(int i=9;i>=0;i--){
		for(int j=0;j<22;j++){
			if(Grid_index_filled[i][j] && i < 9){
				if(!Grid_index_filled[i+1][j]){
					Grid_index_filled[i+1][j] = true;
					Grid_index_filled[i][j] = false;
					//Grid_index_just_moved[i][j+1] = true;
				}
			}
		}
	}
}