class ItemAutoPeelWnd extends UICommonAPI;

const TIMER_ID_ITEM_PEEL_DELAY = 1;
const TIMER_DELAY_ITEM_PEEL = 150;
const DIALOG_ID_ITEM_SET_COUNT = 1;
const RARE_ANOUNCE_LEVEL = 4;

struct ItemAutoPeelInfo
{
	var int targetItemSId;
	var string targetItemName;
	var INT64 remainPeelCnt;
	var INT64 totalPeelCnt;
	var bool isPeeling;
	var bool isPause;
	var bool isExpand;
	var bool isComplete;
	var int readyTargetItemSid;
	var INT64 readyTotalPeelCnt;
	var int maxAnounceLevel;
	var array<UIPacket._AutoPeelResultItem> normalItemInfos;
	var array<UIPacket._AutoPeelResultItem> rareItemInfos;
};

var ItemAutoPeelInfo _itemAutoPeelInfo;
var bool _is_waiting_close;
var bool _is_opened_dialog;
var WindowHandle Me;
var ButtonHandle expandBtn;
var ItemWindowHandle targetItemWnd;
var TextBoxHandle statusTextBox;
var StatusBarHandle statusBar;
var TextureHandle anounceLevelTex;
var CharacterViewportWindowHandle itemPeelEffect;
var EditBoxHandle testEditBox;
var L2UIInventoryObjectSimple iObject;

static function ItemAutoPeelWnd Inst()
{
	return ItemAutoPeelWnd(GetScript("ItemAutoPeelWnd"));
}

function Initialize()
{
	initControls();
	iObject = AddItemListenerSimple(0);
	iObject.DelegateOnUpdateItem = HandleUpdateTargetItem;
}

function initControls()
{
	local string ownerFullPath;

	ownerFullPath = m_hOwnerWnd.m_WindowNameWithFullPath;
	Me = GetWindowHandle(ownerFullPath);
	expandBtn = GetButtonHandle(ownerFullPath $ ".ExpandBTN");
	statusBar = GetStatusBarHandle(ownerFullPath $ ".ProgressBar");
	statusTextBox = GetTextBoxHandle(ownerFullPath $ ".ProgressBar_Text");
	targetItemWnd = GetItemWindowHandle(ownerFullPath $ ".BoxItem_itemWnd");
	anounceLevelTex = GetTextureHandle(ownerFullPath $ ".LvDot_tex");
	itemPeelEffect = GetCharacterViewportWindowHandle(ownerFullPath $ ".ChEffectViewport");
}

function UpdateProgressInfoControls()
{
	if(_itemAutoPeelInfo.isComplete == true)
	{
		statusTextBox.SetText(GetSystemString(898));		
	}
	else
	{
		if(_itemAutoPeelInfo.remainPeelCnt == 0)
		{
			statusTextBox.SetText(GetSystemString(14092));			
		}
		else
		{
			statusTextBox.SetText(MakeCostString(string(_itemAutoPeelInfo.remainPeelCnt)));
		}
	}
	statusBar.SetPoint(_itemAutoPeelInfo.remainPeelCnt, _itemAutoPeelInfo.totalPeelCnt);
	if(_itemAutoPeelInfo.maxAnounceLevel >= 4 && _itemAutoPeelInfo.isExpand == false)
	{
		anounceLevelTex.SetTexture(GetAnounceLevelTextureName(_itemAutoPeelInfo.maxAnounceLevel));
		anounceLevelTex.ShowWindow();		
	}
	else
	{
		anounceLevelTex.HideWindow();
	}
}

function UpdateExpandWnd()
{
	if(_itemAutoPeelInfo.isExpand == true)
	{
		expandBtn.HideWindow();
		class'ItemAutoPeelExpandWnd'.static.Inst().m_hOwnerWnd.ShowWindow();		
	}
	else
	{
		expandBtn.ShowWindow();
		class'ItemAutoPeelExpandWnd'.static.Inst().m_hOwnerWnd.HideWindow();
	}
}

