#include "StdAfx.h"
#include "RowQuickSlot.h"
#include "QuickManager.h"
#include "DragWindow.h"
#include "RadioButton.h"
#include "GameUIManager.h"
#include "SkillManager.h"
#include "SkillTipWindow.h"

#include "ObjectManager.h"
#include "Hero.h"
#include "CommunityScript.h"
#include "SkillScript.h"
#include "GameResourceManager.h"

cRowQuickSlot::cRowQuickSlot()
{
	mRadio[0] = 0;
	mRadio[1] = 0;
	mRadio[2] = 0;

	for( unsigned int i = 0; i < 10; ++i )
		mpNumImage[i] = 0;

	for( unsigned int i = 0; i < QUICKSLOT_COUNT; ++i )
		mIcon[i] = 0;
}

cRowQuickSlot::~cRowQuickSlot()
{
	mRadio[0] = 0;
	mRadio[1] = 0;
	mRadio[2] = 0;

	for( unsigned int i = 0; i < 10; ++i )
		SAFE_DELETE( mpNumImage[i] );
}

void cRowQuickSlot::OnShow()
{
	unsigned int tabIndex = QUICKMAN->GetTabIndex();
	if( mRadio[tabIndex] && mRadio[tabIndex]->IsPress() == false )
	{
		mRadio[tabIndex]->SetPressNoEvent( true, false );
	}

	cUIWindow::OnShow();
}

void cRowQuickSlot::ChangeTab()
{
	unsigned int tabIndex = QUICKMAN->GetTabIndex();
	tabIndex++;
	if( tabIndex > 2 )
		tabIndex = 0;

	if( mRadio[tabIndex] )
		mRadio[tabIndex]->SetPress( true );
}

void cRowQuickSlot::ChangeTab( int tabIndex )
{
	if( mRadio[tabIndex] )
		mRadio[tabIndex]->SetPress( true );
}

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

	mRadio[0] = (cRadioButton*)GetChild( eUIID_ROW_TAB1 );
	mRadio[1] = (cRadioButton*)GetChild( eUIID_ROW_TAB2 );
	mRadio[2] = (cRadioButton*)GetChild( eUIID_ROW_TAB3 );
	
	cString str;
	/// 아이콘 생성
	for( unsigned int i = 0; i < QUICKSLOT_COUNT; ++i )
	{
		str.Format( "QuickRow_Icon%02d", i );
		cIcon* icon = new cIcon;
		if( icon->CreateBySkinName( str, this, i ) == false )
			return false;

		mIcon[i] = icon;
		QUICKMAN->BindRowIcon( i, icon );
	}

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

	cString skinName;
	for( unsigned int i = 0; i < 10; ++i )
	{
		/// 
		skinName.Format( "QuickNumF%02d", i );
		cUINodeSkin* pNodeSkin = pSkin->GetNodeSkin( skinName.Cstr() );

		mpNumImage[i] = new cUIImage( pNodeSkin->mpTexture );
		if( mpNumImage[i] )
		{
			unsigned int x = mIcon[i]->GetAbsoluteRect().mLeft + 1;
			unsigned int y = mIcon[i]->GetAbsoluteRect().mTop + 1;

			unsigned int tx = pNodeSkin->mSkinInfo->mTexX;
			unsigned int ty = pNodeSkin->mSkinInfo->mTexY;
			unsigned int w = pNodeSkin->mSkinInfo->mWidth;
			unsigned int h = pNodeSkin->mSkinInfo->mHeight;
			mpNumImage[i]->SetTextureRect( tx, ty, tx + w, ty + h );
			mpNumImage[i]->SetScreenRect( cUIRect( x, y, x + w, y + h ) );
		}
	}
	return true;
}

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

	for( unsigned int i = 0; i < 10; ++i )
	{
		if( mpNumImage[i] )
		{
			unsigned int x = mIcon[i]->GetAbsoluteRect().mLeft + 1;
			unsigned int y = mIcon[i]->GetAbsoluteRect().mTop + 1;

			mpNumImage[i]->SetPos( x, y );
		}
	}
}

void cRowQuickSlot::OnRender(cUIFontItemKeeper* pKeeper )
{
	cUIWindow::OnRender( pKeeper );

	for( unsigned int i = 0; i < 10; ++i )
	{
		if( mpNumImage[i])
			mpNumImage[i]->Draw();
	}
}

void cRowQuickSlot::OnCommand( cUINode*, unsigned int id )
{
	if( id == eUIID_ROW_ROTATE )
	{	
		Hide();

		///
		cUIWindow* colQuick = (cUIWindow*)UIMAN->GetContainer( eUIID_COLUMN_QUICKSLOT );
		if( colQuick )
		{
			colQuick->ShowTop();
			QUICKMAN->SetRowQuick( false );
		}
	}
}

