class UISkillToolWnd extends UICommonAPI;

const FINDING_NUM_ONTICK = 1000;

var private UIControlTextInput uicontrolFindTextInputScr;
var private UIControlTextInput uicontrolExecptionTextInputScr;
var private RichListCtrlHandle skillListCtrl;
var private int lastSkillClassiD;
var private int lastSkillLevel;
var private int lastSubSkillInfo;
var private bool bFinding;
var private ComboBoxHandle SkillOperateTypeComboBox;
var private ComboBoxHandle SkillGradeComboBox;
var private ComboBoxHandle SkillIconTypeComboBox;
var private TextBoxHandle m_hTbTotal;
var private TextBoxHandle m_hTbFoundResultCnt;
var private CheckBoxHandle m_chkEnchantOnly;
var private CheckBoxHandle m_chkEnchanHide;
var private CheckBoxHandle m_chkLevelHide;
var private int findOnTickNum;
var private array<int> iconTypes;
var private int lastIconTypeReserved;
var private bool bNewIconType;
var bool bFindStop;

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

private function SetCheckBoxs()
{
	local int B;

	GetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, "chkEnchantOnly", B, "l2.ini");
	m_chkEnchantOnly.SetCheck(B == 1);
	GetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, "chkEnchanHide", B, "l2.ini");
	m_chkEnchanHide.SetCheck(B == 1);
	GetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, "chkLevelHide", B, "l2.ini");
	m_chkLevelHide.SetCheck(B == 1);	
}

function Initialize()
{
	InitUIControlTextInput();
	skillListCtrl = GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".SkillListCtrl");
	SkillOperateTypeComboBox = GetComboBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".SkillOperateTypeComboBox");
	SkillOperateTypeComboBox.AddStringWithReserved("OperateType", -1);
	m_chkEnchantOnly = GetCheckBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".chkEnchantOnly");
	m_chkEnchanHide = GetCheckBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".chkEnchanHide");
	m_chkLevelHide = GetCheckBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".chkLevelHide");
	SetCheckBoxs();
	SkillOperateTypeComboBox.AddStringWithReserved("0", 0);
	SkillOperateTypeComboBox.AddStringWithReserved("1", 1);
	SkillOperateTypeComboBox.AddStringWithReserved("2", 2);
	SkillOperateTypeComboBox.AddStringWithReserved("3", 3);
	SkillOperateTypeComboBox.AddStringWithReserved("4", 4);
	SkillOperateTypeComboBox.AddStringWithReserved("5", 5);
	SkillOperateTypeComboBox.AddStringWithReserved("6", 6);
	SkillIconTypeComboBox = GetComboBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".SkillIconTypeComboBox");
	SkillIconTypeComboBox.AddStringWithReserved("IconType", -1);
	SkillGradeComboBox = GetComboBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".SkillGradeComboBox");
	SkillGradeComboBox.AddStringWithReserved("Grade", -1);
	SkillGradeComboBox.AddStringWithReserved("0", 0);
	SkillGradeComboBox.AddStringWithReserved("1", 1);
	SkillGradeComboBox.AddStringWithReserved("2", 2);
	SkillGradeComboBox.AddStringWithReserved("3", 3);
	SkillGradeComboBox.AddStringWithReserved("4", 4);
	SkillGradeComboBox.AddStringWithReserved("5", 5);
	SkillGradeComboBox.AddStringWithReserved("6", 6);
	m_hTbTotal = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".tbTotal");
	m_hTbFoundResultCnt = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".tbFoundResultCnt");
	skillListCtrl.SetSelectedSelTooltip(false);
	skillListCtrl.SetAppearTooltipAtMouseX(true);	
}

private function InitUIControlTextInput()
{
	uicontrolFindTextInputScr = class'UIControlTextInput'.static.InitScript(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".FindTextInput"));
	uicontrolFindTextInputScr.SetDefaultString(GetSystemString(2507));
	uicontrolFindTextInputScr.DelegateESCKey = DelegateESCKey;
	uicontrolFindTextInputScr.DelegateOnCompleteEditBox = DelegateOnCompleteEditBox;
	uicontrolFindTextInputScr.DelegateOnClear = DelegateOnClear;
	uicontrolFindTextInputScr.SetEdtiable(true);
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".FindTextInput").SetFocus();
	uicontrolExecptionTextInputScr = class'UIControlTextInput'.static.InitScript(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".execptionTextInput"));
	uicontrolExecptionTextInputScr.SetDefaultString("제외단어");
	uicontrolExecptionTextInputScr.DelegateESCKey = DelegateESCKey;
	uicontrolExecptionTextInputScr.DelegateOnCompleteEditBox = DelegateOnCompleteEditBox;
	uicontrolExecptionTextInputScr.DelegateOnClear = DelegateOnClear;
	uicontrolExecptionTextInputScr.SetEdtiable(true);	
}

