#include "StdAfx.h"
#include "ChatExtraWindow.h"
#include "UINodeData.h"
#include "UIImage.h"
#include "PlaneObject.h"
#include "TextBox.h"
#include "ChatManager.h"
#include "ItemManager.h"
#include "Gameresourcemanager.h"
#include "GameUIManager.h"

cChatExtraWindow::cChatExtraWindow()
: mButtonH(0)
{
}

cChatExtraWindow::~cChatExtraWindow()
{
	SAFE_DELETE( mpChatBoxImageDown );
	SAFE_DELETE( mpChatBoxImageUp );
	SAFE_DELETE( mpChatBoxImage );
}

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

	/// 채팅윈도우 등록
	CHATMANAGER->SetChatWindow( this );

	mpChatBox = (cTextBox*)GetChild( eUIID_EXTRA_CHATEXBOX );
	if( mpChatBox )
		mpChatBox->SetScrollBottom( true );

	mDefalutRowNum = mpChatBox->GetMaxRowInPage();

	cUISkin* pSkin = UIMAN->GetSkin();
	if( !pSkin )
	{
		assert(0);
		return false;
	}

	/// 채팅 박스의 스킨 로드
	cUINodeSkin* pplaneSkin = pSkin->GetNodeSkin( "ChatBoxImage" );
	if( pplaneSkin && pplaneSkin->mpTexture )
	{
		/// 텍스처 영역 설정
		unsigned short tx = (unsigned short)pplaneSkin->mSkinInfo->mTexX;
		unsigned short ty = (unsigned short)pplaneSkin->mSkinInfo->mTexY;
		unsigned short tw = (unsigned short)pplaneSkin->mSkinInfo->mTexWidth;
		unsigned short th = (unsigned short)pplaneSkin->mSkinInfo->mTexHeight;
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mTop;
		unsigned short w = mpChatBox->GetAbsoluteRect().GetWidth();
		unsigned short h = mpChatBox->GetAbsoluteRect().GetHeight();

		mpChatBoxImage = new cPlaneObject;
		if( mpChatBoxImage->Create( pplaneSkin->mpTexture, x, y, w, h,
			tx, ty, tx + tw, ty + th ) == false )
		{
			assert( 0 && "failed to create chat box image");
			return false;
		}
	}
	else
	{
		assert(0);
		return false;
	}

	/// 채팅박스 이미지 틀 
	mpUpSkin = pSkin->GetNodeSkin( "ChatBoxImage_Up" );
	if( mpUpSkin && mpUpSkin->mpTexture )
	{
		/// 텍스처 영역 설정
		unsigned short tx = (unsigned short)mpUpSkin->mSkinInfo->mTexX;
		unsigned short ty = (unsigned short)mpUpSkin->mSkinInfo->mTexY;
		unsigned short tw = (unsigned short)mpUpSkin->mSkinInfo->mTexWidth;
		unsigned short th = (unsigned short)mpUpSkin->mSkinInfo->mTexHeight;
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mTop - th;
		unsigned short w = mpChatBox->GetAbsoluteRect().GetWidth();

		mpChatBoxImageUp = new cPlaneObject;
		if( mpChatBoxImageUp->Create( mpUpSkin->mpTexture, x, y, w, th,
			tx, ty, tx + tw, ty + th ) == false )
		{
			assert( 0 && "failed to create chat box up image");
			return false;
		}
	}

	cUINodeSkin* downSkin = pSkin->GetNodeSkin( "ChatBoxImage_Down" );
	if( downSkin && downSkin->mpTexture )
	{
		/// 텍스처 영역 설정
		unsigned short tx = (unsigned short)downSkin->mSkinInfo->mTexX;
		unsigned short ty = (unsigned short)downSkin->mSkinInfo->mTexY;
		unsigned short tw = (unsigned short)downSkin->mSkinInfo->mTexWidth;
		unsigned short th = (unsigned short)downSkin->mSkinInfo->mTexHeight;
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mBottom;
		unsigned short w = mpChatBox->GetAbsoluteRect().GetWidth();

		mpChatBoxImageDown = new cPlaneObject;
		if( mpChatBoxImageDown->Create( downSkin->mpTexture, x, y, w, th,
			tx, ty, tx + tw, ty + th ) == false )
		{
			assert( 0 && "failed to create chat box down image");
			return false;
		}
	}

	/// 윈도우 사이즈 갱신
	if( mpChatBox )
	{
		cUISize winSize;
		winSize.mWidth = GetAbsoluteRect().GetWidth();
		winSize.mHeight = mpChatBox->GetRelativeRect().mTop + mpChatBox->GetRelativeRect().GetHeight();
		SetRelativeSize( winSize );
	}

