#include <windows.h>
#include "sprite.h"
#include "../ErrorList/errorlist.h"

//-----------------------------------------------------------------------------
CTextSprite::CTextSprite()
{
	m_pRuWidget = NULL;
	m_pRuTexture = NULL;
	m_delta = 0.0f;
}

//-----------------------------------------------------------------------------
CTextSprite::~CTextSprite()
{	
	ruSAFE_RELEASE( m_pRuWidget );
	ruSAFE_RELEASE( m_pRuTexture );
}

//-----------------------------------------------------------------------------
void CTextSprite::Update( float elapsedTime )
{	
	SetPos( m_nowPos + (CRuVector3(0.0f, 0.15f, 0.0f) * (m_delta)) );
	m_delta -= elapsedTime;

	if ( m_pRuWidget )
	{
		m_pRuWidget->SetTranslation( m_nowPos );
		m_pRuWidget->Update( elapsedTime );
	}

	IBaseSprite::Update( elapsedTime );
}

//-----------------------------------------------------------------------------
void CTextSprite::Render()
{
	if ( m_pRuWidget )
		g_ruSceneRenderer->Scene_Submit( m_pRuWidget );
}

//-----------------------------------------------------------------------------
void CTextSprite::ReceiveDestruct( IBaseSprite* pSprite )
{
}

//-----------------------------------------------------------------------------
void CTextSprite::MotionEvent( int type )
{
}

//-----------------------------------------------------------------------------
void CTextSprite::SetText( TextType type, int value, float scale, int color )
{
	ruSAFE_RELEASE( m_pRuWidget );

	if ( m_pRuTexture == NULL )
		return;

	if ( (m_pRuWidget = ruNEW CRuEntity_Widget_Label()) != NULL )
	{
		char buf[64];
		vector<CRuRectangle> stock;
		CRuRectangle rect;

		m_pRuWidget->SetTexture( m_pRuTexture );
		switch ( type )
		{
		case eText_Number:
			sprintf( buf, "%d", value );
			for ( int i = 0; buf[i]; i++ )
			{
				switch ( buf[i] )
				{
				case '1':
					rect.Set( 0, 0, 64, 64 );
					break;

				case '2':
					rect.Set( 64, 0, 128, 64 );
					break;

				case '3':
					rect.Set( 128, 0, 192, 64 );
					break;

				case '4':
					rect.Set( 192, 0, 256, 64 );
					break;

				case '5':
					rect.Set( 256, 0, 320, 64 );
					break;

				case '6':
					rect.Set( 320, 0, 384, 64 );
					break;

				case '7':
					rect.Set( 384, 0, 448, 64 );
					break;

				case '8':
					rect.Set( 448, 0, 512, 64 );
					break;

				case '9':
					rect.Set( 0, 64, 64, 128 );
					break;

				case '0':
					rect.Set( 64, 64, 128, 128 );
					break;
				}
				stock.push_back( rect );
			}

			if ( !stock.empty() )
				m_pRuWidget->AddRowText( stock.size(), &stock[0], scale, color );
			break;

		case eText_Miss:
			rect.Set( 0, 128, 192, 192 );
			m_pRuWidget->AddRowText( 1, &rect, scale, color );
			break;

		case eText_Parry:
			rect.Set( 0, 192, 256, 256 );
			m_pRuWidget->AddRowText( 1, &rect, scale, color );
			break;

		case eText_Dodge:
			rect.Set( 0, 256, 256, 320 );
			m_pRuWidget->AddRowText( 1, &rect, scale, color );
			break;
		}
	}
}

//-----------------------------------------------------------------------------
bool CTextSprite::LoadTexture( const char* resourceName )
{
	ruSAFE_RELEASE( m_pRuTexture );
	if ( (m_pRuTexture = g_ruResourceManager->LoadTexture( resourceName )) != NULL )
		return true;
	return false;
}