class CollectionSystemCategoryComponent extends UICommonAPI;

const STATE_SELECTED = 'stateSelected';
const STATE_NORMAL = 'stateNormal';

enum DOTTYPE
{
	non,
	canregist,
	notEnough,
	empty,
	Over,
	Max
};

var WindowHandle Me;
var string m_WindowName;
var L2Util util;
var ButtonHandle ItemBTN;
var TextBoxHandle buttonName;
var StatusBarHandle Progress;
var CollectionSystem collectionSystemScript;
var array<CollectionSystemStandComponent> collectionSystemStandComponentScripts;
var int Index;
var bool bOver;
var bool bDown;
var bool bSelected;
var TextureHandle Icon_Tex;
var TextureHandle TooltipTexture;
var DOTTYPE currentDotType;
var TextureHandle ItemInsufficient_Tex;
var TextureHandle ItemRegistration_Tex;
var TextureHandle ItemOverEnchant_Tex;
var Color selectedEmptyColor;

delegate DelegateOnButtonClick(string Name)
{

}

delegate DelegateOnOver()
{

}

function Initialize()
{
	util = L2Util(GetScript("L2Util"));
	collectionSystemScript = CollectionSystem(GetScript("collectionSystem"));
	buttonName = GetTextBoxHandle(m_WindowName $ ".buttonName_Txt");
	ItemBTN = GetButtonHandle(m_WindowName $ ".Item_Btn");
	Progress = GetStatusBarHandle(m_WindowName $ ".progress");
	Icon_Tex = GetTextureHandle(m_WindowName $ ".Icon_Tex");
	TooltipTexture = GetTextureHandle(m_WindowName $ ".TooltipTexture");
	ItemRegistration_Tex = GetTextureHandle(m_WindowName $ ".ItemRegistration_Tex");
	ItemInsufficient_Tex = GetTextureHandle(m_WindowName $ ".ItemInsufficient_Tex");
	ItemOverEnchant_Tex = GetTextureHandle(m_WindowName $ ".ItemOverEnchant_Tex");
	TooltipTexture.SetAlpha(0);
	selectedEmptyColor = GetColor(115, 85, 0, 255);
}

function Init(string WindowName, string _buttonName, int Category)
{
	m_WindowName = WindowName;
	Me = GetWindowHandle(m_WindowName);
	Index = Category - 1;
	Initialize();
	SetStandComponetScript(Category);
	Progress.SetDrawPoint(false);
	buttonName.SetText(_buttonName);
}

function SetStandComponetScript(int Category)
{
	local int i;

	if(Category == collectionSystemScript.favoriteCategory)
	{
		collectionSystemStandComponentScripts = collectionSystemScript.collectionSystemStandComponentFavoriteScript;
		return;
	}

	for(i = 0; i < collectionSystemScript.collectionSystemStandComponentScripts.Length; i++)
	{
		if(collectionSystemScript.collectionSystemStandComponentScripts[i].cMainData.Category == Category)
		{
			collectionSystemStandComponentScripts[collectionSystemStandComponentScripts.Length] = collectionSystemScript.collectionSystemStandComponentScripts[i];
		}
	}
}

event OnClickButton(string btnName)
{
	if(bSelected)
	{
		return;
	}
	switch(btnName)
	{
		case "Item_Btn":
			bDown = false;
			collectionSystemScript.SetCurrentCategory(Index + 1);
			break;
	}
}

