#pragma once
#ifndef TETRISBOARD_H
#define TETRISBOARD_H

//Includes
#include "TetriminoManager.h"
#include <SDL_ttf.h>

//Forward declarations
class Tetrimino;
class SDL_Surface;


#define BOARD_WIDTH 10
#define BOARD_HEIGHT 20

//Length of time between piece "ticks"
#define TET_DROP_INTERVAL 1.0

class TetrisBoard
{
public:
	//Constructor
	TetrisBoard();

	//Destructor
	~TetrisBoard();

	//Frame update board elements
	void update(double elapsedTime);

	//Draw this board to the screen at the specified position
	void draw(SDL_Surface* surface, int posx, int posy);

	//Returns true if the specified piece, placed at the specified position,
	//collides with tiles already on the board
	bool checkCollision(const Tetrimino* tetrimino, int xpos, int ypos) const;

	//Move mobile block horizontally if its position is not valid
	void checkMobileHorizontal();

	//Merge the moving tetrimino piece into the static board grid
	void placeTetrimino(const Tetrimino* tetrimino, int xpos, int ypos);
	
	//Start a new game
	void reset();

	//Clear the board
	void clear();

	//Check for full rows and remove them
	void checkRemoveRow();

private:
	//Remove the specified row and shuffle down ones above it
	void removeRow(int row);

	//Retrieve a new tetrimino to drop
	void fetchNextMobile();

	//Finalize tetrimino drop, merging it into the grid and getting a new one
	void blockDropFinished();

	//Rerender score text image
	void renderScore();

	//The board state
	BlockColor board[BOARD_WIDTH][BOARD_HEIGHT];

	//The block currently being dropped
	Tetrimino* mobileBlock;
	//The position of the block
	int mobileBlockX;
	int mobileBlockY;

	//Player's score
	int score;

	//Are we in game over mode?
	bool gameOver;

	//Image for drawing grid lines
	SDL_Surface* grid;

	//Rendered score image
	SDL_Surface* scoreImage;

	//TTF Font
	TTF_Font* font;

	//Game over image
	SDL_Surface* gameoverText;
	
	//Seconds until next "tick"
	double timeToNextDrop;
};

#endif