//	cUINode* pSystem = GetChild( eUIID_EXTRA_SYSTEM );
//	if( pSystem )
//		pSystem->SetPress( true );

	return true;
}

/// 주의사항 : 
void cChatExtraWindow::UpdateRect()
{
	cUIWindow::UpdateRect();

	/// 채팅박스 배경이미지
	if( mpChatBoxImage )
	{
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mTop;
		unsigned short w = mpChatBox->GetAbsoluteRect().GetWidth();
		unsigned short h = mpChatBox->GetAbsoluteRect().GetHeight();
		mpChatBoxImage->SetScreenRect( x, y, w, h );
	}

	/// 위아래 이미지
	if( mpChatBoxImageUp && mpUpSkin )
	{
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mTop - mpUpSkin->mSkinInfo->mTexHeight;
		mpChatBoxImageUp->SetScreenXY( x, y );
	}

	if( mpChatBoxImageDown )
	{
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mBottom;
		mpChatBoxImageDown->SetScreenXY( x, y );
	}
}

void cChatExtraWindow::OnRender( cUIFontItemKeeper* pKeeper )
{
	/// 이미지 렌더링
	if( mpChatBoxImage )
		mpChatBoxImage->Draw();

	if( mpChatBoxImageUp )
		mpChatBoxImageUp->Draw();

	if( mpChatBoxImageDown )
		mpChatBoxImageDown->Draw();

	cUIWindow::OnRender( pKeeper );
}

void cChatExtraWindow::OnButtonDowned(cUINode*, unsigned int id)
{
	switch( id )
	{
	case eUIID_EXTRA_SIZE_PLUS_BUTTON:
		{
			/// 증가
			unsigned int maxNum = mpChatBox->GetMaxRowInPage();

			if( maxNum < 8 )
			{
				int rowHeight = mpChatBox->GetRowHeight();
				UpdateSize( rowHeight );
			}
		}
		break;
	case eUIID_EXTRA_SIZE_MINUS_BUTTON:
		{
			/// 감소
			unsigned int maxNum = mpChatBox->GetMaxRowInPage();

			if( maxNum > mDefalutRowNum )
			{
				int rowHeight = mpChatBox->GetRowHeight();
				UpdateSize( -rowHeight );
			}
		}
		break;
	}
}

/// 주의사항 : 
void cChatExtraWindow::OnCommand( cUINode*, unsigned int id )
{
	switch( id )
	{
	case eUIID_EXTRA_CLOSE_BUTTON:
		{
			Hide();
		}
		break;
	case eUIID_EXTRA_SIZE_PLUS_BUTTON:
		{
			/// 증가
			unsigned int maxNum = mpChatBox->GetMaxRowInPage();

			if( maxNum < 8 )
			{
				int rowHeight = mpChatBox->GetRowHeight();
				UpdateSize( rowHeight );
			}
		}
		break;
	case eUIID_EXTRA_SIZE_MINUS_BUTTON:
		{
			/// 감소
			unsigned int maxNum = mpChatBox->GetMaxRowInPage();

			if( maxNum > mDefalutRowNum )
			{
				int rowHeight = mpChatBox->GetRowHeight();
				UpdateSize( -rowHeight );
			}
		}
		break;
	}
}