function UpdateSimpleInvenWnd()
{
	if(_itemAutoPeelInfo.isExpand == true)
	{
		if(_itemAutoPeelInfo.isPeeling == true && _itemAutoPeelInfo.isPause == false)
		{
			ShowSimpleInven(false);			
		}
		else
		{
			ShowSimpleInven(true);
		}		
	}
	else
	{
		ShowSimpleInven(false);
	}
}

function UpdateTargetItemInfoControls()
{
	local ItemInfo targetItemInfo;

	if(class'UIDATA_INVENTORY'.static.FindItem(_itemAutoPeelInfo.targetItemSId, targetItemInfo))
	{
		targetItemInfo.bDisabled = 0;
		targetItemInfo.bShowCount = true;
		if(! targetItemWnd.SetItem(0, targetItemInfo))
		{
			targetItemWnd.AddItem(targetItemInfo);
			iObject.setId(targetItemInfo.Id);
		}		
	}
	else
	{
		targetItemWnd.Clear();
		iObject.setId();
		if(_itemAutoPeelInfo.targetItemSId > 0)
		{
			Rq_C_EX_STOP_ITEM_AUTO_PEEL();
			ResetTargetInfo();
		}
	}
}

function UpdateWnd()
{
	UpdateExpandWnd();
	UpdateSimpleInvenWnd();
}

function UpdateUIControls()
{
	UpdateTargetItemInfoControls();
	UpdateProgressInfoControls();
	class'ItemAutoPeelExpandWnd'.static.Inst().UpdateUIControls();
}

function RegisterItem(int itemSId, bool isAllItem)
{
	local INT64 ItemNum;

	if(_itemAutoPeelInfo.isPeeling == true && _itemAutoPeelInfo.isPause == false)
	{
		getInstanceL2Util().showGfxScreenMessage(GetSystemMessage(13666));
		return;
	}
	Me.ShowWindow();
	SetExpand(true);
	if(itemSId != 0)
	{
		ItemNum = GetItemNum(itemSId);
		_itemAutoPeelInfo.readyTargetItemSid = itemSId;
		if(isAllItem == true)
		{
			_itemAutoPeelInfo.readyTotalPeelCnt = 0;
			Rq_C_EX_READY_ITEM_AUTO_PEEL(itemSId);			
		}
		else
		{
			if(ItemNum > 1)
			{
				ShowItemCountDialog(itemSId);				
			}
			else
			{
				_itemAutoPeelInfo.readyTotalPeelCnt = 1;
				Rq_C_EX_READY_ITEM_AUTO_PEEL(itemSId);
			}
		}
	}
}

function StartItemAutoPeel()
{
	_itemAutoPeelInfo.isPeeling = true;
	_itemAutoPeelInfo.isPause = false;
	_itemAutoPeelInfo.isComplete = false;
	CheckAndContinueItemPeel();
	UpdateSimpleInvenWnd();
	UpdateUIControls();
}

function PauseItemAutoPeel()
{
	_itemAutoPeelInfo.isPeeling = true;
	_itemAutoPeelInfo.isPause = true;
	KillItemPeelDelayTimer();
	UpdateSimpleInvenWnd();
	UpdateUIControls();
}

function StopItemAutoPeel()
{
	_itemAutoPeelInfo.isPeeling = false;
	_itemAutoPeelInfo.isPause = false;
	KillItemPeelDelayTimer();
	UpdateSimpleInvenWnd();
	UpdateUIControls();
}

function CompleteItemAutoPeel()
{
	_itemAutoPeelInfo.isComplete = true;
	StopItemAutoPeel();
}

