#include "Tile.h"

#include <iostream>

using std::cout;


	//********************************************************************************/
	//	Boardspace class method definitions
	//********************************************************************************/
	//********************************************************************************/


	//********************************************************************************/
	//	only constructor
	//-------------------------------------------------------------------------------
	Boardspace::Boardspace(QWidget* parent, const char* name, int WordValue, int LetterValue)
			  : Tile(' ', parent, name)
	{

		//	PointValue is the bonus point associated with the space

		Is_Empty = true;

		XWordBonus = WordValue;
		XLetterBonus = LetterValue;
	}

	//********************************************************************************/
	//	sets a boardspace to hold a tile
	//-------------------------------------------------------------------------------
	Boardspace& Boardspace::setTo(Tile* NewTile)
	{

		if ( NewTile->letterID() != Tile::letterID() )
		{
			qWarning("Tile is being set to new value\n");
			cout<< NewTile->letter();

			Tile::SetToDifferent( *NewTile );
		
			// if blank, set to empty tile
			if (NewTile->letterID() == ' ')
				Is_Empty = true;
			else Is_Empty = false;

			emit valueChanged( Tile::value()*XLetterBonus ) ;
			emit letterChanged( Tile::letter() );
			emit letterIDChanged( Tile::letterID() );
			emit isEmpty( Is_Empty );

		}

		return *this;

	}

	
	//********************************************************************************/
	//	sets a boardspace to hold a tile
	//-------------------------------------------------------------------------------
	Boardspace& Boardspace::setTo(Tile& NewTile)
	{


		if ( NewTile.letterID() != Tile::letterID())
		{
			qWarning("Tile is being set to new value ");
			cout<< NewTile.letter() << "\n";

			Tile::SetToDifferent( NewTile );
		
			// if blank, set to empty tile
			if (NewTile.letter() == ' ')
				Is_Empty = true;
			else Is_Empty = false;
			
			emit valueChanged( Tile::value()*XLetterBonus ) ;
			emit letterChanged( Tile::letter() );
			emit letterIDChanged( Tile::letterID() );
			emit isEmpty( Is_Empty );

		}
		return *this;
	}


	//********************************************************************************/
	//	clears tile off board, i.e., player put tile on board, but is not a word
	//-------------------------------------------------------------------------------
	void Boardspace::clear()
	{

		if (!Is_Empty)
			setTo( Tile(' ') );
	}

	//********************************************************************************/
	//	true if Boardspace does not have a valid tile
	//-------------------------------------------------------------------------------
	bool Boardspace::is_empty() const
	{
		return Is_Empty;
	}

	//********************************************************************************/
	// returns total points of this boardspace
	//-------------------------------------------------------------------------------
	int Boardspace::value() const
	{
		if (Is_Empty)
			return 0; 

		return XLetterBonus * Tile::value();
	}

	
	//********************************************************************************/
	// returns letter of tile of this boardspace
	//-------------------------------------------------------------------------------
	char Boardspace::letter() const
	{	
		if (Is_Empty)
			return ' '; 

		return  Tile::letter();

	}
	//********************************************************************************/
	// returns the unweighted points of this boardspace
	//-------------------------------------------------------------------------------
	int Boardspace::points() const
	{

		if (Is_Empty)
			return 0; 

		return Tile::value();

	}


	//********************************************************************************/
	//	returns the bonus letter multiplier
	//-------------------------------------------------------------------------------
	int Boardspace::letterBonus() const
	{
		return XLetterBonus;
	}

	//********************************************************************************/
	//	returns the bonus word multiplier for the space
	//-------------------------------------------------------------------------------
	int Boardspace::wordBonus() const
	{
		return XWordBonus;
	}

	//********************************************************************************/
	//	set a boardspace using a tile letterID
	//---------------------------------------------------------------------------------
	void Boardspace::setTo( int num)
	{
		setTo( Tile(num) );
	}

	//********************************************************************************/
	//	adjust font size to size of boardspace
	//---------------------------------------------------------------------------------
	void Boardspace::resizeEvent(QResizeEvent*e)
	{
		setFont(QFont("Times", size().width()*2/3, QFont::Bold));
	}
