class BoxCreateInfosLikeTooltip extends UICommonAPI;

const ITEMNAME_ICON_WHITESPACE = 70;
const ITEMNAME_HEIGHT = 58;
const LIST_HEIGHT = 28;
const LIST_SHOWROW = 20;

var WindowHandle Me;
var RichListCtrlHandle List_ListCtrl;
var ItemWindowHandle Item_ItemWindow;
var HtmlHandle ItemName_HtmlCtrl;
var array<CreateResultItemData> fixedItemList;
var array<CreateResultItemData> addItemList;
var array<CreateResultItemData> randomItemList;
var bool bUsePercentColumn;
var int LIST_PERCENT_COLUMN;

function OnRegisterEvent()
{
	RegisterEvent(EV_RequestShowXMLDetailTooltip);
	RegisterEvent(EV_HideXMLDetailTooltip);
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_GameStart);
}

function OnLoad()
{
	Initialize();
}

function Initialize()
{
	Me = GetWindowHandle("BoxCreateInfosLikeTooltip");
	List_ListCtrl = GetRichListCtrlHandle("BoxCreateInfosLikeTooltip" $ ".List_ListCtrl");
	Item_ItemWindow = GetItemWindowHandle("BoxCreateInfosLikeTooltip" $ ".Item_ItemWindow");
	ItemName_HtmlCtrl = GetHtmlHandle("BoxCreateInfosLikeTooltip" $ ".ItemName_HtmlCtrl");
	List_ListCtrl.SetSelectable(false);
}

event OnEvent(int a_EventID, string a_Param)
{
	local int IconX, IconY, ClassID, clientW, clientH, ShortcutType;

	local string TooltipType;

	switch(a_EventID)
	{
		case EV_RequestShowXMLDetailTooltip:
			ParseString(a_Param, "TooltipType", TooltipType);
			if(TooltipType == "Skill")
			{
				return;
			}
			if(TooltipType == "Macro")
			{
				return;
			}
			if(TooltipType == "Action")
			{
				return;
			}
			ParseInt(a_Param, "ShortCutType", ShortcutType);
			if((ShortcutType == 1) || ShortcutType == 0)
			{
				ParseInt(a_Param, "IconX", IconX);
				ParseInt(a_Param, "IconY", IconY);
				ParseInt(a_Param, "clientW", clientW);
				ParseInt(a_Param, "clientH", clientH);
				ParseInt(a_Param, "classID", ClassID);
				ShowCreateInfos(IconX, IconY, clientW, clientH, ClassID);
				Me.SetFocus();
				List_ListCtrl.SetFocus();
			}
			break;
		case EV_GameStart:
		case EV_Restart:
		case EV_HideXMLDetailTooltip:
			if(Me.IsShowWindow())
			{
				Me.HideWindow();
			}
			break;
	}
}

event OnShow()
{
	ReturnShowXMLDetailTooltip(true);
}

event OnHide()
{}