function CheckAndContinueItemPeel()
{
	if(_itemAutoPeelInfo.targetItemSId != 0 && _itemAutoPeelInfo.isPeeling == true && _itemAutoPeelInfo.isPause == false)
	{
		if(_itemAutoPeelInfo.remainPeelCnt == 0)
		{
			StopItemAutoPeel();			
		}
		else
		{
			Rq_C_EX_REQUEST_ITEM_AUTO_PEEL(_itemAutoPeelInfo.targetItemSId, _itemAutoPeelInfo.remainPeelCnt, _itemAutoPeelInfo.totalPeelCnt);
		}
	}
}

function AddResultItemInfo(array<UIPacket._AutoPeelResultItem> resultItemInfos)
{
	local int i;
	local UIPacket._AutoPeelResultItem tempInfo;

	for(i = 0; i < resultItemInfos.Length; i++)
	{
		tempInfo = resultItemInfos[i];
		if(tempInfo.AnnounceLevel >= 4)
		{
			_itemAutoPeelInfo.maxAnounceLevel = Max(_itemAutoPeelInfo.maxAnounceLevel, tempInfo.AnnounceLevel);
			AddRareResultInfo(tempInfo);
			// [Explicit Continue]
			continue;
		}
		AddNormalResultInfo(tempInfo);
	}
}

function AddRareResultInfo(UIPacket._AutoPeelResultItem resultItemInfo)
{
	local bool isFound;
	local int i;
	local UIPacket._AutoPeelResultItem tempInfo;

	for(i = 0; i < _itemAutoPeelInfo.rareItemInfos.Length; i++)
	{
		tempInfo = _itemAutoPeelInfo.rareItemInfos[i];
		if(tempInfo.nItemClassID == resultItemInfo.nItemClassID && tempInfo.Enchanted == ResultItemInfo.Enchanted)
		{
			isFound = true;
			tempInfo.nAmount = tempInfo.nAmount + resultItemInfo.nAmount;
			_itemAutoPeelInfo.rareItemInfos[i] = tempInfo;
			// [Explicit Break]
			break;
		}
	}
	if(isFound == false)
	{
		_itemAutoPeelInfo.rareItemInfos[_itemAutoPeelInfo.rareItemInfos.Length] = resultItemInfo;
		_itemAutoPeelInfo.rareItemInfos.Sort(OnItemSortCompare);
	}
}

function AddNormalResultInfo(UIPacket._AutoPeelResultItem resultItemInfo)
{
	local bool isFound;
	local int i;
	local UIPacket._AutoPeelResultItem tempInfo;

	for(i = 0; i < _itemAutoPeelInfo.normalItemInfos.Length; i++)
	{
		tempInfo = _itemAutoPeelInfo.normalItemInfos[i];
		if(tempInfo.nItemClassID == resultItemInfo.nItemClassID && tempInfo.Enchanted == ResultItemInfo.Enchanted)
		{
			isFound = true;
			tempInfo.nAmount = tempInfo.nAmount + resultItemInfo.nAmount;
			_itemAutoPeelInfo.normalItemInfos[i] = tempInfo;
			// [Explicit Break]
			break;
		}
	}
	if(isFound == false)
	{
		_itemAutoPeelInfo.normalItemInfos[_itemAutoPeelInfo.normalItemInfos.Length] = resultItemInfo;
	}
}

delegate int OnItemSortCompare(UIPacket._AutoPeelResultItem A, UIPacket._AutoPeelResultItem B)
{
	if(A.AnnounceLevel < B.AnnounceLevel)
	{
		return -1;		
	}
	else
	{
		return 0;
	}
}

function ResetTargetItem()
{
	if(_itemAutoPeelInfo.targetItemSId > 0)
	{
		Rq_C_EX_STOP_ITEM_AUTO_PEEL();
	}
}

function ResetTargetInfo()
{
	_itemAutoPeelInfo.targetItemSId = 0;
	_itemAutoPeelInfo.targetItemName = "";
	_itemAutoPeelInfo.remainPeelCnt = 0;
	_itemAutoPeelInfo.totalPeelCnt = 0;
	_itemAutoPeelInfo.isPeeling = false;
	_itemAutoPeelInfo.isPause = false;
}

