#include "StdAfx.h"
#include "HeroStateWindow.h"

#include "FontAgent.h"

#include "Label.h"
#include "Gauge.h"
#include "UIImage.h"
#include "ArcGauge.h"

#include "ObjectManager.h"
#include "Hero.h"

#include "LevelScript.h"

cHeroStateWindow::cHeroStateWindow()
: mpName(0)
, mpHPGauge(0)
, mpMPGauge(0)
, mpSXPGauge(0)
, mpEXPGauge(0)
, mpFace(0)
, mpLevelImage(0)
, mpLevelSkin(0)
{
}

cHeroStateWindow::~cHeroStateWindow()
{
	SAFE_DELETE( mpLevelImage );
}

void cHeroStateWindow::UIStageIn()
{
	if( !mpName || !HERO )
	{
		assert(0);
		return;
	}

	/// 이름세팅
	LPTSTR str = HERO->GetName();
	if( mpName )
		mpName->SetText(str);
	
	/// 얼굴 이미지 설정
	cUISkin* pSkin = UIMAN->GetSkin();
	if( !pSkin )
	{
		assert(0);
		return;
	}

	cString skinName;
	unsigned int imgIndex = GetImageIndex( HERO->GetRace(), HERO->GetGender() );
	switch( imgIndex )
	{
	case 0:		skinName = "Face_Human_M";		break;
	case 1:		skinName = "Face_Beast_M";		break;
	case 2:		skinName = "Face_Elf_M";		break;
	case 3:		skinName = "Face_Human_W";		break;
	case 4:		skinName = "Face_Beast_W";		break;
	case 5:		skinName = "Face_Elf_W";		break;
	}

	/// 
	mpFaceSkin = pSkin->GetNodeSkin( skinName.Cstr() );
	if( mpFaceSkin && mpFace )
	{
		unsigned int tx = mpFaceSkin->mSkinInfo->mTexX;
		unsigned int ty = mpFaceSkin->mSkinInfo->mTexY;
		unsigned int w = mpFaceSkin->mSkinInfo->mWidth;
		unsigned int h = mpFaceSkin->mSkinInfo->mHeight;
		mpFace->ChangeImage( tx, ty );
	}

	/// 윈도우 초기화
	UpdateHeroInfo();
	UpdateExpSxp();
}

void cHeroStateWindow::UpdateExpSxp()
{
	if( HERO == 0 )
	{
		assert(0);
		return;
	}

	/// 캐릭터 경험치
	sExpTable*	pExp = LEVELSCRIPT->GetExpTable( HERO->GetLevel() );
	if( pExp )
	{
		unsigned int exp = HERO->GetHeroInfo()->Exp;

		SetMaxEXPGauge( (float)pExp->mExp );
		SetEXPGauge( static_cast<float>(exp) );
	}
	else
	{
		assert(0);
		return;
	}

	///
	sSxpTable*	pSxp = LEVELSCRIPT->GetSxpTable( HERO->GetSkillLevel() );
	if( pSxp )
	{
		unsigned int sxp = HERO->GetHeroInfo()->SkillExp;

		SetMAXSXP( (float)pSxp->mSxp );
		SetSXP( static_cast<float>(sxp) );
	}
	else
	{
		assert(0);
		return;
	}

}

void cHeroStateWindow::UpdateHeroInfo()
{
	if( HERO == 0 )
	{
		assert(0);
		return;
	}

	/// 레벨 세팅
	SetLevel( HERO->GetLevel() );

	/// hp, mp갱신
	UpdateHeroHPMP();
}

void cHeroStateWindow::UpdateHeroHPMP()
{
	if( HERO == 0 )
	{
		assert(0);
		return;
	}

	/// hp, mp갱신
	unsigned long maxHP = HERO->GetMaxHP();
	unsigned long curHP = HERO->GetHP();
	if( mpHPGauge && mpHPGauge->GetMaxValue() != maxHP )
		mpHPGauge->SetMaxValue( (float)maxHP );

	if( mpHPGauge && mpHPGauge->GetValue() != curHP )
		mpHPGauge->SetGaugeNoAni( (float)curHP );

	unsigned long maxMP = HERO->GetMaxMP();
	unsigned long curMP = HERO->GetMP();
	if( mpMPGauge && mpMPGauge->GetMaxValue() != maxMP )
		mpMPGauge->SetMaxValue( (float)maxMP );

	if( mpMPGauge && mpMPGauge->GetValue() != curMP )
		mpMPGauge->SetGaugeNoAni( (float)curMP );
}

char cHeroStateWindow::GetImageIndex( char race, char gender )
{
	/// 남자면 그대로 리턴
	/// 성별이 여자면,
	char result = race;
	if( gender == eGENDER_FEMALE )
	{
		if( race == eRACE_HUMAN ) result = 3;
		if( race == eRACE_BEAST ) result = 4;
		if( race == eRACE_ELF ) result = 5;
	}

	return result;
}

void cHeroStateWindow::CreateFace()
{
	/// 얼굴 이미지 설정
	cUISkin* pSkin = UIMAN->GetSkin();
	if( !pSkin )
	{
		assert(0);
		return;
	}

	/// 디폴트 설정
	mpFaceSkin = pSkin->GetNodeSkin( "Face_Human_M" );
	if( mpFace )
	{
		unsigned int tx = mpFaceSkin->mSkinInfo->mTexX;
		unsigned int ty = mpFaceSkin->mSkinInfo->mTexY;
		mpFace->ChangeImage( tx, ty );
	}
}


