#include "StdAfx.h"
#include "EnhancedWindow.h"
#include "ItemManager.h"
#include "Button.h"
#include "GameUIManager.h"
#include "TipWindow.h"
#include "Gauge.h"
#include "GameResourceManager.h"
#include "ChatManager.h"
#include "ObjectManager.h"
#include "Hero.h"

cEnhancedWindow::cEnhancedWindow()
: mItemSlotIndex(UINT_MAX)
, mCardSlotIndex(UINT_MAX)
, mpStart(0)
, mpGaugeBar(0)
, mItemSlot(0)
, mCardSlot(0)
, mpSuccess(0)
, mpFailure(0)
{

}

cEnhancedWindow::~cEnhancedWindow()
{
}

void cEnhancedWindow::Clear( bool allclear )
{
	if( allclear == true )
	{
		if( mItemSlot )
		{
			mItemSlot->ChangeImage( 0 );
			mItemSlot->SetTipType( eTOOLTIP_NONE );
			mItemSlot->SetItemIndex( 0 );
		}

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

	/// 카드 클리어
	if( mCardSlot )
	{
		mCardSlot->ChangeImage( 0 );
		mCardSlot->SetTipType( eTOOLTIP_NONE );
		mCardSlot->SetItemIndex( 0 );
	}
	
	if( mCardSlotIndex <= MAX_INVENTORY )
	{
		cItem& oldCard = ITEMMAN->GetItem( mCardSlotIndex );
		oldCard.SetLock( false );
		mCardSlotIndex = UINT_MAX;
	}
	
	if( mpGaugeBar )
		mpGaugeBar->ClearFront();
}

void cEnhancedWindow::ShowTop(bool hideOthers, bool onsound )
{
	if( HERO->GetStopFlag() != eSTOP_ITEMMIX )
		cUIWindow::ShowTop( hideOthers, onsound );
}

void cEnhancedWindow::OnShow()
{
	cUIWindow::OnShow();

	GAMEUI->HideItemSetWindow( this );
}

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

	Clear( true );

	/// 결과 처리
	HideResult();
}

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

	mpStart = (cButton*)GetChild( eUIID_GAME_ENHANCED_START	);
	mpGaugeBar = (cGauge*)GetChild( eUIID_GAME_ENHANCED_GAUGEBAR );
	mpSuccess = GetChild( eUIID_GAME_ENHANCED_SUCCESS );
	mpFailure = GetChild( eUIID_GAME_ENHANCED_FAILURE );

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

	/// 결과 처리
	HideResult();

	/// 강화 아이템 아이콘 생성
	mItemSlot = new cItemIcon;
	if( mItemSlot->CreateBySkinName( "Enhanced_ItemSlot", this, 1 ) == false )
		return false;

	/// 강화 카드 아이콘 생성
	mCardSlot = new cItemIcon;
	if( mCardSlot->CreateBySkinName( "Enhanced_CardSlot", this, 2 ) == false )
		return false;

	/// hover 이벤트는 처리하지만 이동 불가 
	mItemSlot->SetUsedFlag( false, false );
	mItemSlot->SetSlotIndex( ENHANCED_ITEM_SLOT );
	mItemSlot->SetLinked( true );		/// 실제 아이템이아니라 인벤으로부터 참조했음을 세팅

	mCardSlot->SetUsedFlag( false, false );
	mCardSlot->SetSlotIndex( ENHANCED_CARD_SLOT );
	mCardSlot->SetLinked( true );
	return true;
}

void cEnhancedWindow::UpdateRect()
{
	cUIWindow::UpdateRect();
}

void cEnhancedWindow::OnRender( cUIFontItemKeeper* pKeeper )
{
	cUIWindow::OnRender( pKeeper );
}