function ResetResultInfo()
{
	_itemAutoPeelInfo.maxAnounceLevel = 0;
	_itemAutoPeelInfo.normalItemInfos.Length = 0;
	_itemAutoPeelInfo.rareItemInfos.Length = 0;
	_itemAutoPeelInfo.isComplete = false;
}

function ResetInfo()
{
	ResetTargetInfo();
	ResetResultInfo();
}

function SetExpand(bool isExpand)
{
	_itemAutoPeelInfo.isExpand = isExpand;
	UpdateExpandWnd();
	UpdateSimpleInvenWnd();
	UpdateProgressInfoControls();
}

function ShowSimpleInven(bool isShow)
{
	if(isShow == true)
	{
		class'ItemAutoPeelInvenWnd'.static.Inst().m_hOwnerWnd.ShowWindow();		
	}
	else
	{
		class'ItemAutoPeelInvenWnd'.static.Inst().m_hOwnerWnd.HideWindow();
	}
}

function ShowItemCountDialog(optional int forceSId)
{
	local ItemInfo targetItemInfo;

	if(forceSId > 0)
	{
		if(class'UIDATA_INVENTORY'.static.FindItem(forceSId, targetItemInfo) == false)
		{
			return;
		}		
	}
	else
	{
		if(class'UIDATA_INVENTORY'.static.FindItem(_itemAutoPeelInfo.targetItemSId, targetItemInfo) == false)
		{
			return;
		}
	}
	DialogHide();
	DialogSetReservedItemID(targetItemInfo.Id);
	DialogSetParamInt64(targetItemInfo.ItemNum);
	DialogSetID(1);
	DialogSetCancelD(1);
	DialogShow(DialogModalType_Modalless, DialogType_NumberPad, MakeFullSystemMsg(GetSystemMessage(13657), targetItemInfo.Name), string(self));
	_is_opened_dialog = true;
	class'DialogBox'.static.Inst().AnchorToOwner(0, 100);
	class'DialogBox'.static.Inst().DelegateOnCancel = OnItemCountDialogCancel;
	class'DialogBox'.static.Inst().DelegateOnOK = OnItemCountDialogConfirm;
	class'DialogBox'.static.Inst().DelegateOnHide = OnItemCountDialogHide;
	class'DialogBox'.static.Inst().SetDefaultAction(EDefaultCancel);
	getInstanceL2Util().ItemRelationWindowHide(getCurrentWindowName(string(self)), "AttributeEnchantWnd,AttributeRemoveWnd,UnrefineryWnd,CrystallizationWnd,ItemAttributeChangeWnd,ProgressBox,AlchemyMixCubeWnd");
	SetCountDialogModal(true);
}

function StartItemPeelDelayTimer()
{
	KillItemPeelDelayTimer();
	Me.SetTimer(TIMER_ID_ITEM_PEEL_DELAY, TIMER_DELAY_ITEM_PEEL);
}

function KillItemPeelDelayTimer()
{
	Me.KillTimer(TIMER_ID_ITEM_PEEL_DELAY);
}

function PlayItemPeelEffect()
{
	if(Me.IsShowWindow() == false)
	{
		return;
	}
	itemPeelEffect.ShowWindow();
	itemPeelEffect.SpawnEffect("LineageEffect2.ui_openbox");
}

function StopItemPeelEffect()
{
	itemPeelEffect.HideWindow();
}

function SetCountDialogModal(bool isModal)
{
	if(isModal == true)
	{
		class'ItemAutoPeelInvenWnd'.static.Inst().m_hOwnerWnd.DisableWindow();
		class'ItemAutoPeelExpandWnd'.static.Inst().ShowDisableWnd(true);
		Me.DisableWindow();		
	}
	else
	{
		class'ItemAutoPeelInvenWnd'.static.Inst().m_hOwnerWnd.EnableWindow();
		class'ItemAutoPeelExpandWnd'.static.Inst().ShowDisableWnd(false);
		Me.EnableWindow();
	}
	UpdateUIControls();
}

