//----------------------------------------------------------------------------------------------------------------
//
// 아티팩트 인챈트 2018.05 추가
//
//----------------------------------------------------------------------------------------------------------------

class ArtifactEnchantWnd extends UICommonAPI;

const ANIM_SUCCESS  = 1;
const ANIM_FAIL     = 2;
const ANIM_PROGRESS = 3;

const TIMER_BUTTONDELAY_ID = 100221;
const MAX_USER_LEVEL                   = 99;

var WindowHandle  Me;

var AnimTextureHandle EnchantProgressAnim;

var TextBoxHandle Instruction_Txt;
var TextBoxHandle EnchantNotice_Txt;

var ItemWindowHandle MaterialSlot1_ItemWnd;
var ItemWindowHandle MaterialSlot2_ItemWnd;
var ItemWindowHandle MaterialSlot3_ItemWnd;
var ItemWindowHandle ArtifactItemSlot_ItemWnd;

var TextureHandle MaterialSlotBack1_Texture;
var TextureHandle MaterialSlotBack2_Texture;
var TextureHandle MaterialSlotBack3_Texture;
var TextureHandle ArtifactItemSlotBack_Texture;

var ButtonHandle Reset_Btn;
var ButtonHandle Enchant_Btn;
var ButtonHandle Close_Btn;


var TextureHandle MaterialSlot1_DropHighlight_Texure;
var TextureHandle MaterialSlot2_DropHighlight_Texure;
var TextureHandle MaterialSlot3_DropHighlight_Texure;
var TextureHandle ArtifactItemSlot_DropHighlight_Texure;

var ProgressCtrlHandle EnchantProgress;
var TextBoxHandle ProbabilityNum_Txt;
var TextBoxHandle Probability_Txt;

//var CharacterViewportWindowHandle EffectViewport;

var bool isProgress, isResult;
var int  nNeedMaterialCount;
var int  nNeedGroupID;

var ArtifactEnchantSubWnd ArtifactEnchantSubWndScript;

//----------------------------------------------------------------------------------------------------------------
// INIT
//----------------------------------------------------------------------------------------------------------------
function OnRegisterEvent()
{
	RegisterEvent( EV_Restart );

	RegisterEvent( EV_Enchant_Artifact_Result );
}

function OnLoad()
{
	SetClosingOnESC();

	Initialize();

	initControl();
}

function Initialize()
{
	ArtifactEnchantSubWndScript = ArtifactEnchantSubWnd(GetScript("ArtifactEnchantSubWnd"));

	Me = GetWindowHandle( "ArtifactEnchantWnd" );

	EnchantProgress    = GetProgressCtrlHandle( "ArtifactEnchantWnd.EnchantProgress" );

	EnchantProgressAnim = GetAnimTextureHandle( "ArtifactEnchantWnd.EnchantProgressAnim" );

	Instruction_Txt   = GetTextBoxHandle( "ArtifactEnchantWnd.Instruction_Txt" );
	EnchantNotice_Txt = GetTextBoxHandle( "ArtifactEnchantWnd.EnchantNotice_Txt" );

	ArtifactItemSlot_ItemWnd = GetItemWindowHandle( "ArtifactEnchantWnd.ArtifactItemSlot_ItemWnd" );

	MaterialSlot1_ItemWnd = GetItemWindowHandle( "ArtifactEnchantWnd.MaterialSlot1_ItemWnd" );
	MaterialSlot2_ItemWnd = GetItemWindowHandle( "ArtifactEnchantWnd.MaterialSlot2_ItemWnd" );
	MaterialSlot3_ItemWnd = GetItemWindowHandle( "ArtifactEnchantWnd.MaterialSlot3_ItemWnd" );

	MaterialSlotBack1_Texture = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlotBack1_Texture" );
	MaterialSlotBack2_Texture = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlotBack2_Texture" );
	MaterialSlotBack3_Texture = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlotBack3_Texture" );

	ArtifactItemSlotBack_Texture = GetTextureHandle( "ArtifactEnchantWnd.ArtifactItemSlotBack_Texture" );

	Enchant_Btn = GetButtonHandle( "ArtifactEnchantWnd.Enchant_Btn" );
	Reset_Btn   = GetButtonHandle( "ArtifactEnchantWnd.Reset_Btn" );	
	Close_Btn   = GetButtonHandle( "ArtifactEnchantWnd.Close_Btn" );

	MaterialSlot1_DropHighlight_Texure = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlot1_DropHighlight_Texure" );
	MaterialSlot2_DropHighlight_Texure = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlot2_DropHighlight_Texure" );
	MaterialSlot3_DropHighlight_Texure = GetTextureHandle( "ArtifactEnchantWnd.MaterialSlot3_DropHighlight_Texure" );
	ArtifactItemSlot_DropHighlight_Texure = GetTextureHandle( "ArtifactEnchantWnd.ArtifactItemSlot_DropHighlight_Texure" );

	//EffectViewport = GetCharacterViewportWindowHandle("ArtifactEnchantWnd.EffectViewport");

	//EffectViewport.SetNPCInfo( 19671) ;
	//EffectViewport.SetUISound( true); //IsUISound	
	Probability_Txt = GetTextBoxHandle("ArtifactEnchantWnd.Probability_Txt");
	ProbabilityNum_Txt = GetTextBoxHandle("ArtifactEnchantWnd.ProbabilityNum_Txt");
}


