#ifndef _TFIGURE2D_CPP
#define _TFIGURE2D_CPP
//-------------------------------------------------------
#include "TFigure2D.h"
//-------------------------------------------------------
TFigure2D::TFigure2D()
{
	std::vector<ITexture*> vTextures;
	TFigure2D(ESimpleFigures_Square, TRectF(0, 0, 25, 25), TColor4(0, 0, 0, 255), TColor4(124, 0, 0, 255), vTextures);
}
//-------------------------------------------------------
TFigure2D::TFigure2D(const ESimpleFigures &simpleFigure, const TRectF &drawingRect, const TColor4 &enabledColor, const TColor4 &disabledColor,
					 const std::vector<ITexture*> &vTextures)
{
	this->_vTextures = vTextures;
	_rotate = 0;
	_simpleFigure = simpleFigure;
	switch(_simpleFigure)
	{
	case ESimpleFigures_Square:
		_maxRotate = 0;
		_deltaWidth = 0;
		_width = _height = 2;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][0].SetTexture(_vTextures[0]);
			_vvRect[0][1].SetTexture(_vTextures[1]);
			_vvRect[1][0].SetTexture(_vTextures[2]);
			_vvRect[1][1].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterI:
		_maxRotate = 1;
		_deltaWidth = 3;
		_width = 4;
		_height = 4;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[0][0].SetExist(true);
		_vvRect[0][1].SetExist(true);
		_vvRect[0][2].SetExist(true);
		_vvRect[0][3].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][0].SetTexture(_vTextures[0]);
			_vvRect[0][1].SetTexture(_vTextures[1]);
			_vvRect[0][2].SetTexture(_vTextures[2]);
			_vvRect[0][3].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterS:
		_maxRotate = 1;
		_deltaWidth = 1;
		_width = 3;
		_height = 3;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[0][1].SetExist(true);
		_vvRect[0][2].SetExist(true);
		_vvRect[1][0].SetExist(true);
		_vvRect[1][1].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][1].SetTexture(_vTextures[0]);
			_vvRect[0][2].SetTexture(_vTextures[1]);
			_vvRect[1][0].SetTexture(_vTextures[2]);
			_vvRect[1][1].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterZ:
		_maxRotate = 1;
		_deltaWidth = 1;
		_width = 3;
		_height = 3;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[0][0].SetExist(true);
		_vvRect[0][1].SetExist(true);
		_vvRect[1][1].SetExist(true);
		_vvRect[1][2].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][0].SetTexture(_vTextures[0]);
			_vvRect[0][1].SetTexture(_vTextures[1]);
			_vvRect[1][1].SetTexture(_vTextures[2]);
			_vvRect[1][2].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterL:
		_maxRotate = 3;
		_deltaWidth = -1;
		_width = 3;
		_height = 3;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[1][0].SetExist(true);
		_vvRect[1][1].SetExist(true);
		_vvRect[1][2].SetExist(true);
		_vvRect[2][0].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[1][0].SetTexture(_vTextures[0]);
			_vvRect[1][1].SetTexture(_vTextures[1]);
			_vvRect[1][2].SetTexture(_vTextures[2]);
			_vvRect[2][0].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterJ:
		_maxRotate = 3;
		_deltaWidth = 1;
		_width = 3;
		_height = 3;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[0][0].SetExist(true);
		_vvRect[1][0].SetExist(true);
		_vvRect[1][1].SetExist(true);
		_vvRect[1][2].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][0].SetTexture(_vTextures[0]);
			_vvRect[1][0].SetTexture(_vTextures[1]);
			_vvRect[1][1].SetTexture(_vTextures[2]);
			_vvRect[1][2].SetTexture(_vTextures[3]);
		}
		break;
	case ESimpleFigures_LetterT:
		_maxRotate = 3;
		_deltaWidth = 1;
		_width = 3;
		_height = 3;
		_vvRect = GetRect(_width, _height);
		SetSettingsTovvRect(drawingRect, enabledColor, disabledColor, false);
		_vvRect[0][0].SetExist(true);
		_vvRect[0][1].SetExist(true);
		_vvRect[0][2].SetExist(true);
		_vvRect[1][1].SetExist(true);
		if(_vTextures.size() >= 4)
		{
			_vvRect[0][0].SetTexture(_vTextures[0]);
			_vvRect[0][1].SetTexture(_vTextures[1]);
			_vvRect[0][2].SetTexture(_vTextures[2]);
			_vvRect[1][1].SetTexture(_vTextures[3]);
		}
		break;
	}
}
//-------------------------------------------------------
TFigure2D::~TFigure2D()
{

}
//-------------------------------------------------------
void TFigure2D::SetSettingsTovvRect(const TRectF &drawingRect, const TColor4 &enabledColor, const TColor4 &disabledColor, const bool &exist)
{
	for(uint i = 0; i < _vvRect.size(); ++i)
		for(uint j = 0; j < _vvRect[i].size(); ++j)
		{
			_vvRect[i][j].SetRect(
									TRectF
										(
											drawingRect.x + i * drawingRect.width,
											drawingRect.y - (j + 1) * drawingRect.height,
											drawingRect.width,
											drawingRect.height
										)
									);
			_vvRect[i][j].SetEnabledColor(enabledColor);
			_vvRect[i][j].SetDisabledColor(disabledColor);
			_vvRect[i][j].SetExist(exist);
		}
}
//-------------------------------------------------------
void TFigure2D::Draw(IRender2D *pRender2D)
{
	for(uint i = 0; i < _vvRect.size(); ++i)
		for(uint j = 0; j < _vvRect[i].size(); ++j)
			if(_vvRect[i][j].GetExist())
				_vvRect[i][j].Draw(pRender2D);
}
//-------------------------------------------------------
void TFigure2D::SetPoint(const TPoint2 &point)
{
	for(uint i = 0; i < _vvRect.size(); ++i)
		for(uint j = 0; j < _vvRect[i].size(); ++j)
		{
			_vvRect[i][j].SetRect(
									TRectF
										(
											point.x + i * _vvRect[i][j].GetRect().width,
											point.y - (j + 1) * _vvRect[i][j].GetRect().height,
											_vvRect[i][j].GetRect().width,
											_vvRect[i][j].GetRect().height
										)
									);
		}
}
//-------------------------------------------------------
void TFigure2D::SetRect(const TRectF &rect)
{
	for(uint i = 0; i < _vvRect.size(); ++i)
		for(uint j = 0; j < _vvRect[i].size(); ++j)
		{
			_vvRect[i][j].SetRect(
									TRectF
										(
											rect.x + i * rect.width,
											rect.y - (j + 1) * rect.height,
											rect.width,
											rect.height
										)
									);
		}
}
//-------------------------------------------------------
std::vector< std::vector<TSquare> > TFigure2D::GetMaskRect() const
{
	return _vvRect;
}
//-------------------------------------------------------
uint TFigure2D::GetWidth() const
{
	return this->_width;
}
//-------------------------------------------------------
uint TFigure2D::GetHeight() const
{
	return this->_height;
}
//-------------------------------------------------------
int TFigure2D::GetDeltaWidth() const
{
	return this->_deltaWidth;
}
//-------------------------------------------------------
void TFigure2D::Rotate(bool Clockwise)
{
	if(Clockwise)
	{
		if(this->_rotate == this->_maxRotate)
			this->_rotate = 0;
		else
			++this->_rotate;
	}
	else
	{
		if(this->_rotate == 0)
			this->_rotate = this->_maxRotate;
		else
			--this->_rotate;
	}
	for(uint i = 0; i < _vvRect.size(); ++i)
		for(uint j = 0; j < _vvRect[i].size(); ++j)
			this->_vvRect[i][j].SetExist(false);
	switch(_simpleFigure)
	{
	case ESimpleFigures_Square:
		_vvRect[0][0].SetExist(true);
		_vvRect[0][1].SetExist(true);
		_vvRect[1][0].SetExist(true);
		_vvRect[1][1].SetExist(true);
		break;
	case ESimpleFigures_LetterL:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = -1;
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][2].SetExist(true);
			_vvRect[2][0].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[1][0].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[1]);
				_vvRect[1][2].SetTexture(_vTextures[2]);
				_vvRect[2][0].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][0].SetExist(true);
			_vvRect[0][1].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[2][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[3]);
				_vvRect[0][1].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[1]);
				_vvRect[2][1].SetTexture(_vTextures[2]);
			}
			break;
		case 2:
			_deltaWidth = 1;
			_vvRect[0][2].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][2].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][2].SetTexture(_vTextures[3]);
				_vvRect[1][0].SetTexture(_vTextures[2]);
				_vvRect[1][1].SetTexture(_vTextures[1]);
				_vvRect[1][2].SetTexture(_vTextures[0]);
			}
			break;
		case 3:
			_deltaWidth = 0;
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[2][0].SetExist(true);
			_vvRect[2][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[2]);
				_vvRect[1][0].SetTexture(_vTextures[1]);
				_vvRect[2][0].SetTexture(_vTextures[0]);
				_vvRect[2][1].SetTexture(_vTextures[3]);
			}
			break;
		}
		break;
	case ESimpleFigures_LetterI:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = 3;
			_vvRect[0][0].SetExist(true);
			_vvRect[0][1].SetExist(true);
			_vvRect[0][2].SetExist(true);
			_vvRect[0][3].SetExist(true);
			if(this->_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[0]);
				_vvRect[0][1].SetTexture(_vTextures[1]);
				_vvRect[0][2].SetTexture(_vTextures[2]);
				_vvRect[0][3].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[2][0].SetExist(true);
			_vvRect[3][0].SetExist(true);
			if(this->_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[0]);
				_vvRect[1][0].SetTexture(_vTextures[1]);
				_vvRect[2][0].SetTexture(_vTextures[2]);
				_vvRect[3][0].SetTexture(_vTextures[3]);
			}
			break;
		}
		break;
	case ESimpleFigures_LetterS:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = 1;
			_vvRect[0][1].SetExist(true);
			_vvRect[0][2].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][1].SetTexture(_vTextures[0]);
				_vvRect[0][2].SetTexture(_vTextures[1]);
				_vvRect[1][0].SetTexture(_vTextures[2]);
				_vvRect[1][1].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[2][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[2]);
				_vvRect[1][0].SetTexture(_vTextures[3]);
				_vvRect[1][1].SetTexture(_vTextures[0]);
				_vvRect[2][1].SetTexture(_vTextures[1]);
			}
			break;
		}
		break;
	case ESimpleFigures_LetterZ:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = 1;
			_vvRect[0][0].SetExist(true);
			_vvRect[0][1].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][2].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[0]);
				_vvRect[0][1].SetTexture(_vTextures[1]);
				_vvRect[1][1].SetTexture(_vTextures[2]);
				_vvRect[1][2].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][1].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[2][0].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][1].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[1]);
				_vvRect[1][0].SetTexture(_vTextures[2]);
				_vvRect[2][0].SetTexture(_vTextures[3]);
			}
			break;
		}
		break;
	case ESimpleFigures_LetterJ:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = 1;
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][2].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[0]);
				_vvRect[1][0].SetTexture(_vTextures[1]);
				_vvRect[1][1].SetTexture(_vTextures[2]);
				_vvRect[1][2].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][1].SetExist(true);
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[2][0].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][1].SetTexture(_vTextures[0]);
				_vvRect[0][0].SetTexture(_vTextures[1]);
				_vvRect[1][0].SetTexture(_vTextures[2]);
				_vvRect[2][0].SetTexture(_vTextures[3]);
			}
			break;
		case 2:
			_deltaWidth = -1;
			_vvRect[1][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[1][2].SetExist(true);
			_vvRect[2][2].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[1][0].SetTexture(_vTextures[3]);
				_vvRect[1][1].SetTexture(_vTextures[2]);
				_vvRect[1][2].SetTexture(_vTextures[1]);
				_vvRect[2][2].SetTexture(_vTextures[0]);
			}
			break;
		case 3:
			_deltaWidth = 0;
			_vvRect[0][1].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[2][1].SetExist(true);
			_vvRect[2][0].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][1].SetTexture(_vTextures[3]);
				_vvRect[1][1].SetTexture(_vTextures[2]);
				_vvRect[2][1].SetTexture(_vTextures[1]);
				_vvRect[2][0].SetTexture(_vTextures[0]);
			}
			break;
		}
		break;
	case ESimpleFigures_LetterT:
		switch(this->_rotate)
		{
		case 0:
			_deltaWidth = 1;
			_vvRect[0][0].SetExist(true);
			_vvRect[0][1].SetExist(true);
			_vvRect[0][2].SetExist(true);
			_vvRect[1][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[0]);
				_vvRect[0][1].SetTexture(_vTextures[1]);
				_vvRect[0][2].SetTexture(_vTextures[2]);
				_vvRect[1][1].SetTexture(_vTextures[3]);
			}
			break;
		case 1:
			_deltaWidth = 0;
			_vvRect[0][1].SetExist(true);
			_vvRect[1][1].SetExist(true);
			_vvRect[2][1].SetExist(true);
			_vvRect[1][0].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][1].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[1]);
				_vvRect[2][1].SetTexture(_vTextures[2]);
				_vvRect[1][0].SetTexture(_vTextures[3]);
			}
			break;
		case 2:
			_deltaWidth = -1;
			_vvRect[2][0].SetExist(true);
			_vvRect[2][1].SetExist(true);
			_vvRect[2][2].SetExist(true);
			_vvRect[1][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[2][0].SetTexture(_vTextures[2]);
				_vvRect[2][1].SetTexture(_vTextures[1]);
				_vvRect[2][2].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[3]);
			}
			break;
		case 3:
			_deltaWidth = 0;
			_vvRect[0][0].SetExist(true);
			_vvRect[1][0].SetExist(true);
			_vvRect[2][0].SetExist(true);
			_vvRect[1][1].SetExist(true);
			if(_vTextures.size() >= 4)
			{
				_vvRect[0][0].SetTexture(_vTextures[2]);
				_vvRect[1][0].SetTexture(_vTextures[1]);
				_vvRect[2][0].SetTexture(_vTextures[0]);
				_vvRect[1][1].SetTexture(_vTextures[3]);
			}
			break;
		}
		break;
	}
}
//-------------------------------------------------------
#endif