function SetTargetTotalPeelCnt(INT64 Count)
{
	local INT64 oldPeelCnt, newPeelCnt;

	if(_itemAutoPeelInfo.isPeeling == false)
	{
		oldPeelCnt = _itemAutoPeelInfo.totalPeelCnt;
		newPeelCnt = Min64(GetTargetItemNum(), Count);
		if(oldPeelCnt != newPeelCnt)
		{
			_itemAutoPeelInfo.totalPeelCnt = newPeelCnt;
			_itemAutoPeelInfo.remainPeelCnt = _itemAutoPeelInfo.totalPeelCnt;
			UpdateUIControls();
		}
	}
}

function CloseWindow()
{
	if(Me.IsShowWindow() == false)
	{
		return;
	}
	if(_itemAutoPeelInfo.targetItemSId > 0)
	{
		_is_waiting_close = true;
		Rq_C_EX_STOP_ITEM_AUTO_PEEL();		
	}
	else
	{
		Me.HideWindow();
	}
}

function string GetAnounceLevelTextureName(int anounceLevel)
{
	local int validAnounceLevel;

	validAnounceLevel = Min(anounceLevel, 4 + 2);
	switch(validAnounceLevel)
	{
		case 4:
			return "L2UI_NewTex.ItemAutoPeelWnd.ItemAutoPeelDOT_Lv4";
		case 4 + 1:
			return "L2UI_NewTex.ItemAutoPeelWnd.ItemAutoPeelDOT_Lv5";
		case 4 + 2:
			return "L2UI_NewTex.ItemAutoPeelWnd.ItemAutoPeelDOT_Lv6";
		default:
			return "";
	}
}

function INT64 GetItemNum(int itemSId)
{
	local ItemInfo tempInfo;

	if(class'UIDATA_INVENTORY'.static.FindItem(itemSId, tempInfo) == false)
	{
		return 0;		
	}
	else
	{
		return tempInfo.ItemNum;
	}
}

function INT64 GetTargetItemNum()
{
	local int TargetItemID;

	TargetItemID = _itemAutoPeelInfo.targetItemSId;
	if(TargetItemID == 0)
	{
		return 0;
	}
	return GetItemNum(TargetItemID);
}

function ItemAutoPeelInfo GetItemAutoPeelInfo()
{
	return _itemAutoPeelInfo;
}

function bool IsItemPeeling()
{
	if(_itemAutoPeelInfo.isPeeling == true && _itemAutoPeelInfo.isPause == false)
	{
		return true;
	}
	return false;
}

function bool IsItemReady()
{
	if(_itemAutoPeelInfo.targetItemSId > 0)
	{
		return true;
	}
	return false;
}

event OnLoad()
{
	SetClosingOnESC();
	Initialize();
}

event OnShow()
{
	itemPeelEffect.SetNPCInfo(19671);
	itemPeelEffect.SpawnNPC();
	ResetInfo();
	Me.SetFocus();
	UpdateUIControls();
	_is_waiting_close = false;
}

event OnHide()
{
	if(DialogIsMine())
	{
		DialogHide();
	}
	if(_itemAutoPeelInfo.targetItemSId > 0)
	{
		Rq_C_EX_STOP_ITEM_AUTO_PEEL();
	}
}

event OnDropItem(string strID, ItemInfo Info, int X, int Y)
{
	if(strID == "BoxItem_itemWnd")
	{
		if(class'UIDATA_ITEM'.static.IsDefaultActionPeel(Info.Id.ClassID) == false)
		{
			getInstanceL2Util().showGfxScreenMessage(GetSystemMessage(1960));
			return;
		}
		RegisterItem(Info.Id.ServerID, false);
	}
}

event OnDBClickItem(string strID, int Index)
{
	if(strID == "BoxItem_itemWnd")
	{
		if(_itemAutoPeelInfo.isPeeling == true && _itemAutoPeelInfo.isPause == false)
		{			
		}
		else
		{
			ResetTargetItem();
		}
	}
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "ExpandBTN":
			SetExpand(true);
			break;
	}
}