function ShowCreateInfos(int lconX, int lconY, int clientW, int clientH, int ClassID)
{
	local int i, Count, maxTextW, W, h, N,
		itemNameTextW;

	local array<ItemCreateUIData> o_ItemList;
	local string titleStr, htmlAdd;
	local ItemInfo Info;
	local int OffsetX, toolTipWidth, TooltipHeight;

	class'UIDATA_ITEM'.static.GetItemCreateInfo(ClassID, o_ItemList);
	bUsePercentColumn = true;
	if(o_ItemList.Length == 0)
	{
		return;
	}
	Info = GetItemInfoByClassID(ClassID);
	Item_ItemWindow.Clear();
	Item_ItemWindow.AddItem(Info);
	GetTextSize(Info.Name @ Info.AdditionalName, "gameDefault10", itemNameTextW, h);
	htmlAdd = htmlAddText(Info.Name, "gameDefault10", getColorHexString(GTColor().BrightWhite));
	if(Info.AdditionalName != "")
	{
		htmlAdd = htmlAdd $ (htmlAddText(" " $ Info.AdditionalName, "gameDefault10", getColorHexString(GTColor().Yellow)));
	}
	ItemName_HtmlCtrl.SetWindowSize(itemNameTextW + LIST_SHOWROW, h + 4);
	ItemName_HtmlCtrl.LoadHtmlFromString(htmlSetHtmlStart(htmlAdd));
	List_ListCtrl.DeleteAllItem();
	if(Info.MaxUseCount <= 0)
	{
		itemNameTextW = itemNameTextW + ITEMNAME_ICON_WHITESPACE;
		toolTipWidth = divCreateInfos(maxTextW, o_ItemList);

		for(i = 0; i < fixedItemList.Length; i++)
		{
			if(i == 0)
			{
				titleStr = GetSystemString(13978);
				List_ListCtrl.InsertRecord(makeTitleListItem(titleStr));
				GetTextSize(titleStr, "gameDefault10", W, h);
				if(toolTipWidth < W)
				{
					toolTipWidth = W + 5;
				}
				Count++;
			}
			List_ListCtrl.InsertRecord(makeRecord(fixedItemList[i]));
			Count++;
		}

		for(i = 0; i < addItemList.Length; i++)
		{
			if(i == 0)
			{
				titleStr = GetSystemString(13979);
				List_ListCtrl.InsertRecord(makeTitleListItem(titleStr));
				GetTextSize(titleStr, "gameDefault10", W, h);
				if(toolTipWidth < W)
				{
					toolTipWidth = W + 5;
				}
				Count++;
			}
			List_ListCtrl.InsertRecord(makeRecord(addItemList[i]));
			Count++;
		}

		for(i = 0; i < randomItemList.Length; i++)
		{
			if(i == 0)
			{
				titleStr = GetSystemString(13980);
				List_ListCtrl.InsertRecord(makeTitleListItem(titleStr));
				GetTextSize(titleStr, "gameDefault10", W, h);
				if(toolTipWidth < W)
				{
					toolTipWidth = W + 5;
				}
				Count++;
			}
			List_ListCtrl.InsertRecord(makeRecord(randomItemList[i]));
			Count++;
		}		
	}
	else
	{
		itemNameTextW = itemNameTextW + ITEMNAME_ICON_WHITESPACE;

		for(i = 0; i < o_ItemList.Length; i++)
		{
			if(o_ItemList[i].Category == 0)
			{
				titleStr = GetSystemString(13981);				
			}
			else
			{
				titleStr = MakeFullSystemMsg(GetSystemMessage(13625), string(o_ItemList[i].Category));
			}
			GetTextSize(titleStr, "gameDefault10", W, h);
			if(toolTipWidth < W)
			{
				toolTipWidth = W + 5;
			}
			List_ListCtrl.InsertRecord(makeTitleListItem(titleStr));
			Count++;

			for(N = 0; N < o_ItemList[i].Items.Length; N++)
			{
				List_ListCtrl.InsertRecord(makeRecord(o_ItemList[i].Items[N]));
				toolTipWidth = getMaxWidthInCreateResultItemData(toolTipWidth, o_ItemList[i].Items[N]);
				Count++;
			}
		}
	}
	if(bUsePercentColumn)
	{
		LIST_PERCENT_COLUMN = 120;		
	}
	else
	{
		LIST_PERCENT_COLUMN = 0;
	}
	if(itemNameTextW > (toolTipWidth + LIST_PERCENT_COLUMN))
	{
		List_ListCtrl.SetColumnWidth(0, itemNameTextW - LIST_PERCENT_COLUMN);
		List_ListCtrl.SetColumnWidth(1, LIST_PERCENT_COLUMN);
		if(itemNameTextW > toolTipWidth)
		{
			toolTipWidth = itemNameTextW;
		}	
	}
	else
	{
		List_ListCtrl.SetColumnWidth(0, toolTipWidth);
		List_ListCtrl.SetColumnWidth(1, LIST_PERCENT_COLUMN);
		toolTipWidth = toolTipWidth + LIST_PERCENT_COLUMN;
	}
	toolTipWidth = toolTipWidth + 9;
	if(Count >= LIST_SHOWROW)
	{
		TooltipHeight = (LIST_HEIGHT * LIST_SHOWROW) + ITEMNAME_HEIGHT;
		Me.SetWindowSize(toolTipWidth, TooltipHeight);
		List_ListCtrl.SetWindowSize(toolTipWidth - 9, LIST_HEIGHT * LIST_SHOWROW);		
	}
	else
	{
		TooltipHeight = (LIST_HEIGHT * Count) + ITEMNAME_HEIGHT;
		Me.SetWindowSize((toolTipWidth + 5) + 1, TooltipHeight);
		List_ListCtrl.SetWindowSize(toolTipWidth, 28 * Count);
	}
	if(lconY - TooltipHeight < 0)
	{
		lconY = 0;
		OffsetX = 32;		
	}
	else
	{
		lconY = lconY - TooltipHeight;
	}
	if(lconX + OffsetX + toolTipWidth > clientW)
	{
		OffsetX = OffsetX - (((lconX + OffsetX) + toolTipWidth) - clientW);		
	}
	else
	{
		if(lconX + OffsetX < 0)
		{
			lconX = lconX + OffsetX;
		}
	}
	Me.MoveC(lconX + OffsetX, lconY);
	Me.ShowWindow();
}