function initControl()
{
	ResetUI();

	EnchantProgress.SetProgressTime(1500);
	EnchantProgress.SetPos(0);
	EnchantProgress.Reset();
}

//----------------------------------------------------------------------------------------------------------------
// OnEvent
//----------------------------------------------------------------------------------------------------------------

function OnEvent(int Event_ID, string param)
{
	//Debug( "OnEvent " @ Event_ID  @ param) ;
	if (Event_ID == EV_Restart) 
	{
		initControl();
	}
	else if (Event_ID == EV_Enchant_Artifact_Result) 
	{
		Debug("EV_Enchant_Artifact_Result" @ param);
		resultHandler(param);
	}
}

function OnTimer(int TimerID)
{
	if(TimerID == TIMER_BUTTONDELAY_ID)
	{
		// 계속 버튼 활성화
		Enchant_Btn.EnableWindow();
		Me.KillTimer( TIMER_BUTTONDELAY_ID );
	}
}

function resultHandler(string Param)
{
	local int nResult, nEnchant;
	local ItemInfo resultItemInfo;

	// Result: 결과(0:성공, 1:실패, 2:재료의 조건이 맞지 않음)
	ParseInt(Param, "Result", nResult);
	ParseInt(Param, "Enchant", nEnchant);

	isResult = true;

	// 성공 실패 관계 없이 초기화 버튼 활성화
	Reset_Btn.EnableWindow();

	// 성공이라면..
	if(nResult == 0)
	{
		// 메인슬롯 아이템 Info를 가져와서..
		if(getSlot(0).GetItemNum() > 0)	
		{
			// 재료 아이템 숨기기
			MaterialSlot1_ItemWnd.HideWindow();
			MaterialSlot2_ItemWnd.HideWindow();
			MaterialSlot3_ItemWnd.HideWindow();

			getSlot(0).GetItem(0, resultItemInfo);
			getSlot(0).Clear();
			
			resultItemInfo.Enchanted = nEnchant;

			getSlot(0).SetItem( 0, resultItemInfo );

			getSlot(0).AddItem(resultItemInfo);

			Debug("resultItemInfo 인챈트 " @ resultItemInfo.Enchanted);

			// 강화 성공
			Instruction_Txt.SetText(GetSystemString(3885));

			// 계속 버튼을 눌러 강화를 다시 진행할 수 있습니다.
			EnchantNotice_Txt.SetText(GetSystemString(3890));

			// 버튼 계속으로 변경
			Enchant_Btn.SetNameText( GetSystemString(3135) );	// 3135 계속, 5005 강화
			
			playEffectAnim(ANIM_SUCCESS);

			// 버튼 비활성화 해제
			Me.SetTimer(TIMER_BUTTONDELAY_ID, 1000);
		}
	}
	else if(nResult == 1) 
	{
		Debug("강화 실패!");

		// 재료 아이템 숨기기
		MaterialSlot1_ItemWnd.HideWindow();
		MaterialSlot2_ItemWnd.HideWindow();
		MaterialSlot3_ItemWnd.HideWindow();

		// 강화 실패
		Instruction_Txt.SetText(GetSystemString(3886));

		// 계속 버튼을 눌러 강화를 다시 진행할 수 있습니다.
		EnchantNotice_Txt.SetText(GetSystemString(3890));

		// 버튼 계속으로 변경
		Enchant_Btn.SetNameText( GetSystemString(3135) );	// 3135 계속, 5005 강화

		playEffectAnim(ANIM_FAIL);

		// 버튼 비활성화 해제
		Me.SetTimer(TIMER_BUTTONDELAY_ID, 1000);
	}
	else
	{
		 Debug("오류 : 재료가 잘못되었거나 문제가 생겨서 아티팩트 강화에 실패한 경우");
		 ResetUI();
		 AddSystemMessage(4559);
		 Me.HideWindow();
	}	
}