event OnLButtonDown(WindowHandle a_WindowHandle, int X, int Y)
{
	if(bSelected)
	{
		return;
	}
	switch(a_WindowHandle)
	{
		case ItemBTN:
			bDown = true;
			Icon_Tex.SetTexture(("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index)) $ "_down");
			break;
	}
}

event OnMouseOut(WindowHandle a_WindowHandle)
{
	if(bSelected)
	{
		return;
	}
	switch(a_WindowHandle)
	{
		case ItemBTN:
			bDown = false;
			HandleBtnOut();
			break;
	}
}

event OnMouseOver(WindowHandle a_WindowHandle)
{
	if(bSelected)
	{
		return;
	}
	switch(a_WindowHandle)
	{
		case ItemBTN:
			HandleBtnOver();
			break;
	}
}

function SetSelecte()
{
	bSelected = true;
	bOver = false;
	bDown = false;
	Icon_Tex.SetTexture(("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index)) $ "_selected");
	ItemBTN.SetEnable(false);
	SetFontColor();
	HandleBtnOuts();
}

function HandleBtnOuts()
{
	local int i;

	for(i = 0; i < collectionSystemStandComponentScripts.Length; i++)
	{
		collectionSystemStandComponentScripts[i].HandleBtnOut();
	}
}

function SetDeselecte()
{
	bSelected = false;
	ItemBTN.SetEnable(true);
	Icon_Tex.SetTexture("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index));
	SetFontColor();
}

function HandleBtnOver()
{
	if(collectionSystemScript.Me.IsShowWindow())
	{
		bOver = true;
		Icon_Tex.SetTexture("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index) $ "_over");
		HandleBtnOvers();
	}
}

function HandleBtnOvers()
{
	local int i;

	for(i = 0; i < collectionSystemStandComponentScripts.Length; i++)
	{
		collectionSystemStandComponentScripts[i].HandleBtnOver();
	}
}

function HandleBtnOut()
{
	if(collectionSystemScript.Me.IsShowWindow())
	{
		bOver = false;
		Icon_Tex.SetTexture("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index));
		HandleBtnOuts();
	}
}

function SetBtnOver()
{
	if((collectionSystemScript.Me.IsShowWindow() && ! bOver) && ! bSelected)
	{
		ItemBTN.SetEnable(false);
		Icon_Tex.SetTexture(("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index)) $ "_over");
	}
}

function SetBtnOut()
{
	if(collectionSystemScript.Me.IsShowWindow() && ! bSelected)
	{
		ItemBTN.SetEnable(true);
		Icon_Tex.SetTexture("L2UI_EPIC.CollectionSystemWnd.Category_Icon" $ collectionSystemScript.GetStringKeyByIndex(Index));
	}
}

function SetPoint(int Min, int Max)
{
	local string ToolTipString;

	Progress.SetPoint(Min, Max);
	ToolTipString = (string(int((float(Min) / float(Max)) * 100)) $ "%") @ GetSystemString(898);
	ItemBTN.SetTooltipCustomType(MakeTooltipSimpleText(ToolTipString));
	TooltipTexture.SetTooltipCustomType(MakeTooltipSimpleText(ToolTipString));
}

function HideDot()
{
	currentDotType = non;
	Icon_Tex.SetColorModify(GetColor(255, 255, 255, 255));
	ItemRegistration_Tex.HideWindow();
	ItemInsufficient_Tex.HideWindow();
	ItemOverEnchant_Tex.HideWindow();
	SetFontColor();
}

function ShowDot(int Type)
{
	currentDotType = DOTTYPE(Type);
	switch(currentDotType)
	{
		case DOTTYPE.non:
			HideDot();
			break;
		case DOTTYPE.canregist:
			Icon_Tex.SetColorModify(GetColor(255, 255, 255, 255));
			ItemRegistration_Tex.ShowWindow();
			ItemInsufficient_Tex.HideWindow();
			ItemOverEnchant_Tex.HideWindow();
			break;
		case DOTTYPE.notEnough:
			Icon_Tex.SetColorModify(GetColor(255, 255, 255, 255));
			ItemRegistration_Tex.HideWindow();
			ItemInsufficient_Tex.ShowWindow();
			ItemOverEnchant_Tex.HideWindow();
			break;
		case DOTTYPE.empty:
			Icon_Tex.SetColorModify(GetColor(80, 80, 80, 255));
			ItemRegistration_Tex.HideWindow();
			ItemInsufficient_Tex.HideWindow();
			ItemOverEnchant_Tex.HideWindow();
			break;
		case DOTTYPE.Over/*4*/:
			Icon_Tex.SetColorModify(GetColor(255, 255, 255, 255));
			ItemRegistration_Tex.HideWindow();
			ItemInsufficient_Tex.HideWindow();
			ItemOverEnchant_Tex.ShowWindow();
			break;
	}
	SetFontColor();
}

function SetFontColor()
{
	if(currentDotType == DOTTYPE.empty)
	{
		if(bSelected)
		{
			buttonName.SetTextColor(selectedEmptyColor);
		}
		else
		{
			buttonName.SetTextColor(getInstanceL2Util().DarkGray);
		}
	}
	else
	{
		if(bSelected)
		{
			buttonName.SetTextColor(getInstanceL2Util().Yellow);
		}
		else
		{
			buttonName.SetTextColor(getInstanceL2Util().White);
		}
	}
}