void cEnhancedWindow::OnCommand( cUINode*, unsigned int id )
{
	switch( id )
	{
	case eUIID_GAME_ENHANCED_CLOSE:
		{
			 ITEMMAN->TryToEnhancedCancel();
		}
		break;
	case eUIID_GAME_ENHANCED_CANCEL:
		{
			ITEMMAN->TryToEnhancedCancel( false );
		}
		break;
	case eUIID_GAME_ENHANCED_START:
		if( mItemSlotIndex <= MAX_INVENTORY && mCardSlotIndex <= MAX_INVENTORY )
		{
			ITEMMAN->TryToEnhancedStart();
		}
		break;
	}
}

void cEnhancedWindow::OnGaugeEnd( cUINode* n , unsigned int )
{
	if( mpGaugeBar && n == mpGaugeBar )
	{
		if( ITEMMAN->TryToEnhanced( mItemSlotIndex, mCardSlotIndex ) == true )
		{
			if( mpGaugeBar )
				mpGaugeBar->ClearFront();
		}

		UIMAN->HideTip();
	}
}

bool cEnhancedWindow::AddItem( unsigned int slotIndex )
{
	if( mpStart->IsNetLock() == true )
		return false;

	if( slotIndex > MAX_INVENTORY )
		return false;

	/// 결과 숨기기
	HideResult();

	/// 강화 아이템 삽입
	cItem& item = ITEMMAN->GetItem( slotIndex );
	cItemDefine* define = item.GetDefine();
	if( !define )
	{
		assert(0);
		return false;
	}

	/// 무기, 방어구 타입 아니면 리턴
	if( !(define->GetType() == ITEM_WEAPON || define->GetType() == ITEM_WEAR) )
		return false;

	/// 강화단계 검사 ( 최대 강화넘으면 리턴 )
	if( item.GetEnhanceStep() == MAX_ITEM_ENHANCED )
		return false;

	/// 강화카드랑 비교
	if( mCardSlotIndex <= MAX_INVENTORY )
	{
		cItem& card = ITEMMAN->GetItem( mCardSlotIndex );
		cItemDefine* cardDefine = card.GetDefine();
		if( !cardDefine )
			return false;

		/// 무기 + 무기강화카드 아니면 리턴
		char cardSubType = cardDefine->GetSubType();
		if( define->GetType() == ITEM_WEAPON )
		{
			if( item.GetEnhanceStep() < ITEM_ENHANCED_LV1_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV1 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV2_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV2 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV3_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV3 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV4_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV4 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV5 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
		}
		else if( define->GetType() == ITEM_WEAR )
		{
			/// 방어구 + 방어구강화카드 아니면 리턴
			if( item.GetEnhanceStep() < ITEM_ENHANCED_LV1_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV1 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV2_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV2 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV3_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV3 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV4_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV4 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV5 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
		}
	}
	
	/// 
	if( mItemSlotIndex != slotIndex )
	{
		if( mItemSlotIndex <= MAX_INVENTORY )
		{
			cItem& oldItem = ITEMMAN->GetItem( mItemSlotIndex );
			oldItem.SetLock( false );
		}

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

	/// 아이템 이미지 체인지
	if( mItemSlot )
	{
		mItemSlot->SetItemIndex( item.GetIndex() );
		mItemSlot->ChangeImage( define->GetIconParam() );
		mItemSlot->SetTipType( define->GetTipType() );
	}
	else
	{
		assert(0);
	}

	return true;
}

///
bool cEnhancedWindow::AddCard( unsigned int slotIndex )
{
	if( mpStart->IsNetLock() == true )
		return false;

	if( slotIndex > MAX_INVENTORY )
		return false;

	/// 결과 숨기기
	HideResult();

	/// 강화 카드 삽입	
	cItem& item = ITEMMAN->GetItem( slotIndex );
	cItemDefine* cardDefine = item.GetDefine();
	if( !cardDefine )
	{
		assert(0);
		return false;
	}

	/// 강화카드가 아니면 리턴
	if( !( cardDefine->GetType() == ITEM_CARD && (cardDefine->GetSubType() >= ITEM_CARD_ENHANCE_WEAPON_LV1 && 
												  cardDefine->GetSubType() <= ITEM_CARD_ENHANCE_WEAR_LV5) ) )
		return false;

	/// 아이템이 채워져있을시, 무기, 방어구 분리
	if( mItemSlotIndex <= MAX_INVENTORY )
	{
		cItem& item = ITEMMAN->GetItem( mItemSlotIndex );
		cItemDefine* define = item.GetDefine();
		if( !define )
			return false;

		/// 무기 + 무기강화카드 아니면 리턴
		char cardSubType = cardDefine->GetSubType();
		if( define->GetType() == ITEM_WEAPON )
		{
			if( item.GetEnhanceStep() < ITEM_ENHANCED_LV1_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV1 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV2_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV2 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV3_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV3 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV4_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV4 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAPON_LV5 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
		}
		else if( define->GetType() == ITEM_WEAR )
		{
			/// 방어구 + 방어구강화카드 아니면 리턴
			if( item.GetEnhanceStep() < ITEM_ENHANCED_LV1_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV1 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV2_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV2 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV3_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV3 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else if( item.GetEnhanceStep() < ITEM_ENHANCED_LV4_END )
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV4 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
			else
			{
				if( cardSubType != ITEM_CARD_ENHANCE_WEAR_LV5 ) 
				{
					CHATMANAGER->AddSystemMsg( eSYSTEM_NORMAL, GAMERESOURCEMAN->GetGameText( 262 ) );
					return false;
				}
			}
		}
	}
	
	/// 
	if( mCardSlotIndex != slotIndex )
	{
		if( mCardSlotIndex <= MAX_INVENTORY )
		{
			cItem& oldCard = ITEMMAN->GetItem( mCardSlotIndex );
			oldCard.SetLock( false );
		}

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

	/// 카드 아이템 이미지 체인지
	if( mCardSlot )
	{
		mCardSlot->SetItemIndex( item.GetIndex() );
		mCardSlot->ChangeImage( cardDefine->GetIconParam() );
		mCardSlot->SetTipType( cardDefine->GetTipType() );
	}
	else
	{
		assert(0);
	}

	return true;
}

void cEnhancedWindow::OnNetLock( int lockTry )
{
	if( mpStart )
		mpStart->NetLock( lockTry );
}

void cEnhancedWindow::OnNetUnLock( int lockTry )
{
	if( mpStart )
		mpStart->NetUnLock( lockTry );
}

void cEnhancedWindow::OnIconHovered( cUINode* caller, unsigned int id, const cUIPos& pos )
{
	cIcon* hoverIcon = (cIcon*)caller;
	if( hoverIcon == mItemSlot )
	{
		if( mItemSlotIndex > MAX_INVENTORY )
			return;

		cItem& item = ITEMMAN->GetItem( mItemSlotIndex );
        UIMAN->ShowItemTip( eTIP_ITEM, pos, item.GetIndex(), mItemSlotIndex, 0 );
	}
	else
	{
		if( mCardSlotIndex > MAX_INVENTORY )
			return;

		cItem& item = ITEMMAN->GetItem( mCardSlotIndex );
		UIMAN->ShowItemTip( eTIP_ITEM, pos, item.GetIndex(), mCardSlotIndex, 0 );
	}
}

void cEnhancedWindow::HideResult()
{
	if( mpSuccess )
		mpSuccess->Hide();

	if( mpFailure )
		mpFailure->Hide();
}

void cEnhancedWindow::HideESC( bool onsound )
{
	ITEMMAN->TryToEnhancedCancel();
}

/// 결과 이미지 보여주기
void cEnhancedWindow::SetResult( bool success )
{
	if( success == true )
	{
		if( mpSuccess )
			mpSuccess->Show();
	}
	else
	{
		if( mpFailure )
			mpFailure->Show();
	}
}

void cEnhancedWindow::StartGauge()
{
	if( mpGaugeBar )
		mpGaugeBar->SetGaugeAni( 100 );
}

void cEnhancedWindow::StopGauge()
{
	if( mpGaugeBar )
		mpGaugeBar->ClearFront();
}