/// 주의사항 : 
void cRowQuickSlot::OnRadioPressed( cUINode*, unsigned int id )
{
	unsigned int tabIndex = 0;
	switch( id )
	{
	case eUIID_ROW_TAB1:
		tabIndex = 0;
		break;
	case eUIID_ROW_TAB2:
		tabIndex = 1;
		break;
	case eUIID_ROW_TAB3:
		tabIndex = 2;
		break;
	default:
		assert(0);
		return;
	}

	/// 퀵슬롯 탭변경
	QUICKMAN->ChangeTab( tabIndex );
	UIMAN->HideTip();
}


void cRowQuickSlot::OnIconHovered( cUINode* caller, unsigned int id, const cUIPos& pos )
{
	if( id < 0 || id >= QUICKSLOT_COUNT )
	{
		assert(0 && "error id");
		return;
	}

	cIcon* icon = (cIcon*)caller;
	if( !icon )
	{
		assert(0);
		return;
	}

	eSHORTCUT_TYPE type = QUICKMAN->GetQuickType( id );
	if( type == eSHORTCUT_ITEM )
	{
		/// 아이템
		unsigned int itemIndex = QUICKMAN->GetItemIndex( id );
		UIMAN->ShowItemTip( eTIP_QUICK, pos, itemIndex, UINT_MAX, &icon->GetGauge() );
	}
	else if( type == eSHORTCUT_SKILL )
	{
		/// 스킬
		unsigned int skillIndex = QUICKMAN->GetSkillIndex( id );

		/// 스킬 팁 
		sKeepSkill* pInfo = SKILLMAN->GetKeepInfo( skillIndex );
		if( pInfo )
		{
			UIMAN->ShowSkillTip( eTIP_QUICK, GAMEUI->GetSkillTip(), pos, skillIndex, pInfo->mStep, false );
		}
	}
	else if( type == eSHORTCUT_COMMU )
	{
		unsigned int index = QUICKMAN->GetSkillIndex( id );
		sCommunitySkillInfo* pInfo = COMMUNITYSCRIPT->GetCommunityInfo( index );
		if( pInfo )
		{
			UIMAN->ShowCommunityTip( GAMEUI->GetSimpleTip(), pos, UIMAN->GetUIText( pInfo->mTooltipIndex) );
		}
	}
}

void cRowQuickSlot::OnIconDragged( cUINode* caller, unsigned int id, const cUIPos& pos, bool shift )
{
	cIcon* icon = (cIcon*)caller;
	if( icon )
	{
		bool IsUse = QUICKMAN->IsUseSlot( id );

		/// 데이터가 있는 슬롯만 드래그 가능함.
		if( IsUse == true )
			UIMAN->ShowDrag( this, DRAG_FROM_QUICKSLOT, icon, pos, shift );

	}
}

/// 퀵슬롯 사용
void cRowQuickSlot::OnIconRightClicked( cUINode*, unsigned int id, bool )
{
	QUICKMAN->OnMouseDown( id );
}

/// 퀵슬롯 사용
void cRowQuickSlot::OnIconDoubleClicked( cUINode*, unsigned int id )
{
	QUICKMAN->OnMouseDown( id );
}

//////////////////////////////////////////////////////////////////////// 
/// FS 퀵슬롯
cFSRowQuickSlot::cFSRowQuickSlot()
{
	for( unsigned int i = 0; i < QUICKSLOT_COUNT; ++i )
		mIcon[i] = 0;
}

cFSRowQuickSlot::~cFSRowQuickSlot()
{
	for( unsigned int i = 0; i < QUICKSLOT_COUNT; ++i )
		SAFE_DELETE( mpNumImage[i] );
}