// 모션 재생이 끝날 때 이벤트 처리
function OnTextureAnimEnd( AnimTextureHandle a_WindowHandle )
{
	local itemInfo info;
	local array<int> materialServerIDArray;
	local int i;

	EnchantProgressAnim.Stop();
	EnchantProgressAnim.HideWindow();

	switch ( a_WindowHandle )
	{
		case EnchantProgressAnim:
			if (isProgress)
			{
				isProgress = false;
				//Debug("RequestEnchantItem" @ SelectItemInfo.ID.ClassID @ SelectHelperItemInfo.ID.ClassID);
				//class'EnchantAPI'.static.RequestEnchantItem(SelectItemInfo.ID, SelectHelperItemInfo.ID);
				i = 1;
				// 재료 1,2,3 를 뒤져서 슬롯에 값이 있으면 배열에 추가 
				i = addMetialArr(i, materialServerIDArray);
				i = addMetialArr(i, materialServerIDArray);
				i = addMetialArr(i, materialServerIDArray);

				//RequestEnchantArtifact(
				if(getSlot(0).GetItemNum() > 0)
				{
					getSlot(0).GetItem(0, info);

					RequestEnchantArtifact(info.Id.ServerID, materialServerIDArray);

					Debug("----> Call Api RequestEnchantArtifact " @ info.Id.ServerID @", len: "@materialServerIDArray.Length);
					for(i = 0; i < materialServerIDArray.Length; i++)
					{						
						Debug("materialServerIDArray:  " @ i  @ materialServerIDArray[i]);
					}

					Enchant_Btn.DisableWindow();
				}
				//getSlot(0).GetItem(0, info)
			}
					
		break;
	}
}

function OnShow()
{
	getInstanceL2Util().ItemRelationWindowHide(getCurrentWindowName(String(self)));
	GetWindowHandle("ArtifactEnchantSubWnd").ShowWindow();

	//EffectViewport.SpawnNPC();
	//EffectViewport.ShowNPC(0.1f);
	//EffectViewport.SpawnEffect( "LineageEffect2.bg_astatine_portal") ;

	setSlotHighlightFocus(0);

	ArtifactEnchantSubWndScript.setEnable(true);
	ArtifactEnchantSubWndScript.syncInventory(-1, -1);
}

function OnHide()
{
	ResetUI();
}

//----------------------------------------------------------------------------------------------------------------
// OnClickButton
//----------------------------------------------------------------------------------------------------------------
function OnClickButton( string Name )
{
	switch( Name )
	{
		case "Reset_Btn":
			OnReset_BtnClick();
			break;

		case "Enchant_Btn":
			OnEnchant_BtnClick();
			break;

		case "Close_Btn":
			OnClose_BtnClick();
			break;
	}
}

//----------------------------------------------------------------------------------------------------------------
// ClickHandler
//----------------------------------------------------------------------------------------------------------------

function OnReset_BtnClick()
{
	ResetUI();
	ArtifactEnchantSubWndScript.syncInventory(-1, -1);
}