void cHeroStateWindow::CreateLevelImage()
{
	/// 레벨 배경 이미지 설정
	cUISkin* pSkin = UIMAN->GetSkin();
	if( !pSkin )
	{
		assert(0);
		return;
	}

	/// 디폴트 설정
	mpLevelSkin = pSkin->GetNodeSkin( "HeroState_LevelImg" );
	mpLevelImage = new cUIImage( mpLevelSkin->mpTexture );
	if( mpLevelImage )
	{
		unsigned int tx = mpLevelSkin->mSkinInfo->mTexX;
		unsigned int ty = mpLevelSkin->mSkinInfo->mTexY;
		unsigned int x = mpLevelSkin->mSkinInfo->mX + GetAbsoluteRect().mLeft;
		unsigned int y = mpLevelSkin->mSkinInfo->mY + GetAbsoluteRect().mTop;
		unsigned int w = mpLevelSkin->mSkinInfo->mWidth;
		unsigned int h = mpLevelSkin->mSkinInfo->mHeight;
		mpLevelImage->SetTextureRect( tx, ty, tx + w, ty + h );
		mpLevelImage->SetScreenRect( cUIRect( x, y, x + w, y + h ) );
	}
}

/// 주의사항 : 
bool cHeroStateWindow::OnCreate( cUINodeProperty* pproperty )
{
	if( cUIWindow::OnCreate( pproperty ) == false )
		return false;

	mpFace = (cLabel*)GetChild( eUIID_GAME_HERO_FACE );
	if( mpFace )
		mpFace->SetExceptEvent( true );

	mpName = (cLabel*)GetChild( eUIID_GAME_HERO_NAME_LABEL );
	mpHPGauge = (cDoubleGauge*)GetChild( eUIID_GAME_HERO_HP_GAUGE_BAR );
	mpMPGauge = (cDoubleGauge*)GetChild( eUIID_GAME_HERO_MP_GAUGE_BAR );
	mpSXPGauge = (cGauge*)GetChild( eUIID_GAME_HERO_SXP_GAUGE_BAR );
	mpEXPGauge = (cGauge*)GetChild( eUIID_GAME_HERO_EXP_GAUGE_BAR );

	/// 캐릭터 얼굴이미지 생성
	CreateFace();

	/// 레벨 배경 이미지
	CreateLevelImage();
	return true;
}

/// 주의사항 : 
bool cHeroStateWindow::HandleEvent( const cUIEvent& event )
{
	if( mEnabled == false )
		return mpParent->HandleEvent( event );

	switch( event.mType )
	{
	case eUIEVENT_LBUTTON_DOWN:
		{
			if( mpFace->ContainPoint( event.mPos ) )
			{
				HERO->SetTargetObject( eOBJECTTYPE_HERO, HERO->GetObjectID() );
				return true;
			}
		}
		break;
	}

	return cUIWindow::HandleEvent( event );
}

void cHeroStateWindow::UpdateRect()
{
	cUIWindow::UpdateRect();

	if( mpLevelImage && mpLevelSkin )
	{
		short x = (short)(mpLevelSkin->mSkinInfo->mX + GetAbsoluteRect().mLeft);
		short y = (short)(mpLevelSkin->mSkinInfo->mY + GetAbsoluteRect().mTop);
		mpLevelImage->SetPos( x, y );
	}
}

void cHeroStateWindow::OnRender(cUIFontItemKeeper* pKeeper )
{
	if( mVisible == false )
		return;

	if( mpLevelSkin )
	{
		cUIRect rc;
		rc.mLeft = mpLevelSkin->mSkinInfo->mX + GetAbsoluteRect().mLeft;
		rc.mTop = mpLevelSkin->mSkinInfo->mY + GetAbsoluteRect().mTop;
		rc.mRight = rc.mLeft + mpLevelSkin->mSkinInfo->mWidth;
		rc.mBottom = rc.mTop + mpLevelSkin->mSkinInfo->mHeight;

		unsigned int px = rc.mLeft + (int)((rc.GetWidth() - FONTAGENT->GetTextExtent( cFontAgent::eFont_UI, mLevel.Cstr(), mLevel.GetLength())) * 0.5f);
		unsigned int py = rc.mTop + (int)((rc.GetHeight() - FONTAGENT->GetTextHeight(cFontAgent::eFont_UI)) * 0.5f);
		pKeeper->AddFontItem( cFontAgent::eFont_System, (LPCTSTR)mLevel.Cstr(), px, py, mDefaultColor );
	}

	/// 이미지 렌더링
	if( mpImage )
		mpImage->Draw();

	/// 자식 렌더링
	cChildList::cIterator i = mChildList.Begin();
	cChildList::cIterator end = mChildList.End();

	for( ; i != end; ++i )
	{
		if( ((cUINode*)(*i))->IsVisible() == true )
		{
			((cUINode*)(*i))->OnRender(pKeeper);
		}
	}

	/// 레벨 배경이미지
	if( mpLevelImage )
		mpLevelImage->Draw();

	pKeeper->DrawAll();
}

/// 주의사항 : 레벨세팅
void cHeroStateWindow::SetLevel( int level )
{
	mLevel.Format( _T("%d"), level );
}

/// 주의사항 : 현재 SXP 세팅 
void cHeroStateWindow::SetSXP( float value )
{
	if( mpSXPGauge )
		mpSXPGauge->SetGaugeNoAni( value );
}
/// 주의사항 : 최대 SXP 세팅 
void cHeroStateWindow::SetMAXSXP( float value )
{
	if( mpSXPGauge )
		mpSXPGauge->SetMaxValue( value );
}

void cHeroStateWindow::SetMaxEXPGauge( float value )
{
	if( mpEXPGauge )
		mpEXPGauge->SetMaxValue( value );
}

void cHeroStateWindow::SetEXPGauge( float value )
{
	if( mpEXPGauge )
		mpEXPGauge->SetGaugeNoAni( value );
}