bool cFSRowQuickSlot::OnCreate( cUINodeProperty* pproperty )
{
	if( cUIWindow::OnCreate( pproperty ) == false )
		return false;

	cString str;
	/// 아이콘 생성
	for( unsigned int i = 0; i < QUICKSLOT_COUNT; ++i )
	{
		str.Format( "QuickFSRow_Icon%02d", i );
		cIcon* icon = new cIcon;
		if( icon->CreateBySkinName( str, this, i ) == false )
			return false;

		mIcon[i] = icon;
		QUICKMAN->BindFSRowIcon( i, icon );
	}

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

	cString skinName;
	for( unsigned int i = 0; i < 10; ++i )
	{
		/// 
		skinName.Format( "QuickNum%02d", i );
		cUINodeSkin* pNodeSkin = pSkin->GetNodeSkin( skinName.Cstr() );

		mpNumImage[i] = new cUIImage( pNodeSkin->mpTexture );
		if( mpNumImage[i] )
		{
			unsigned int x = mIcon[i]->GetAbsoluteRect().mLeft + 1;
			unsigned int y = mIcon[i]->GetAbsoluteRect().mTop + 1;

			unsigned int tx = pNodeSkin->mSkinInfo->mTexX;
			unsigned int ty = pNodeSkin->mSkinInfo->mTexY;
			unsigned int w = pNodeSkin->mSkinInfo->mWidth;
			unsigned int h = pNodeSkin->mSkinInfo->mHeight;
			mpNumImage[i]->SetTextureRect( tx, ty, tx + w, ty + h );
			mpNumImage[i]->SetScreenRect( cUIRect( x, y, x + w, y + h ) );
		}
	}
	return true;
}

void cFSRowQuickSlot::OnCommand( cUINode* caller, unsigned int id )
{
	if( id == eUIID_ROW_ROTATE_FS )
	{	
		Hide();

		///
		cUIWindow* colQuick = (cUIWindow*)UIMAN->GetContainer( eUIID_COLUMN_QUICKSLOT_FS );
		if( colQuick )
		{
			colQuick->ShowTop();
			QUICKMAN->SetRowQuickFS( false );
		}
	}
}

void cFSRowQuickSlot::OnIconHovered(cUINode* caller, unsigned int id, const cUIPos& pos )
{
	if( id < 0 || id >= QUICKSLOT_COUNT )
	{
		assert(0 && "error id");
		return;
	}

	cIcon* icon = (cIcon*)caller;
	if( !icon )
	{
		assert(0);
		return;
	}

	eSHORTCUT_TYPE type = QUICKMAN->GetQuickType( id, true );
	if( type == eSHORTCUT_ITEM )
	{
		/// 아이템
		unsigned int itemIndex = QUICKMAN->GetItemIndex( id, true );
		UIMAN->ShowItemTip( eTIP_QUICK, pos, itemIndex, UINT_MAX, &icon->GetGauge() );
	}
	else if( type == eSHORTCUT_SKILL )
	{
		/// 스킬
		unsigned int skillIndex = QUICKMAN->GetSkillIndex( id, true );
		sKeepSkill* pInfo = SKILLMAN->GetKeepInfo( skillIndex );
		if( pInfo )
		{
			UIMAN->ShowSkillTip( eTIP_QUICK, GAMEUI->GetSkillTip(), pos, skillIndex, pInfo->mStep, false );
		}
	}
	else if( type == eSHORTCUT_MONSKILL )
	{
		/// 몬스터 변신상태
		short skillIndex = QUICKMAN->GetMonSkillIndex( id );
		if( skillIndex == -1 || skillIndex >= eMONSTERATTACK_MAX )
			return;

		unsigned long monIdx = HERO->GetTransMonsterClassIdx();
		sMonsterSkillScript* skill = SKILLSCRIPT->GetMonsterSkillInfo( monIdx, (eMONSTERATTACK_TYPE)skillIndex );
		if( !skill )
		{
			assert(0);
			return;
		}

		UIMAN->ShowCommunityTip( GAMEUI->GetSimpleTip(), pos, GAMERESOURCEMAN->GetSkillToolTipText(skill->mToolTipIdx) );
	}
	else if( type == eSHORTCUT_COMMU )
	{
		unsigned int index = QUICKMAN->GetSkillIndex( id, true );
		sCommunitySkillInfo* pInfo = COMMUNITYSCRIPT->GetCommunityInfo( index );
		if( pInfo )
		{
			UIMAN->ShowCommunityTip( GAMEUI->GetSimpleTip(), pos, UIMAN->GetUIText( pInfo->mTooltipIndex) );
		}
	}
}

void cFSRowQuickSlot::OnIconDragged( cUINode* caller, unsigned int id, const cUIPos& pos, bool shift )
{
	cIcon* icon = (cIcon*)caller;
	if( icon )
	{
		bool IsUse = QUICKMAN->IsUseSlot( id, true );

		/// 데이터가 있는 슬롯만 드래그 가능함.
		if( IsUse == true )
			UIMAN->ShowDrag( this, DRAG_FROM_QUICKSLOT, icon, pos, shift );
	}
}

void cFSRowQuickSlot::OnIconRightClicked( cUINode* caller, unsigned int id, bool shift )
{
	QUICKMAN->OnMouseDown( id, true );
}

void cFSRowQuickSlot::OnIconDoubleClicked( cUINode* caller, unsigned int id )
{
	QUICKMAN->OnMouseDown( id, true );
}