function OnClose_BtnClick()
{	
	if(isProgress)
	{
		progressBarCancel();
	}
	else
	{
		ResetUI();
		//ArtifactEnchantSubWndScript.syncInventory(-1, -1);
		Me.HideWindow();
	}
}

function OnEnchant_BtnClick()
{
	local ItemInfo info;

	// 결과 상태에서, 계속을 누른 경우
	if (isResult)
	{
		if(getSlot(0).GetItemNum() > 0)
		{
			getSlot(0).GetItem(0, info);
			getSlot(0).Clear();

			ResetUI();
			// 다시 첫번째 슬롯에 넣기
			dropProcess(info);
		}		
	}
	else
	{
		// 서브창 비활성화
		ArtifactEnchantSubWndScript.setEnable(false);

		// 포커싱 텍스쳐 숨기기
		setSlotHighlightFocus(-1);

		// 인챈트 시작 준비 모션
		playEffectAnim(ANIM_PROGRESS);

		//DropHighlight_enchantitem.SetTexture("L2UI_ch3.RefineryWnd.refineslot2");
		//DropHighlight_enchantscript.SetTexture("L2UI_ch3.RefineryWnd.refineslot2");
		//DropHighlight_CloverItem.SetTexture("L2UI_ch3.RefineryWnd.refineslot2");	
		
		// 취소 가능 대기 진행바 시작
		EnchantProgress.SetProgressTime(1500);
		EnchantProgress.SetPos(0);
		EnchantProgress.Reset();

		EnchantProgress.Start();
		
		// 배치는 아래와 같이 되어 있다. 
		// 1   2
		// 아티팩트
		//   3
		
		// 자료 이동 모션 
		MaterialSlot1_ItemWnd.EnableTick();
		MaterialSlot2_ItemWnd.EnableTick();
		MaterialSlot3_ItemWnd.EnableTick();

		MaterialSlot1_ItemWnd.ClearAnchor();
		MaterialSlot2_ItemWnd.ClearAnchor();
		MaterialSlot3_ItemWnd.ClearAnchor();

		MaterialSlot1_ItemWnd.Move( 72,  29, 1.5f);
		MaterialSlot2_ItemWnd.Move(-64,  29, 1.5f);
		MaterialSlot3_ItemWnd.Move(  0, -71, 1.5f);

		// 버튼 이름 변경
		Close_Btn.SetNameText( GetSystemString(141) ); // 취소 141,  닫기 646

		Enchant_Btn.SetNameText( GetSystemString(5005) );	// 3135 계속, 5005 강화

		isProgress = true;
		setEnchantButtonCheck();
	}
}

// 초기화, 강화 버튼 상태
function setEnchantButtonCheck()
{
	//local ItemInfo info;
	local int filledSlotCount, i;
	//local bool bEnable;

	// 진행중이면 무조건 버튼 비활성화
	if (isProgress) 
	{
		Enchant_Btn.DisableWindow();
		Reset_Btn.DisableWindow();
		return;
	}

	// 재료슬롯만 체크
	// 슬롯이 차있는 수를 체크 1,2,3 , 3가지 재료만 체크 (0)은 인챈트할 대상
	for(i = 1; i < 4; i++)
	{
		if(getSlot(i).GetItemNum() > 0)
		{
			filledSlotCount++;
		}
	}

	// 메인 슬롯도 체크
	if (filledSlotCount > 0 || getSlot(0).GetItemNum() > 0)
	{
		// 실패 시 현재 수치가 유지 됩니다.
		Instruction_Txt.SetText(GetSystemString(3884));
		Reset_Btn.EnableWindow();
	}
	else
	{
		Reset_Btn.EnableWindow();
	}
	// 필요한 재료가 채워 졌으면..
	if (nNeedMaterialCount == filledSlotCount)
	{
		// 아래의 강화 버튼을 눌러 아티팩트 강화를 시작하세요.
		EnchantNotice_Txt.SetText(GetSystemString(3889));
		Enchant_Btn.EnableWindow();
	}
	else
	{
		Enchant_Btn.DisableWindow();
	}
}

function setInitButtonCheck()
{

}