function RichListCtrlRowData makeRecord(CreateResultItemData boxinfo)
{
	local RichListCtrlRowData rowData;
	local Color applyColor;

	rowData.cellDataList.Length = 2;
	applyColor = GTColor().White;
	if(boxinfo.EnchantValue > 0)
	{
		addRichListCtrlString(rowData.cellDataList[0].drawitems, ("+" $ string(boxinfo.EnchantValue)) $ " ", GTColor().Yellow, false, 0, 0, "GameDefault");
	}
	addRichListCtrlString(rowData.cellDataList[0].drawitems, boxinfo.ItemName, applyColor, false, 0, 0, "GameDefault");
	if(boxinfo.AdditionalName != "")
	{
		addRichListCtrlString(rowData.cellDataList[0].drawitems, " " $ boxinfo.AdditionalName, GetColor(255, 217, 105, 255), false, 0, 0, "GameDefault");
	}
	addRichListCtrlString(rowData.cellDataList[0].drawitems, " " $ MakeFullSystemMsg(GetSystemMessage(1983), string(boxinfo.Count)), GTColor().White, false, 0, 0, "GameDefault");
	if((float(boxinfo.Prob) == 0) || bUsePercentColumn == false)
	{
		bUsePercentColumn = false;
		addRichListCtrlString(rowData.cellDataList[1].drawitems, "", GTColor().White, false, 14, 0, "GameDefault");		
	}
	else
	{
		addRichListCtrlString(rowData.cellDataList[1].drawitems, getInstanceL2Util().CutFloatIntByString(boxinfo.Prob), GTColor().White, false, 14, 0, "GameDefault");
	}
	Debug("boxinfo.ItemName" @ boxinfo.ItemName);
	Debug("boxinfo.prob" @ boxinfo.Prob);
	Debug("bUsePercentColumn" @ string(bUsePercentColumn));
	return rowData;
}

function RichListCtrlRowData makeTitleListItem(string Str)
{
	local Color applyColor;
	local RichListCtrlRowData rowData;

	rowData.cellDataList.Length = 1;
	rowData.sOverlayTex = "L2UI_NewTex.ToolTip.TooltipwndListHeader";
	applyColor = GetColor(238, 170, 34, 255);
	addRichListCtrlString(rowData.cellDataList[0].drawitems, Str, applyColor, false, 0, 0, "gameDefault10");
	rowData.OverlayTexU = 256;
	rowData.OverlayTexV = LIST_HEIGHT;
	return rowData;
}

function int getMaxWidthInCreateResultItemData(int maxTextW, CreateResultItemData ItemData)
{
	local string AdditionalName, enchantedStr, countStr, allStr;
	local int W, h;

	if(ItemData.EnchantValue > 0)
	{
		enchantedStr = ("+" $ string(ItemData.EnchantValue)) $ " ";
	}
	if(ItemData.AdditionalName != "")
	{
		AdditionalName = " " $ ItemData.AdditionalName;
	}
	countStr = " " $ MakeFullSystemMsg(GetSystemMessage(1983), string(ItemData.Count));
	allStr = ((enchantedStr $ ItemData.ItemName) $ AdditionalName) $ countStr;
	GetTextSizeDefault(allStr, W, h);
	W = W + 30;
	if(maxTextW < W)
	{
		maxTextW = W;
	}
	return maxTextW;
}

function int divCreateInfos(int maxTextW, array<ItemCreateUIData> ItemCreateUIDatas)
{
	local int i, N;

	fixedItemList.Length = 0;
	addItemList.Length = 0;
	randomItemList.Length = 0;

	for(i = 0; i < ItemCreateUIDatas.Length; i++)
	{
		for(N = 0; N < ItemCreateUIDatas[i].Items.Length; N++)
		{
			switch(ItemCreateUIDatas[i].Category)
			{
				case 0:
					fixedItemList.Insert(fixedItemList.Length, 1);
					fixedItemList[fixedItemList.Length - 1] = ItemCreateUIDatas[i].Items[N];
					break;
				case 1:
					addItemList.Insert(addItemList.Length, 1);
					addItemList[addItemList.Length - 1] = ItemCreateUIDatas[i].Items[N];
					break;
				case 2:
					randomItemList.Insert(randomItemList.Length, 1);
					randomItemList[randomItemList.Length - 1] = ItemCreateUIDatas[i].Items[N];
					break;
			}
			maxTextW = getMaxWidthInCreateResultItemData(maxTextW, ItemCreateUIDatas[i].Items[N]);
		}
	}
	fixedItemList.Sort(OnSortProbCompare);
	addItemList.Sort(OnSortProbCompare);
	randomItemList.Sort(OnSortProbCompare);
	return maxTextW;
}

delegate int OnSortProbCompare(CreateResultItemData A, CreateResultItemData B)
{
	if(float(A.Prob) > float(B.Prob))
	{
		return -1;		
	}
	else
	{
		return 0;
	}
}

function Color GetNameClassColor(int NameClass)
{
	local Color applyColor;

	switch(NameClass)
	{
		case 0:
			applyColor = GetColor(137, 137, 137, 255);
			break;
		case 2:
			applyColor = GetColor(255, 251, 4, 255);
			break;
		case 3:
			applyColor = GetColor(240, 68, 68, 255);
			break;
		case 4:
			applyColor = GetColor(33, 164, 255, 255);
			break;
		case 5:
			applyColor = GetColor(255, 0, 255, 255);
			break;
		default:
			applyColor = GetColor(255, 255, 255, 255);
			break;
	}
	return applyColor;
}