private function DelegateESCKey()
{
	OnReceivedCloseUI();	
}

private function DelegateOnCompleteEditBox(string Text)
{
	NewFinding();	
}

private function DelegateOnClear()
{
	NewFinding();	
}

event OnComboBoxItemSelected(string sName, int Index)
{
	NewFinding();	
}

event OnShow()
{
	setWindowTitleByString("UIPowerTools [ SkillSearchTool ]");
	m_hOwnerWnd.SetFocus();
	SetListNum();	
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "searchSkillBtn":
			NewFinding();
			break;
	}	
}

event OnClickCheckBox(string strID)
{
	SetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, strID, GetCheckBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ "." $ strID).IsChecked(), "l2.ini");
	NewFinding();	
}

event OnTick()
{
	MeDisableTick();
	FindAllSkill();	
}

event OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	m_hOwnerWnd.HideWindow();	
}

event OnDBClickListCtrlRecord(string ListCtrlID)
{
	local RichListCtrlRowData rowData;

	skillListCtrl.GetSelectedRec(rowData);
	ExecuteCommand("//setskill" @ string(rowData.nReserved1) @ string(rowData.nReserved2) @ string(rowData.nReserved3));	
}

event OnRClickListCtrlRecord(string strID)
{
	local RichListCtrlRowData rowData;
	local UserInfo User;
	local string TargetName, ShowMsg;

	if(GetTargetInfo(User))
	{
		TargetName = User.Name;		
	}
	else
	{
		TargetName = GetSystemString(27);
	}
	skillListCtrl.GetSelectedRec(rowData);
	ShowMsg = GetSystemString(14391) @ ":" @ rowData.cellDataList[0].szData $ "," @ GetSystemString(13624) @ ":" @ TargetName;
	class'L2Util'.static.Inst().showGfxScreenMessage(ShowMsg);
	ExecuteCommand("//use_skill" @ string(rowData.nReserved1) @ string(rowData.nReserved2) @ string(rowData.nReserved3));
	OnDBClickListCtrlRecord(strID);	
}

event OnRollOverListCtrlRecord(string strID, int Index)
{
	HandleSetDescWindow(Index);	
}

