class ShopLcoinCraftMinimizeWnd extends UICommonAPI
	dependson(ShopLcoinCraftWnd);

var ShopLcoinCraftWnd.AutoCraftInfo _autoCraftInfo;
var TextBoxHandle cntTextBox;
var TextBoxHandle descTextBox;
var ButtonHandle CancelBtn;
var ButtonHandle maximizeBtn;
var ShopLcoinCraftWnd craftWndScript;
var EffectViewportWndHandle craftEffect;
var ProgressCtrlHandle ProgressBar;
var WindowHandle Me;
var array<WindowHandle> slotWndArray;
var array<int> slotXPosArray;
var bool _isUserCancel;

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

function Initialize()
{
	InitControls();
	craftWndScript = ShopLcoinCraftWnd(GetScript("ShopLcoinCraftWnd"));	
}

function InitControls()
{
	local string ownerFullPath;
	local int i;

	ownerFullPath = m_hOwnerWnd.m_WindowNameWithFullPath;
	Me = GetWindowHandle(ownerFullPath);
	cntTextBox = GetTextBoxHandle(ownerFullPath $ ".Num_Txt");
	descTextBox = GetTextBoxHandle(ownerFullPath $ ".Desc_Txt");
	CancelBtn = GetButtonHandle(ownerFullPath $ ".Cancel_btn");
	maximizeBtn = GetButtonHandle(ownerFullPath $ ".Maximize_btn");
	craftEffect = GetEffectViewportWndHandle(ownerFullPath $ ".EffectViewport00");
	ProgressBar = GetProgressCtrlHandle(ownerFullPath $ ".Progressbar");
	slotWndArray.Length = 0;
	ProgressBar.SetProgressTime(class'ShopLcoinCraftWnd'.const.TIMER_AUTO_CRAFT_DELAY - 100);

	for(i = 0; i < class'ShopLcoinCraftWnd'.const.MAX_CARD; i++)
	{
		slotWndArray[i] = GetWindowHandle(ownerFullPath $ ".RandomSlotGroup_Wnd" $ string(i));
	}	
}

function SetItemSlots(PurchaseLimitCraftUIData productData)
{
	local int i;
	local WindowHandle slotWnd;

	if(USE_AUTO_CRAFT_MINIMIZE == false)
	{
		return;
	}
	AlignCards(productData.BuyItems.Length);

	for(i = 0; i < slotWndArray.Length; i++)
	{
		slotWnd = slotWndArray[i];
		if(i < productData.BuyItems.Length)
		{
			SetBuySlot(slotWnd, productData.BuyItems[i]);
			slotWnd.ShowWindow();
			// [Explicit Continue]
			continue;
		}
		slotWnd.HideWindow();
	}
	_isUserCancel = false;	
}

function AlignCards(int Num)
{
	local int i, StartX, gab, gabW, targetX;

	gab = 60;
	gabW = ((class'ShopLcoinCraftWnd'.const.MAX_CARD - Num) * 60) / 2;
	StartX = 16 + gabW;

	for(i = 0; i < Num; i++)
	{
		targetX = StartX + (gab * i);
		slotWndArray[i].MoveC(targetX, 50);
		slotXPosArray[i] = targetX;
	}	
}

function SetBuySlot(WindowHandle slotWnd, PurchaseLimitCraftBuyItemInfo buyItemInfo)
{
	local ItemWindowHandle ItemWnd;
	local TextureHandle gradeTex, limitedTex, disableTex;
	local TextBoxHandle resultNumTextBox;
	local ItemInfo ItemInfo;

	if(USE_AUTO_CRAFT_MINIMIZE == false)
	{
		return;
	}
	ItemWnd = GetItemWindowHandle(slotWnd.m_WindowNameWithFullPath $ ".CraftSlot_ItemWnd");
	gradeTex = GetTextureHandle(slotWnd.m_WindowNameWithFullPath $ ".CraftCardBG_tex");
	limitedTex = GetTextureHandle(slotWnd.m_WindowNameWithFullPath $ ".LimitedRibbon_Tex");
	disableTex = GetTextureHandle(slotWnd.m_WindowNameWithFullPath $ ".AutoCraft_CardCover_Tex");
	resultNumTextBox = GetTextBoxHandle(slotWnd.m_WindowNameWithFullPath $ ".ResultNum_textbox");
	ItemInfo = GetItemInfoByClassID(buyItemInfo.ItemClassID);
	ItemInfo.RefineryOp1 = 0;
	ItemInfo.RefineryOp2 = 0;
	ItemInfo.Enchanted = buyItemInfo.Enchant;
	if(buyItemInfo.IsLimitServer)
	{
		limitedTex.ShowWindow();		
	}
	else
	{
		limitedTex.HideWindow();
	}
	resultNumTextBox.SetText("");
	gradeTex.SetTexture(GetGradeTextureByRank(buyItemInfo.ProductRank));
	ItemWnd.Clear();
	ItemWnd.AddItem(ItemInfo);
	disableTex.ShowWindow();	
}