event OnTimer(int TimerID)
{
	if(TimerID == 1)
	{
		KillItemPeelDelayTimer();
		CheckAndContinueItemPeel();
	}
}

event OnRegisterEvent()
{
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_READY_ITEM_AUTO_PEEL));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_RESULT_ITEM_AUTO_PEEL));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_STOP_ITEM_AUTO_PEEL));
	RegisterEvent(EV_Restart);
}

event OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_PacketID(class'UIPacket'.const.S_EX_READY_ITEM_AUTO_PEEL):
			Rs_S_EX_READY_ITEM_AUTO_PEEL();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_RESULT_ITEM_AUTO_PEEL):
			Rs_S_EX_RESULT_ITEM_AUTO_PEEL();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_STOP_ITEM_AUTO_PEEL):
			Rs_S_EX_STOP_ITEM_AUTO_PEEL();
			break;
		case EV_Restart:
			StopItemAutoPeel();
			ResetInfo();
			break;
	}
}

event OnItemCountDialogConfirm()
{
	local INT64 reserved2;
	local int itemSId;
	local INT64 Number;

	if(DialogIsMine())
	{
		Number = int64(DialogGetString());
		if(Number < 1)
		{
			return;
		}
		reserved2 = DialogGetReservedInt2();
		itemSId = DialogGetReservedItemID().ServerID;
		_itemAutoPeelInfo.readyTotalPeelCnt = Number;
		_itemAutoPeelInfo.readyTargetItemSid = itemSId;
		Rq_C_EX_READY_ITEM_AUTO_PEEL(itemSId);
	}
}

event OnItemCountDialogCancel()
{
	if(DialogIsMine())
	{
		if(_is_opened_dialog == true)
		{
			_is_opened_dialog = false;
			SetCountDialogModal(false);
		}
	}
}

event OnItemCountDialogHide()
{
	if(DialogIsMine() == true)
	{
		if(_is_opened_dialog == true)
		{
			_is_opened_dialog = false;
			SetCountDialogModal(false);
		}
	}
}