/// 주의사항 : 
void cChatExtraWindow::UpdateSize( int height )
{
	if( !mpChatBox )
	{
		assert(0);
		return;
	}

	/// 채팅박스 갱신 
	cUISize boxsize;
	boxsize.mWidth = mpChatBox->GetAbsoluteRect().GetWidth();
	boxsize.mHeight = mpChatBox->GetAbsoluteRect().GetHeight() + height;
	mpChatBox->SetRelativeSize( boxsize );

	/// 채팅윈도우 갱신
	cUISize winSize;
	winSize.mWidth = GetAbsoluteRect().GetWidth();
	winSize.mHeight = mpChatBox->GetRelativeRect().mTop + mpChatBox->GetRelativeRect().GetHeight();
	SetRelativeSize( winSize );

	/// 이미지갱신
	if( mpChatBoxImage )
	{
		unsigned short x = mpChatBox->GetAbsoluteRect().mLeft;
		unsigned short y = mpChatBox->GetAbsoluteRect().mTop;
		unsigned short w = mpChatBox->GetAbsoluteRect().GetWidth();
		unsigned short h = mpChatBox->GetAbsoluteRect().GetHeight();
		mpChatBoxImage->SetScreenRect( x, y, w, h );
	}

	/// 사이즈 변경에 따른 채팅박스 데이터 갱신 
	bool add = ( height >= 0 ) ? true : false;
	mpChatBox->UpdateTextBox( add );
}

/// 외부에서 채팅박스에 직접적으로 문자열을 넣을 경우 사용
void cChatExtraWindow::InsertText( eChatMsgKind msgkind, LPCTSTR text )
{
	if( !mpChatBox )
	{
		assert(0);
		return;
	}

	TCHAR temp[256]={0,};

	switch( msgkind )
	{
	case eCHAT_NORMAL:
		{
			cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );

			if( pNormal && pNormal->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 86 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_WHITE );
			}
		}
		break;
	case eCHAT_PARTY:
		{
			cUINode* pParty = GetChild( eUIID_EXTRA_PARTY );
			
			if( pParty && pParty->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 87 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_PARTY );
			}
		}
		break;
	case eCHAT_GUILD:
		{
			cUINode* pGuild = GetChild( eUIID_EXTRA_GUILD );

			if( pGuild && pGuild->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 88 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_GUILD );
			}
		}
		break;
	case eCHAT_SHOUT:
		{
			cUINode* pShout = GetChild( eUIID_EXTRA_SHOUT );

			if( pShout && pShout->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 89 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_SHOUT );
			}
		}
		break;
	case eCHAT_TRADE:
		{
			cUINode* pTrade = GetChild( eUIID_EXTRA_TRADE );
			if( pTrade && pTrade->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 90 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_TRADE );
			}
		}
		break;
	case eCHAT_WHISPER:
		{
			cUINode* pWhisper = GetChild( eUIID_EXTRA_WHISPER );
			if( pWhisper && pWhisper->IsPress() == true )
			{
				mpChatBox->AddNewRow( text, eCOLOR_WHISPER );
			}
		}
		break;
	case eCHAT_GM:
		{
			cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );

			if( pNormal && pNormal->IsPress() == true )
			{
				::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 180 ), text );
				mpChatBox->AddNewRow( temp, eCOLOR_GM );
			}
		}
		break;
	}
}

void cChatExtraWindow::InsertTextSystem( eSystemMsgKind kind, LPCTSTR chatmsg )
{
	if( !mpChatBox )
	{
		assert(0);
		return;
	}

	unsigned long color = eCOLOR_WHITE;

	switch( kind )
	{
	case eSYSTEM_NORMAL:	color = 0xFFFFFFFF;		break;
	case eSYSTEM_GETMONEY:	color = 0xFFFFFF0A;		break;
	case eSYSTEM_GETITEM:	color = 0xFF02FFA3;		break;
	case eSYSTEM_QUEST:		color = 0xFF00FFFF;		break;
	case eSYSTEM_DOITEM:	color = 0xFFA6B27D;		break;
	case eSYSTEM_CASH:		color = 0xFF643264;		break;
	case eSYSTEM_NPCSTORE:  color = 0xFFE9A921;		break;
	case eSYSTEM_PVP:		color = eCOLOR_RED;		break;
	case eSYSTEM_GM:		color = eCOLOR_RED;		break;
	default:				assert(0);				break;
	}

	cUINode* pSystem = GetChild( eUIID_EXTRA_SYSTEM );
	if( pSystem && pSystem->IsPress() == true )
	{
		TCHAR temp[256]={0,};
		::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 91 ), chatmsg );
		mpChatBox->AddNewRow( temp, color );
	}
}