private function HandleSetDescWindow(int Index)
{
	local ItemDescWnd itemDescWndScr;
	local string HtmlString;
	local RichListCtrlRowData overRec;
	local GMFindTreeWnd gMFindTreeWndScr;

	gMFindTreeWndScr = GMFindTreeWnd(GetScript("GMFindTreeWnd"));
	itemDescWndScr = ItemDescWnd(GetScript("ItemDescWnd"));
	itemDescWndScr.setWindowTitleByString(GetSystemString(14159));
	if(! itemDescWndScr.m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	skillListCtrl.GetRec(Index, overRec);
	HtmlString = gMFindTreeWndScr._GetHtmlDetaiInfoSkill(int(overRec.nReserved1), int(overRec.nReserved2), int(overRec.nReserved3));
	HtmlString = gMFindTreeWndScr._HtmlMakeTitleNDesc("ID", string(overRec.nReserved1)) $ HtmlString;
	HtmlString = gMFindTreeWndScr._MakeHtmlTable(HtmlString);
	HtmlString = gMFindTreeWndScr._HtmlQuestnameTableAdd(overRec.cellDataList[0].szData) $ "</br>" $ HtmlString;
	itemDescWndScr._LoadHtmlFromString(htmlSetHtmlStart(HtmlString));	
}

function _Show(string findString)
{
	m_hOwnerWnd.ShowWindow();
	uicontrolFindTextInputScr.SetString(findString);
	NewFinding();	
}

private function InsertSubLevelInfos(array<int> subLevelInfos)
{
	local int i;

	for(i = 1; i < subLevelInfos.Length; i++)
	{
		lastSubSkillInfo = subLevelInfos[i];
		class'UIDATA_SKILL'.static.GetNextID();
		if(! m_chkEnchanHide.IsChecked())
		{
			InsertRecordFindTreeList();
		}
		findOnTickNum++;
	}
	lastSubSkillInfo = 0;	
}

function _SetFindStop()
{
	bFindStop = true;	
}

private function bool IsFindStop()
{
	if(bFindStop)
	{
		bFindStop = false;
		return true;
	}
	return false;	
}

private function bool FindAllSkill()
{
	local ItemID cID;
	local int i;
	local array<int> subLevelInfos;

	if(IsFindStop())
	{
		SetListFindingStop();
		return false;
	}
	GMFindTreeWnd(GetScript("GMFindTreeWnd"))._SetFindStop();
	if(! bFinding)
	{
		cID = class'UIDATA_SKILL'.static.GetFirstID();
		lastSkillClassiD = cID.ClassID;
		lastSkillLevel = 1;
		InsertRecordFindTreeList();		
	}
	else
	{
		cID.ClassID = lastSkillClassiD;
	}
	bFinding = true;

	for(i = 0; i < FINDING_NUM_ONTICK; i++)
	{
		findOnTickNum++;
		if(GetSkillSubLevelList(lastSkillClassiD, lastSkillLevel, subLevelInfos) > 1)
		{
			InsertSubLevelInfos(subLevelInfos);
		}
		cID = class'UIDATA_SKILL'.static.GetNextID();
		if(! IsValidItemID(cID))
		{
			lastSkillClassiD = -1;
			bFinding = false;
			SetListNum();
			return false;
		}
		if(lastSkillClassiD != cID.ClassID)
		{
			lastSkillLevel = 1;			
		}
		else
		{
			lastSkillLevel++;
		}
		lastSkillClassiD = cID.ClassID;
		InsertRecordFindTreeList();
	}
	MeEnabmeTick();
	SetListFindingNum();
	return true;	
}

private function ClearFinding()
{
	findOnTickNum = -1;
	lastSkillClassiD = -1;
	bFindStop = false;
	bFinding = false;
	skillListCtrl.DeleteAllItem();	
}

private function InsertRecordFindTreeList()
{
	local string modifiedString;
	local RichListCtrlRowData rowData;
	local string findString, fullNameString, IconName;
	local SkillInfo sInfo;
	local ItemID cID;

	cID.ClassID = lastSkillClassiD;
	fullNameString = class'UIDATA_SKILL'.static.GetName(cID, lastSkillLevel, lastSubSkillInfo);
	IconName = class'UIDATA_SKILL'.static.GetIconName(cID, lastSkillLevel, lastSubSkillInfo);
	IconName = Caps(IconName);
	GetSkillInfo(cID.ClassID, lastSkillLevel, lastSubSkillInfo, sInfo);
	modifiedString = Substitute(fullNameString, " ", "", false);
	if((API_StringMatching(modifiedString, uicontrolExecptionTextInputScr.GetString())) != -1)
	{
		return;
	}
	findString = uicontrolFindTextInputScr.GetString();
	if(((findString != "") && findString != string(cID.ClassID)) && (API_StringMatching(modifiedString, findString)) == -1)
	{
		return;
	}
	if(SkillOperateTypeComboBox.GetSelectedNum() > 0)
	{
		if(SkillOperateTypeComboBox.GetReserved(SkillOperateTypeComboBox.GetSelectedNum()) != sInfo.OperateType)
		{
			return;
		}
	}
	if(SkillOperateTypeComboBox.GetSelectedNum() > 0)
	{
		if(SkillOperateTypeComboBox.GetReserved(SkillOperateTypeComboBox.GetSelectedNum()) != sInfo.OperateType)
		{
			return;
		}
	}
	if(SkillGradeComboBox.GetSelectedNum() > 0)
	{
		if(SkillGradeComboBox.GetReserved(SkillGradeComboBox.GetSelectedNum()) != sInfo.Grade)
		{
			return;
		}
	}
	if(SkillIconTypeComboBox.GetSelectedNum() > 0)
	{
		if(SkillIconTypeComboBox.GetReserved(SkillIconTypeComboBox.GetSelectedNum()) != sInfo.IconType)
		{
			return;
		}
	}
	if(m_chkEnchantOnly.IsChecked())
	{
		if(lastSubSkillInfo == 0)
		{
			return;
		}
	}
	if(m_chkLevelHide.IsChecked())
	{
		if(lastSkillLevel > 1)
		{
			return;
		}
	}
	AddIconType(sInfo.IconType);
	rowData.cellDataList.Length = 4;
	AddRichListCtrlSkillBySkillInfo(rowData.cellDataList[0].drawitems, sInfo, 32, 32, 10);
	addRichListCtrlString(rowData.cellDataList[0].drawitems, fullNameString, class'L2Util'.static.Inst().White, false, 5, 9);
	if(getInstanceUIData().getIsClassicServer())
	{
		addRichListCtrlString(rowData.cellDataList[0].drawitems, "Lv", class'L2Util'.static.Inst().Yellow, false, 5);
		addRichListCtrlString(rowData.cellDataList[0].drawitems, string(lastSkillLevel), class'L2Util'.static.Inst().Yellow, false, 2);		
	}
	else
	{
		addRichListCtrlString(rowData.cellDataList[0].drawitems, "Lv", class'L2Util'.static.Inst().Gray, false, 5);
		addRichListCtrlString(rowData.cellDataList[0].drawitems, string(lastSkillLevel), class'L2Util'.static.Inst().Gold, false, 2);
	}
	addRichListCtrlString(rowData.cellDataList[1].drawitems, string(lastSkillLevel), class'L2Util'.static.Inst().White, false, 10);
	if(lastSubSkillInfo > 0)
	{
		addRichListCtrlString(rowData.cellDataList[2].drawitems, string(lastSubSkillInfo), class'L2UIColor'.static.Inst().VIOLET01, false, 10);		
	}
	else
	{
		addRichListCtrlString(rowData.cellDataList[2].drawitems, string(lastSubSkillInfo), class'L2Util'.static.Inst().Gray, false, 10);
	}
	addRichListCtrlString(rowData.cellDataList[3].drawitems, string(lastSkillClassiD), class'L2UIColor'.static.Inst().White, false);
	rowData.cellDataList[1].nReserved1 = lastSkillLevel;
	rowData.nReserved1 = cID.ClassID;
	rowData.cellDataList[0].szData = fullNameString;
	rowData.cellDataList[1].szData = string(cID.ClassID);
	rowData.nReserved1 = lastSkillClassiD;
	rowData.nReserved2 = lastSkillLevel;
	rowData.nReserved3 = lastSubSkillInfo;
	skillListCtrl.InsertRecord(rowData);	
}

private function SetListNum()
{
	m_hTbTotal.SetText(GetSystemString(14135));
	m_hTbFoundResultCnt.SetText(string(skillListCtrl.GetRecordCount()));
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".findingIcon").HideWindow();
	InsertIconTypes();	
}

