#include "stdafx.h"
#include "NpcDisjointWindow.h"
#include "TipWindow.h"
#include "GameUIManager.h"
#include "ItemManager.h"
#include "Icon.h"
#include "Gauge.h"
#include "Label.h"
#include "NumberEditBox.h"
#include "Button.h"
#include "ItemIcon.h"
#include "GameResourceManager.h"
#include "ChatManager.h"

cNpcDisjointWindow::cNpcDisjointWindow()
: mItemSlotIndex( UINT_MAX )
, mpGaugeBar(0)
, mpPrice(0)
, mpDisjointButton(0)
, mItemIcon(0)
{
	for( int i = 0; i < DISJOINT_COUNT; ++i )
		mResultIcon[i] = 0;
}

cNpcDisjointWindow::~cNpcDisjointWindow()
{
}

/// 초기화
void cNpcDisjointWindow::Clear()
{
	/// 아이콘 초기화
	mItemIcon->ChangeImage( 0 );

	for( unsigned int i = 0; i < DISJOINT_COUNT; ++i )
	{
		mResultIcon[i]->ChangeImage( 0 );
		mResultIcon[i]->SetItemIndex( 0 );
		mResultIcon[i]->ShowNumber( false );
		mResultIcon[i]->SetNumber( 0 );
	}

	/// 아이템 락 초기화
	if( mItemSlotIndex <= MAX_INVENTORY )
	{
		cItem& oldItem = ITEMMAN->GetItem( mItemSlotIndex );
		oldItem.SetLock( false );
		mItemSlotIndex = UINT_MAX;
	}

	if( mpGaugeBar )
		mpGaugeBar->ClearFront();

	SetPrice( 0 );

	///
	NetUnLock();
}

void cNpcDisjointWindow::OnHide()
{
	cUIWindow::OnHide();

	///
	UIMAN->HideNpcBack();

	///
	Clear();
}

bool cNpcDisjointWindow::OnCreate( cUINodeProperty* property )
{
	if( cUIWindow::OnCreate( property ) == false )
		return false;

	mpGaugeBar = (cGauge*)GetChild( eUIID_GAME_NPCDISJOINT_GAUGEBAR );
	mpPrice	   = (cLabel*)GetChild( eUIID_GAME_NPCDISJOINT_PRICE );
	mpDisjointButton = (cButton*)GetChild( eUIID_GAME_NPCDISJOINT_DISJOINT );

	if( mpGaugeBar )
		mpGaugeBar->SetMaxValue( 100 );

	/// 분해 결과 아이콘 생성
	cString str;
	for( unsigned int i = 0; i < DISJOINT_COUNT; ++i )
	{
		str.Format( "Disjoint_Result%02d", i );
		cItemIcon* icon = new cItemIcon;
		if( icon->CreateBySkinName( str, this, i+1 ) == false )
			return false;

		icon->SetUsedFlag( false, false );
		mResultIcon[i] = icon;
	}

	/// 분해할 아이템 아이콘 생성
	mItemIcon = new cItemIcon;
	if( mItemIcon->CreateBySkinName( "Disjoint_Item", this, 100 ) == false )
		return false;

	/// hover 이벤트는 처리하지만 이동 불가 
	mItemIcon->SetUsedFlag( false, false );
	mItemIcon->SetSlotIndex( DISJOINT_SLOT );
	mItemIcon->SetLinked( true );	/// 인벤토리에서 참조함을 세팅

	//
	SetPrice(0);
	return true;
}

//bool cNpcDisjointWindow::HandleEvent( const cUIEvent& event )
//{
//	/// 게이지의 이벤트를 받기 위함
//	if( mVisible == false )
//		return false;
//
//	if( cUIContainer::HandleEvent( event ) == false )
//		return false;
//	return true;
//}

void cNpcDisjointWindow::OnCommand( cUINode*, unsigned int id )
{
	switch( id )
	{
	case eUIID_GAME_NPCDISJOINT_CLOSE:
	case eUIID_GAME_NPCDISJOINT_CANCEL:
		/// 닫기버튼
		Hide();
		break;
	case eUIID_GAME_NPCDISJOINT_DISJOINT:
		if( mItemSlotIndex <= MAX_INVENTORY )
		{
			/// 분해 조건검사
			if( ITEMMAN->CheckDisjoint( mItemSlotIndex ) == false )
				return;

			if( mpGaugeBar )
				mpGaugeBar->SetGaugeAni( 100.0f );

			/// 락 걸기
			NetLock();
		}
		break;
	}
}