// 서버에 보낼 재료 슬롯에서 꺼내서, 서버 아이디 배열에 넣기
function int addMetialArr(int i, out array<int> materialServerIDArray)
{
	local itemInfo info;
	if(getSlot(i).GetItemNum() > 0)
	{					
		getSlot(i).GetItem(0, info);
		if (info.Id.ServerID > 0)
		{
			materialServerIDArray[i - 1] = info.Id.ServerID;
			i++;

			Debug("보낼 Id.ClassID" @ info.Id.ClassID);
		}
	}

	return i;
}

function ResetUI()
{
	Reset_Btn.DisableWindow();
	// 서브창 비활성화
	ArtifactEnchantSubWndScript.setEnable(true);

	EnchantProgress.SetPos(0);
	EnchantProgress.Reset();

	EnchantProgressAnim.Stop();
	EnchantProgressAnim.HideWindow();

	ArtifactItemSlot_ItemWnd.Clear();
	
	MaterialSlot1_ItemWnd.Clear();
	MaterialSlot2_ItemWnd.Clear();
	MaterialSlot3_ItemWnd.Clear();

	MaterialSlot1_ItemWnd.ShowWindow();
	MaterialSlot2_ItemWnd.ShowWindow();
	MaterialSlot3_ItemWnd.ShowWindow();
	
	MaterialSlot1_ItemWnd.DisableTick();
	MaterialSlot2_ItemWnd.DisableTick();
	MaterialSlot3_ItemWnd.DisableTick();

	// 위치 초기화
	MaterialSlot1_ItemWnd.MoveC(94 ,  106);
	MaterialSlot2_ItemWnd.MoveC(231,  106);
	MaterialSlot3_ItemWnd.MoveC(167,  205);

	MaterialSlotBack1_Texture.ShowWindow();
	MaterialSlotBack2_Texture.ShowWindow();
	MaterialSlotBack3_Texture.ShowWindow();

	setSlotHighlightFocus(0);

	Me.KillTimer( TIMER_BUTTONDELAY_ID );
	Enchant_Btn.DisableWindow();

	// 강화, 텍스트 변경
	Enchant_Btn.SetNameText(GetSystemString(5005));

	// 닫기, 텍스트 변경
	Close_Btn.SetNameText(GetSystemString(646));

	isProgress = false;

	isResult = false;

	// 아무것도 안나오게.
	Instruction_Txt.SetText("");

	// 강화할 아이템을 등록 해주세요.
	EnchantNotice_Txt.SetText(GetSystemString(3887));
	Probability_Txt.HideWindow();
	ProbabilityNum_Txt.SetText("");
}

function progressBarCancel()
{
	isProgress = false;

	EnchantProgress.SetPos(0);
	EnchantProgress.Reset();

	EnchantProgressAnim.Stop();
	EnchantProgressAnim.HideWindow();

	// 위치 초기화
	MaterialSlot1_ItemWnd.DisableTick();
	MaterialSlot2_ItemWnd.DisableTick();
	MaterialSlot3_ItemWnd.DisableTick();

	MaterialSlot1_ItemWnd.MoveC(94, 106);
	MaterialSlot2_ItemWnd.MoveC(231, 106);
	MaterialSlot3_ItemWnd.MoveC(167, 205);

	// 강화, 텍스트 변경
	Enchant_Btn.SetNameText(GetSystemString(5005));
	//Enchant_Btn.EnableWindow();

	// 강화 버튼 활성화 여부 체크 
	setEnchantButtonCheck();

	// 닫기, 텍스트 변경
	Close_Btn.SetNameText(GetSystemString(646));

	// 서브창 해제
	ArtifactEnchantSubWndScript.setEnable(true);
}

function OnRClickItem( String strID, int index )
{
	OnDBClickItem(strID, index);
}