function UpdateBuySlot(int SlotIndex, int craftNum)
{
	local WindowHandle slotWnd;
	local Rect slotWndRect;

	if(USE_AUTO_CRAFT_MINIMIZE == false)
	{
		return;
	}
	if(SlotIndex >= slotWndArray.Length)
	{
		return;
	}
	slotWnd = slotWndArray[SlotIndex];
	GetTextureHandle(slotWnd.m_WindowNameWithFullPath $ ".AutoCraft_CardCover_Tex").HideWindow();
	GetTextBoxHandle(slotWnd.m_WindowNameWithFullPath $ ".ResultNum_textbox").SetText("x" $ string(craftNum));
	slotWndRect = slotWnd.GetRect();
	if(Me.IsShowWindow())
	{
		craftEffect.MoveC(slotXPosArray[SlotIndex] - 41, 6);
		craftEffect.SpawnEffect("LineageEffect2.ui_upgrade_succ");
	}	
}

function UpdateAutoCraftInfo(ShopLcoinCraftWnd.AutoCraftInfo Info)
{
	if(USE_AUTO_CRAFT_MINIMIZE == false)
	{
		return;
	}
	_autoCraftInfo = Info;
	if(Info.uiState == craftWndScript.EAutoCraftState.ACProcess)
	{
		cntTextBox.ShowWindow();
		descTextBox.HideWindow();
		if(Info.Type == craftWndScript.EAutoCraftType.ACLimitedItem)
		{
			cntTextBox.SetText(GetSystemMessage(13861) @ string(_autoCraftInfo.currentCount));			
		}
		else
		{
			if(Info.Type == craftWndScript.EAutoCraftType.ACFixedCount)
			{
				cntTextBox.SetText(GetSystemMessage(13861) @ string(_autoCraftInfo.currentCount) $ "/" $ string(_autoCraftInfo.maxCount));
			}
		}
		CancelBtn.SetButtonName(141);		
	}
	else
	{
		cntTextBox.HideWindow();
		descTextBox.ShowWindow();
		ProgressBar.Reset();
		if(_isUserCancel == true)
		{
			_isUserCancel = false;
			descTextBox.SetText(GetSystemMessage(13885));			
		}
		else
		{
			descTextBox.SetText(GetSystemMessage(13884));
		}
		descTextBox.SetTextColor(GetColor(229, 219, 189, 255));
		CancelBtn.SetButtonName(140);
	}	
}

function string GetGradeTextureByRank(int ProductRank)
{
	switch(ProductRank)
	{
		case 0:
			return "L2UI_EPIC.LCoinShopWnd.LCoinShopCraftCardSmall_01";
		case 1:
			return "L2UI_EPIC.LCoinShopWnd.LCoinShopCraftCardSmall_02";
		case 2:
			return "L2UI_EPIC.LCoinShopWnd.LCoinShopCraftCardSmall_03";
		case 3:
			return "L2UI_EPIC.LCoinShopWnd.LCoinShopCraftCardSmall_04";
	}
	return "";	
}

function SetFailMessage(int msgIndex, optional bool isSystemStr)
{
	if(isSystemStr == true)
	{
		descTextBox.SetText(GetSystemString(msgIndex));		
	}
	else
	{
		descTextBox.SetText(GetSystemMessage(msgIndex));
	}
	descTextBox.SetTextColor(GetColor(255, 0, 0, 255));	
}

function StartCraftProgressBar()
{
	if((true == false) || Me.IsShowWindow() == false)
	{
		return;
	}
	ProgressBar.Reset();
	ProgressBar.Start();	
}

function SetCraftProgressBar(int CurrentTime)
{
	if(_autoCraftInfo.uiState != craftWndScript.EAutoCraftState.ACProcess)
	{
		return;
	}
	ProgressBar.SetPos(CurrentTime);
	ProgressBar.Start();	
}

function OpenWindow()
{
	Me.ShowWindow();	
}

function CloseWindow()
{
	ProgressBar.Reset();
	Me.HideWindow();	
}

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

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "Cancel_btn":
			if(_autoCraftInfo.uiState == craftWndScript.EAutoCraftState.ACProcess)
			{
				_isUserCancel = true;
				craftWndScript.HandleCraftBtn();				
			}
			else
			{
				craftWndScript.SetAutoCraftMinimize(false);
			}
			break;
		case "Maximize_btn":
			craftWndScript.SetAutoCraftMinimize(false);
			break;
	}	
}

event OnCancelBtnClicked();

event OnMaximizeBtnClicked();

event OnReceivedCloseUI()
{
	craftWndScript.OnReceivedCloseUI();	
}