private function SetListFindingNum()
{
	m_hTbTotal.SetText(GetSystemString(3513));
	m_hTbFoundResultCnt.SetText(string(findOnTickNum));
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".findingIcon").ShowWindow();	
}

private function SetListFindingStop()
{
	m_hTbTotal.SetText("검색 중지");
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".findingIcon").HideWindow();	
}

private function AddIconType(int IconType)
{
	local int i;

	for(i = 0; i < iconTypes.Length; i++)
	{
		if(iconTypes[i] == IconType)
		{
			return;
		}
	}
	bNewIconType = true;
	iconTypes[iconTypes.Length] = IconType;	
}

private function InsertIconTypes()
{
	local int i;

	if(! bNewIconType)
	{
		bNewIconType = false;
		return;
	}
	iconTypes.Sort(DelegateSortIconType);
	SkillIconTypeComboBox.Clear();
	SkillIconTypeComboBox.AddStringWithReserved("IconType", -1);

	for(i = 0; i < iconTypes.Length; i++)
	{
		SkillIconTypeComboBox.AddStringWithReserved(getSkillTypeString(iconTypes[i]), iconTypes[i]);
	}

	for(i = 0; i < iconTypes.Length; i++)
	{
		if(lastIconTypeReserved == SkillIconTypeComboBox.GetReserved(i))
		{
			SkillIconTypeComboBox.SetSelectedNum(i);
			return;
		}
	}	
}

delegate int DelegateSortIconType(int A, int B)
{
	if(A > B)
	{
		return -1;
	}
	return 0;	
}

private function NewFinding()
{
	lastIconTypeReserved = SkillIconTypeComboBox.GetReserved(SkillIconTypeComboBox.GetSelectedNum());
	ClearFinding();
	FindAllSkill();	
}

private function MeDisableTick()
{
	m_hOwnerWnd.DisableTick();	
}

private function MeEnabmeTick()
{
	m_hOwnerWnd.EnableTick();	
}

private function int API_StringMatching(string Str, string a_Param)
{
	if(StringMatching(Str, a_Param, " "))
	{
		return 1;
	}
	return -1;	
}