void cNpcDisjointWindow::OnGaugeEnd( cUINode* n , unsigned int )
{
	if( mpGaugeBar && n == mpGaugeBar )
	{
		/// 분해시작버튼
		if( ITEMMAN->TryToDisjoint( mItemSlotIndex ) == false )
		{
			NetUnLock();
			CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 261 ) );

			if( mpGaugeBar )
				mpGaugeBar->ClearFront();
		}

		UIMAN->HideTip();
	}
}

/// 분해 아이템 등록
bool cNpcDisjointWindow::AddItem( unsigned int slotIndex )
{
	/// 이미 분해중이면 리턴
	if( mpDisjointButton->IsNetLock() == true )
		return false;

	if( slotIndex > MAX_INVENTORY )
		return false;

	cItem& item = ITEMMAN->GetItem( slotIndex );
	cItemDefine* define = item.GetDefine();
	if( !define )
	{
		assert(0);
		return false;
	}

	sItemDisjoint* data = define->GetDisjoint();
	if( !data )
		return false;

	/// 결과 정보 초기화
	for( unsigned int i = 0; i < DISJOINT_COUNT; ++i )
	{
		mResultIcon[i]->ChangeImage( 0 );
		mResultIcon[i]->SetTipType( eTOOLTIP_NONE );
		mResultIcon[i]->SetNumber( 0 );
		mResultIcon[i]->SetItemIndex( 0 );
		mResultIcon[i]->ShowNumber( false );
	}

	/// 기존 등록 아이템 삭제
	if( mItemSlotIndex <= MAX_INVENTORY && mItemSlotIndex != slotIndex )
	{
		cItem& oldItem = ITEMMAN->GetItem( mItemSlotIndex );
		oldItem.SetLock( false );
	}

	/// 참조 슬롯 인덱스
	mItemSlotIndex = slotIndex;
	item.SetLock( true );

	/// 아이템 이미지 체인지
	mItemIcon->SetItemIndex( item.GetIndex() );
	mItemIcon->ChangeImage( define->GetIconParam() );
	mItemIcon->SetTipType( define->GetTipType() );

	/// 분해 결과 아이템 정보 등록
	int result = 0;
	for( int i = 0 ; i < MAX_DISJOINT; ++i, ++result )
	{
		if( data->mDisjoint[i].itemIndex == 0 )
			break;

		cItemDefine* resultDefine = ITEMMAN->GetItemDefine( data->mDisjoint[i].itemIndex );
		if( !resultDefine )
		{
			assert(0);
			return false;
		}

		/// 정보 세팅
		mResultIcon[result]->SetItemIndex( data->mDisjoint[i].itemIndex );
		mResultIcon[result]->ChangeImage( resultDefine->GetIconParam() );
		mResultIcon[result]->SetNumber( data->mDisjoint[i].count );
		mResultIcon[result]->SetTipType( resultDefine->GetTipType() );

		bool show = ( resultDefine->GetCapacity() > 1 ) ? true : false;
		mResultIcon[result]->ShowNumber( show );
	}

	SetPrice( data->mPrice );
	return true;
}

void cNpcDisjointWindow::SetPrice( unsigned int price )
{
	if( mpPrice )
	{
		LPCTSTR value = cNumberEditBox::FormatNumber( price );
		mpPrice->SetText( value );
	}
}

///
void cNpcDisjointWindow::OnNetLock( int lockTry )
{
	if( mpDisjointButton )
		mpDisjointButton->NetLock( lockTry );
}

void cNpcDisjointWindow::OnNetUnLock( int lockTry )
{
	if( mpDisjointButton )
		mpDisjointButton->NetUnLock( lockTry );
}

void cNpcDisjointWindow::OnIconHovered( cUINode* caller, unsigned int id, const cUIPos& pos )
{
	cIcon* hoverIcon = (cIcon*)caller;
	if( hoverIcon == mItemIcon && mItemSlotIndex <= MAX_INVENTORY )
	{
		cItem& item = ITEMMAN->GetItem( mItemSlotIndex );
		UIMAN->ShowItemTip( eTIP_ITEM, pos, item.GetIndex(), mItemSlotIndex, 0 );
	}
}