function Rq_C_EX_READY_ITEM_AUTO_PEEL(int itemServerID)
{
	local array<byte> stream;
	local UIPacket._C_EX_READY_ITEM_AUTO_PEEL packet;

	packet.nItemSid = itemServerID;
	if(! class'UIPacket'.static.Encode_C_EX_READY_ITEM_AUTO_PEEL(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_READY_ITEM_AUTO_PEEL, stream);
}

function Rq_C_EX_REQUEST_ITEM_AUTO_PEEL(int itemServerID, INT64 remainPeelCnt, INT64 totalPeelCnt)
{
	local array<byte> stream;
	local UIPacket._C_EX_REQUEST_ITEM_AUTO_PEEL packet;

	packet.nItemSid = itemServerID;
	packet.nRemainPeelCount = remainPeelCnt;
	packet.nTotalPeelCount = totalPeelCnt;
	if(! class'UIPacket'.static.Encode_C_EX_REQUEST_ITEM_AUTO_PEEL(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_REQUEST_ITEM_AUTO_PEEL, stream);
}

function Rq_C_EX_STOP_ITEM_AUTO_PEEL()
{
	local array<byte> stream;
	local UIPacket._C_EX_STOP_ITEM_AUTO_PEEL packet;

	if(! class'UIPacket'.static.Encode_C_EX_STOP_ITEM_AUTO_PEEL(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_STOP_ITEM_AUTO_PEEL, stream);
}

function Rs_S_EX_READY_ITEM_AUTO_PEEL()
{
	local UIPacket._S_EX_READY_ITEM_AUTO_PEEL packet;
	local bool IsSuccess;
	local ItemInfo targetItemInfo;

	if(! class'UIPacket'.static.Decode_S_EX_READY_ITEM_AUTO_PEEL(packet))
	{
		return;
	}
	IsSuccess = bool(packet.bResult);
	if(IsSuccess == true)
	{
		_itemAutoPeelInfo.targetItemSId = packet.nItemSid;
		if(class'UIDATA_INVENTORY'.static.FindItem(_itemAutoPeelInfo.targetItemSId, targetItemInfo) == true)
		{
			_itemAutoPeelInfo.targetItemName = targetItemInfo.Name;
			if(_itemAutoPeelInfo.readyTotalPeelCnt == 0)
			{
				_itemAutoPeelInfo.totalPeelCnt = targetItemInfo.ItemNum;
				_itemAutoPeelInfo.remainPeelCnt = _itemAutoPeelInfo.totalPeelCnt;				
			}
			else
			{
				_itemAutoPeelInfo.totalPeelCnt = Min64(_itemAutoPeelInfo.readyTotalPeelCnt, targetItemInfo.ItemNum);
				_itemAutoPeelInfo.remainPeelCnt = _itemAutoPeelInfo.totalPeelCnt;
			}
		}
		ResetResultInfo();		
	}
	else
	{
		ResetTargetInfo();
	}
	_itemAutoPeelInfo.readyTargetItemSid = 0;
	_itemAutoPeelInfo.readyTotalPeelCnt = 0;
	UpdateUIControls();
}

function Rs_S_EX_RESULT_ITEM_AUTO_PEEL()
{
	local UIPacket._S_EX_RESULT_ITEM_AUTO_PEEL packet;
	local bool IsSuccess;

	if(! class'UIPacket'.static.Decode_S_EX_RESULT_ITEM_AUTO_PEEL(packet))
	{
		return;
	}
	IsSuccess = bool(packet.bResult);
	if(IsSuccess == true)
	{
		_itemAutoPeelInfo.remainPeelCnt = packet.nRemainPeelCount;
		_itemAutoPeelInfo.totalPeelCnt = packet.nTotalPeelCount;
		AddResultItemInfo(packet.vResultItemList);
		PlayItemPeelEffect();
		if(packet.nRemainPeelCount == 0 && packet.nTotalPeelCount > 0)
		{
			CompleteItemAutoPeel();
			getInstanceL2Util().showGfxScreenMessage(GetSystemMessage(13656));
			Rq_C_EX_STOP_ITEM_AUTO_PEEL();
			return;			
		}
		else
		{
			StartItemPeelDelayTimer();
		}		
	}
	else
	{
		if(_itemAutoPeelInfo.targetItemSId == 0)
		{
			StopItemAutoPeel();			
		}
		else
		{
			if(_itemAutoPeelInfo.remainPeelCnt > (GetTargetItemNum()))
			{
				_itemAutoPeelInfo.remainPeelCnt = GetTargetItemNum();
			}
			PauseItemAutoPeel();
		}
	}
	UpdateUIControls();
}

function Rs_S_EX_STOP_ITEM_AUTO_PEEL()
{
	local UIPacket._S_EX_STOP_ITEM_AUTO_PEEL packet;
	local bool IsSuccess;

	if(! class'UIPacket'.static.Decode_S_EX_STOP_ITEM_AUTO_PEEL(packet))
	{
		return;
	}
	IsSuccess = bool(packet.bResult);
	if(IsSuccess == true)
	{
		ResetTargetInfo();
		if(_is_waiting_close == true)
		{
			_is_waiting_close = false;
			Me.HideWindow();
			ResetInfo();
		}		
	}
	else
	{
		if(_is_waiting_close == true)
		{
		}
	}
	UpdateUIControls();
	UpdateWnd();
}

function HandleUpdateTargetItem(optional array<ItemInfo> iInfo, optional int Index)
{
	if(_itemAutoPeelInfo.targetItemSId > 0 && iInfo.Length > 0)
	{
		if(iInfo[0].ItemNum == 0)
		{
			UpdateUIControls();
		}
	}
}

event OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	CloseWindow();
}