function OnDBClickItem( string ControlName, int index )
{
	local ItemInfo info;
	local ItemWindowHandle targetItemWnd;
	//SubWnd_Item1.GetItem(index, info);

	if (isProgress) return;
	if (isResult) return;

	switch(ControlName)
	{
		case "MaterialSlot1_ItemWnd" :
			 targetItemWnd = MaterialSlot1_ItemWnd;
			 break;
		case "MaterialSlot2_ItemWnd" :
			 targetItemWnd = MaterialSlot2_ItemWnd;
			 break;
		case "MaterialSlot3_ItemWnd" :
			 targetItemWnd = MaterialSlot3_ItemWnd;
			 break;
		case "ArtifactItemSlot_ItemWnd" :
			 //targetItemWnd = ArtifactItemSlot_ItemWnd;
			 ResetUI();
			 ArtifactEnchantSubWndScript.syncInventory(-1, -1);
			 return;			 
	}

	if (ControlName == "") return;
	
	targetItemWnd.GetItem(0, info);
	
	//Debug("info.Id.ClassID" @ info.Id.ClassID);

	if (info.Id.ClassID > 0) 
	{
		targetItemWnd.Clear();
		// 수정해야..

		ArtifactEnchantSubWndScript.syncInventory(nNeedGroupID, minArtifactEnchantNum());

		//ArtifactEnchantSubWndScript.syncInventory(-1, -1);

		setSlotHighlightFocus(findEmptySlotIndex());
	}	
}


function OnDropItem( String a_WindowID, ItemInfo a_ItemInfo, int X, int Y)
{	
	//if (a_ItemInfo.ShortcutType == EShortCutItemType.SCIT_MACRO) return;
	//Debug("a_ItemInfo.DragSrcName" @ a_ItemInfo.DragSrcName);

	if (a_ItemInfo.DragSrcName != "SubWnd_Item1") return;
	
	dropProcess(a_ItemInfo);
}

function dropProcess(itemInfo a_ItemInfo)
{
	local int emptySlotIndex;
	local int resultProb;
	// 빈슬롯 찾기
	emptySlotIndex = findEmptySlotIndex();

	// Debug("빈슬롯 번호" @ emptySlotIndex);

	if(emptySlotIndex <= -1) return;// emptyItemID;
	
	// 첫번째 슬롯이 꼽혔으면..
	// 아티팩트 인챈트 조건을 가져옴
	if(emptySlotIndex == 0)
	{
		Debug("--------------------------------------------------------------------------");
		class'UIDATA_ARTIFACT'.static.GetArtifactEnchantCondition(a_ItemInfo.Id.ClassID, a_ItemInfo.Enchanted, nNeedGroupID, nNeedMaterialCount, resultProb);
		Debug("메인 슬롯 강화 대상 아이템: " @ a_ItemInfo.Name @ a_ItemInfo.Id.ClassID @ a_ItemInfo.Enchanted);
		Debug("groupID       : " @ nNeedGroupID);
		Debug("materialCount : " @ nNeedMaterialCount);
		Debug("resultProb    : " @ resultProb);
		Debug("--------------------------------------------------------------------------");
		Probability_Txt.ShowWindow();
		ProbabilityNum_Txt.SetText(string(ResultProb) $ "%");
	}

	//class'UIDATA_ARTIFACT'.static.GetArtifactMinEnchantMaterial(

	// Debug("resultProb      : " @ resultProb);    //성공 활율인데 사용 안함.
	
	// 빈슬롯에 꼽는다.
	getSlot(emptySlotIndex).AddItem(a_ItemInfo);

	// 재료 슬롯 활성화, 비활성화 설정.
	setActiveMaterialSlot(nNeedMaterialCount);

	//GetArtifactMinEnchantMaterial

	ArtifactEnchantSubWndScript.syncInventory(nNeedGroupID, minArtifactEnchantNum());

	//Debug("- 추가" @ a_ItemInfo.Name);	
	//Debug("- findEmptySlotIndex()" @ findEmptySlotIndex());
	setSlotHighlightFocus(findEmptySlotIndex());

	// 강화 버튼 활성화 여부 체크 
	setEnchantButtonCheck();
}

//----------------------------------------------------------------------------------------------------------------
// UTIL
//----------------------------------------------------------------------------------------------------------------