void cChatExtraWindow::InsertItemText( unsigned long itemIndex, unsigned int slotIndex, LPCTSTR msg1, LPCTSTR msg2, bool IsQuest)
{
	if( !mpChatBox )
	{
		assert(0);
		return;
	}

	/// 시스템 메세지 보이기 설정되어 있으면 출력
	cUINode* pSystem = GetChild( eUIID_EXTRA_SYSTEM );
	if( pSystem && pSystem->IsPress() == true )
	{
		cItemDefine* itemDefine = ITEMMAN->GetItemDefine( itemIndex );
		if( !itemDefine )
			return;

		/// 글씨 컬러 정하기
		unsigned long itemColor = eCOLOR_WHITE;
		unsigned long textColor = (IsQuest == false) ? eCOLOR_WHITE : 0xFF00FFFF;	/// 퀘스트 색
		ITEMMAN->GetTooltipColor( itemDefine->GetTipType(), itemColor );

		/// 새로운줄에 삽입할지 결정
		TCHAR itemName[256] = {0,};
		::_stprintf( itemName, GAMERESOURCEMAN->GetGameText( 241 ), itemDefine->GetName() );

		TCHAR temp[256]={0,};
		if( msg1 )
			::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 91 ), msg1 );
		else
			::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 93 ) );

		mpChatBox->AddNewRow( temp, textColor );
		mpChatBox->AddItemRow( false, itemIndex, slotIndex, itemName, itemColor );
		mpChatBox->AddPasteRow( msg2, textColor );
	}
}

/// 아이템 거래 메세지 출력
void cChatExtraWindow::InsertItemText( sInventory inventory, LPCTSTR name, LPCTSTR msg, eChatMsgKind kind )
{
	if( !mpChatBox )
	{
		assert(0);
		return;
	}

	TCHAR temp[256] = {0,};

	cItemDefine* define = ITEMMAN->GetItemDefine( inventory.ItemIndex );
	if( !define )
	{
		/// 링크된 아이템이 없으면 그냥 출력
		if( kind != eCHAT_WHISPER )
		{
			::_stprintf( temp, UIMAN->GetUIText( 10000 ), name, msg );
			InsertText( kind, temp );
		}
		else
		{
			::_stprintf( temp, _T("%s%s"), name, msg );
			InsertText( kind, temp );
		}
	}
	else
	{
		/// 링크된 아이템이 있으면 연결정보 출력
		unsigned long itemColor = eCOLOR_WHITE;
		ITEMMAN->GetTooltipColor( define->GetTipType(), itemColor );

		TCHAR itemName[256] = {0,};

		/// 강화단계 + 이름 출력
		if( inventory.enhanced > 0 )
		{
			::_stprintf( itemName, GAMERESOURCEMAN->GetGameText(249), inventory.enhanced, define->GetName() );
		}
		else
		{
			::_stprintf( itemName, GAMERESOURCEMAN->GetGameText(241), define->GetName() );
		}

		switch( kind )
		{
		case eCHAT_NORMAL:
			{	
				cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );
				if( pNormal && pNormal->IsPress() == true )
				{
					::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 94 ), name );

					mpChatBox->AddNewRow( temp, eCOLOR_WHITE );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_WHITE );
				}
			}
			break;
		case eCHAT_PARTY:
			{
				cUINode* pParty = GetChild( eUIID_EXTRA_PARTY );
				if( pParty && pParty->IsPress() == true )
				{
					::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 95 ), name );

					mpChatBox->AddNewRow( temp, eCOLOR_PARTY );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_PARTY );
				}
			}
			break;
		case eCHAT_GUILD:
			{
				cUINode* pGuild = GetChild( eUIID_EXTRA_GUILD );
				if( pGuild && pGuild->IsPress() == true )
				{
					::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 96 ), name );

					mpChatBox->AddNewRow( temp, eCOLOR_GUILD );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_GUILD );
				}
			}
			break;
		case eCHAT_TRADE:
			{
				cUINode* pTrade = GetChild( eUIID_EXTRA_TRADE );
				if( pTrade && pTrade->IsPress() == true )
				{
					::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 97 ), name );

					mpChatBox->AddNewRow( temp, eCOLOR_TRADE );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_TRADE );
				}
			}
			break;
		case eCHAT_WHISPER:
			{
				cUINode* pWhisper = GetChild( eUIID_EXTRA_WHISPER );
				if( pWhisper && pWhisper->IsPress() == true )
				{
					mpChatBox->AddNewRow( name, eCOLOR_WHISPER );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_WHISPER );
				}
			}
			break;
		case eCHAT_GM:
			{	
				cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );
				if( pNormal && pNormal->IsPress() == true )
				{
					::_stprintf( temp, GAMERESOURCEMAN->GetGameText( 181 ), name );

					mpChatBox->AddNewRow( temp, eCOLOR_GM );
					mpChatBox->AddItemRow( inventory, itemName, itemColor );
					mpChatBox->AddPasteRow( msg, eCOLOR_GM );
				}
			}
			break;
		default: 
			assert(0);
			break;
		}
	}
}