function int minArtifactEnchantNum()
{
	local ItemInfo info;
	local int minEnchantNum;

	minEnchantNum = -1;

	// 메인 슬롯의 아이템 정보를 얻어와서 요구 하는 최소 자료 인챈트 수치를 얻는다.
	if(getSlot(0).GetItemNum() > 0)
	{
		getSlot(0).GetItem(0, info);
		minEnchantNum = class'UIDATA_ARTIFACT'.static.GetArtifactMinEnchantMaterial(info.Enchanted);

		//Debug("---info.Id.ClassID" @ info.Id.ClassID);
		//Debug("---minEnchantNum: " @ minEnchantNum);
	}

	return minEnchantNum;
}

// 재료 슬롯 활성화 
function setActiveMaterialSlot (int activeMaterialCount)
{
	getSlot(3).DisableWindow();
	getSlot(2).DisableWindow();
	getSlot(1).DisableWindow();

	MaterialSlotBack1_Texture.HideWindow();
	MaterialSlotBack2_Texture.HideWindow();
	MaterialSlotBack3_Texture.HideWindow();

	// Debug(" 재료 슬롯 활성화 세팅.");
	switch(activeMaterialCount)
	{
		case 3 : getSlot(3).EnableWindow(); MaterialSlotBack3_Texture.ShowWindow();
		case 2 : getSlot(2).EnableWindow(); MaterialSlotBack2_Texture.ShowWindow();
		case 1 : getSlot(1).EnableWindow(); MaterialSlotBack1_Texture.ShowWindow();
	}
}

// 빈슬롯 번호 0이 가운데 메인, 1 2, 3 재료 
function int findEmptySlotIndex()
{
	
	if(ArtifactItemSlot_ItemWnd.GetItemNum() <= 0 && ArtifactItemSlot_ItemWnd.IsEnableWindow()) return 0;
	else if(MaterialSlot1_ItemWnd.GetItemNum() <= 0 && MaterialSlot1_ItemWnd.IsEnableWindow()) return 1;
	else if(MaterialSlot2_ItemWnd.GetItemNum() <= 0 && MaterialSlot2_ItemWnd.IsEnableWindow()) return 2;
	else if(MaterialSlot3_ItemWnd.GetItemNum() <= 0 && MaterialSlot3_ItemWnd.IsEnableWindow()) return 3;
	
	return -1;
}


// 0이 메인, 재료 1,2,3
function setSlotHighlightFocus(int slotIndex)
{
	local int limitEnchantNum;

	MaterialSlot1_DropHighlight_Texure.HideWindow();
	MaterialSlot2_DropHighlight_Texure.HideWindow();
	MaterialSlot3_DropHighlight_Texure.HideWindow();
	ArtifactItemSlot_DropHighlight_Texure.HideWindow();
	
	limitEnchantNum = minArtifactEnchantNum();
	switch(slotIndex)
	{
		// 1,  2
		//   3    재료 슬롯 
		case 1: if (MaterialSlot1_ItemWnd.IsEnableWindow()) 
				{
					MaterialSlot1_DropHighlight_Texure.ShowWindow(); 
					
					// +$s1 이상의 재료가 필요 합니다.
					if(limitEnchantNum > 0)
						EnchantNotice_Txt.SetText(MakeFullSystemMsg(GetSystemMessage(5217), string(limitEnchantNum))); 
					else						
						EnchantNotice_Txt.SetText(GetSystemString(3888)); // 강화할 재료를 등록해주세요.					
				}
			    break;

		case 2: if (MaterialSlot2_ItemWnd.IsEnableWindow()) 
				{
					MaterialSlot2_DropHighlight_Texure.ShowWindow(); 

					// +$s1 이상의 재료가 필요 합니다.
					if(limitEnchantNum > 0)
						EnchantNotice_Txt.SetText(MakeFullSystemMsg(GetSystemMessage(5217), string(limitEnchantNum))); 
					else						
						EnchantNotice_Txt.SetText(GetSystemString(3888)); // 강화할 재료를 등록해주세요.					
				}
			    break;

		case 3: if (MaterialSlot3_ItemWnd.IsEnableWindow()) 
				{
					MaterialSlot3_DropHighlight_Texure.ShowWindow(); 

					// +$s1 이상의 재료가 필요 합니다.
					if(limitEnchantNum > 0)
						EnchantNotice_Txt.SetText(MakeFullSystemMsg(GetSystemMessage(5217), string(limitEnchantNum))); 
					else						
						EnchantNotice_Txt.SetText(GetSystemString(3888)); // 강화할 재료를 등록해주세요.					
				}
			    break;

		// 가운데 메인 슬롯
		case 0: ArtifactItemSlot_DropHighlight_Texure.ShowWindow(); 
				// 강화할 아이템을 등록해주세요.
				EnchantNotice_Txt.SetText(GetSystemString(3887));
			    break;
	}

}

// 0 ~ 3 강화 창의 슬롯 4개의 itemInfo를 리턴 하는 용도 
function ItemInfo getSlotItemInfo(int slotIndex)
{  
	local ItemInfo tmInfo;
	 
	switch(slotIndex)
	{
		case 1: if(MaterialSlot1_ItemWnd.GetItemNum() > 0) MaterialSlot1_ItemWnd.GetItem(0, tmInfo); break;
		case 2: if(MaterialSlot2_ItemWnd.GetItemNum() > 0) MaterialSlot2_ItemWnd.GetItem(0, tmInfo); break;
		case 3:	if(MaterialSlot3_ItemWnd.GetItemNum() > 0) MaterialSlot3_ItemWnd.GetItem(0, tmInfo); break;
		case 0:	if(ArtifactItemSlot_ItemWnd.GetItemNum() > 0) ArtifactItemSlot_ItemWnd.GetItem(0, tmInfo); break;

		default : Debug("** 경고 : ArtifactEnchantWnd 의 getSlotItemInfo의 입력값이 잘못됨 :" @ slotIndex);
	}

	return tmInfo;
}

// 0 ~ 3 강화 창의 슬롯 4개의 itemInfo를 리턴 하는 용도 
function ItemWindowHandle getSlot(int slotIndex)
{  
	local ItemWindowHandle tm;

	switch(slotIndex)
	{
		case 1: tm = MaterialSlot1_ItemWnd; break;
		case 2: tm = MaterialSlot2_ItemWnd; break;
		case 3:	tm = MaterialSlot3_ItemWnd; break;
		case 0:	tm = ArtifactItemSlot_ItemWnd; break;

		default : Debug("** 경고 : ArtifactEnchantWnd 의 getSlot의 입력값이 잘못됨 :" @ slotIndex);
	}

	return tm;
}

// 현재 슬롯에 꼽아서 사용 중인 아이템 과 같은게 있나 체크 , 서브 인벤에서 사용
function bool externalCheckUsingItem(ItemInfo info)
{
	local bool rValue;

	local int i;
	
	for (i = 0; i < 4; i++)
	{
		if (getSlotItemInfo(i).Id == Info.Id) rValue = true;
	}

	return rValue;
}

// 애님 텍스쳐를 플레이 시켜 준다
function playEffectAnim(int animType)
{
	EnchantProgressAnim.SetLoopCount( 1 );

	if (animType == ANIM_SUCCESS)
	{
		Playsound("ItemSound3.enchant_success");
		EnchantProgressAnim.SetTexture("l2ui_ct1.ItemEnchant_DF_Effect_Success_00");
		EnchantProgressAnim.Stop();
		EnchantProgressAnim.Play();
	}
	else if (animType == ANIM_FAIL)
	{
		Playsound("ItemSound3.enchant_fail");
		EnchantProgressAnim.SetTexture("l2ui_ct1.ItemEnchant_DF_Effect_Failed_01");
		EnchantProgressAnim.Stop();
		EnchantProgressAnim.Play();		
	}
	else // if (animType == ANIM_PROGRESS)
	{
		// 인챈트 시작 준비 모션
		Playsound("ItemSound3.enchant_process");	
		EnchantProgressAnim.SetTexture("l2ui_ct1.ItemEnchant_DF_Effect_Loading_01");
		EnchantProgressAnim.Stop();
		EnchantProgressAnim.Play();	
	}

	EnchantProgressAnim.ShowWindow();
}

/**
 * 윈도우 ESC 키로 닫기 처리 
 * "Esc" Key
 ***/
function OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	GetWindowHandle( getCurrentWindowName(string(Self))).HideWindow();
}
defaultproperties
{
}