void cChatExtraWindow::SetPos( int i )
{
	/// 생성위치 조정하기 ( 조금씩 밀려서 생성 
	cUISkin* pSkin = UIMAN->GetSkin();
	if( pSkin )
	{
		cUINodeSkin* pNodeSkin = pSkin->GetNodeSkin( "ChatExtraWindow" );
		if( pNodeSkin )
		{
			cUIPos pos;
			pos.mX = pNodeSkin->mSkinInfo->mX + (i*CHATEXTRA_GAP);
			pos.mY = pNodeSkin->mSkinInfo->mY + (i*CHATEXTRA_GAP);
			SetRelativePos( pos );
		}
	}
}

void cChatExtraWindow::SetPress( bool normal, bool party, bool guild, bool shout, bool trade, bool whisper, bool system )
{
	cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );
	if( pNormal )
		pNormal->SetPress( normal );

	cUINode* pParty = GetChild( eUIID_EXTRA_PARTY );
	if( pParty )
		pParty->SetPress( party );

	cUINode* pGuild = GetChild( eUIID_EXTRA_GUILD );
	if( pGuild )
		pGuild->SetPress( guild );

	cUINode* pShout = GetChild( eUIID_EXTRA_SHOUT );
	if( pShout )
		pShout->SetPress( shout );

	cUINode* pTrade = GetChild( eUIID_EXTRA_TRADE );
	if( pTrade )
		pTrade->SetPress( shout );

	cUINode* pWhisper = GetChild( eUIID_EXTRA_WHISPER );
	if( pWhisper )
		pWhisper->SetPress( whisper );

	cUINode* pSystem = GetChild( eUIID_EXTRA_SYSTEM );
	if( pSystem )
		pSystem->SetPress( system );
}

bool cChatExtraWindow::IsPressNormal()
{
	cUINode* pNormal = GetChild( eUIID_EXTRA_NOMAL );
	if( pNormal && pNormal->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressParty()
{
	cUINode* pParty = GetChild( eUIID_EXTRA_PARTY );
	if( pParty && pParty->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressGuild()
{
	cUINode* pGuild = GetChild( eUIID_EXTRA_GUILD );
	if( pGuild && pGuild->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressShout()
{
	cUINode* pShout = GetChild( eUIID_EXTRA_SHOUT );
	if( pShout && pShout->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressTrade()
{
	cUINode* pTrade = GetChild( eUIID_EXTRA_TRADE );
	if( pTrade && pTrade->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressWhisper()
{
	cUINode* pWhisper = GetChild( eUIID_EXTRA_WHISPER );
	if( pWhisper && pWhisper->IsPress() == true )
		return true;

	return false;
}

bool cChatExtraWindow::IsPressSystem()
{
	cUINode* pSystem = GetChild( eUIID_EXTRA_SYSTEM );
	if( pSystem && pSystem->IsPress() == true )
		return true;

	return false;
}
