class GMFindTreeWnd extends UICommonAPI;

const ASK_SUMMON_NUMBER = 10000;
const SHOWLISTNUM_ONTICK_10 = 10;
const SHOWLISTNUM_ONTICK_500 = 3000;

enum EListType
{
	LISTTYPE_ITEM,
	LISTTYPE_NPC,
	LISTTYPE_QUEST,
	LISTTYPE_SKILL,	
	LISTTYPE_CLASS,	
	LISTTYPE_SYSTEMSTRING,		
	LISTTYPE_SYSTEMMESSAGE,		
};

var private WindowHandle Me;
var private string m_WindowName;
var private bool bShow;
var private ListCtrlHandle m_hFindTreeList;
var private ComboBoxHandle m_hComboBox;
var private ButtonHandle m_hBtnSummon;
var private ButtonHandle m_hbtnDetailInfo;
var private CheckBoxHandle m_checkbox64size;
var private CheckBoxHandle m_ChkBoxLv1;
var private TextBoxHandle m_hTbFoundResultCnt;
var private TextBoxHandle m_hTbTotal;
var private UIControlTextInput uicontrolFindTextInputScr;
var private UIControlTextInput uicontrolExecptionTextInputScr;
var private UIControlNumberInput SummonNumberTextInputScr;
var private WindowHandle m_GMFindTreeWndItem;
var private WindowHandle m_GMFindTreeWndSkill;
var private int findOnTickCurrentID;
var private int OnTickNum;
var private int findOnTickNum;
var private int showListNum_OnTick;
var private int lastSkillClassiD;
var private int SkillLevel;
var private bool bFindStop;
var private EListType curListType;

event OnRegisterEvent()
{	
	RegisterEvent(EV_DialogOK);
	RegisterEvent(EV_DialogCancel);
}


private function SetCheckBoxs()
{
	local int B;

	GetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, "ChkBox64", B, "l2.ini");
	m_checkbox64size.SetCheck(B == 1);
	GetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, "ChkBoxLv1", B, "l2.ini");
	m_ChkBoxLv1.SetCheck(B == 1);
	SetWindowSize();	
}

event OnLoad()
{
	SetClosingOnESC();
	Me = GetWindowHandle("GMFindTreeWnd");
	m_hFindTreeList = GetListCtrlHandle(m_WindowName $ ".ListFindWnd");
	m_checkbox64size = GetCheckBoxHandle(m_WindowName $ ".ChkBox64");
	m_ChkBoxLv1 = GetCheckBoxHandle(m_WindowName $ ".ChkBoxLv1");
	SetCheckBoxs();
	m_hTbFoundResultCnt = GetTextBoxHandle(m_WindowName $ ".tbFoundResultCnt");
	m_hTbTotal = GetTextBoxHandle(m_WindowName $ ".tbTotal");
	m_hComboBox = GetComboBoxHandle(m_WindowName $ ".ComboBox");
	m_hBtnSummon = GetButtonHandle(m_WindowName $ ".btnSummon");
	m_hbtnDetailInfo = GetButtonHandle(m_WindowName $ ".btnDetailInfo");
	// ??????????? ????? api , ?????????? ??????? ???? ???????? ??????? ???????????
	m_hFindTreeList.SetSelectedSelTooltip(false);
	m_hFindTreeList.SetAppearTooltipAtMouseX(true);
	bShow = false;
	FillOutComboBoxHandle();
	m_GMFindTreeWndItem = GetWindowHandle("GMFindTreeWndItem");
	m_GMFindTreeWndSkill = GetWindowHandle("GMFindTreeWndSkill");
	GetTextureHandle("GMFindTreeWnd.Texture64").SetTextureSize(64, 64);
	GetTextureHandle("GMFindTreeWnd.Texture48").SetTextureSize(64, 64);
	GetTextureHandle("GMFindTreeWnd.Texture32").SetTextureSize(64, 64);
	GetTextureHandle("GMFindTreeWnd.Texture24").SetTextureSize(64, 64);
	GetTextureHandle("GMFindTreeWnd.Texture16").SetTextureSize(64, 64);
	InitUIControlTextInput();
	ClearOnTickInfos();
	showListNum_OnTick = SHOWLISTNUM_ONTICK_10;	
}

private function InitUIControlTextInput()
{
	uicontrolFindTextInputScr = class'UIControlTextInput'.static.InitScript(GetWindowHandle(m_WindowName $ ".FindTextInput"));
	uicontrolFindTextInputScr.SetDefaultString(GetSystemString(2507));
	uicontrolFindTextInputScr.DelegateESCKey = DelegateESCKey;
	uicontrolFindTextInputScr.DelegateOnCompleteEditBox = DelegateOnCompleteEditBox;
	uicontrolFindTextInputScr.DelegateOnClear = DelegateOnClearEditBox;
	uicontrolFindTextInputScr.DelegateOnChangeEdited = DelegateOnEditEditBox;
	uicontrolFindTextInputScr.DelegateOnKeyUP = DelegateOnKeyUP;
	uicontrolFindTextInputScr.SetEdtiable(true);
	uicontrolExecptionTextInputScr = class'UIControlTextInput'.static.InitScript(GetWindowHandle(m_WindowName $ ".execptionTextInput"));
	uicontrolExecptionTextInputScr.SetDefaultString("??????");
	uicontrolExecptionTextInputScr.DelegateESCKey = DelegateESCKey;
	uicontrolExecptionTextInputScr.DelegateOnCompleteEditBox = DelegateOnCompleteEditBox;
	uicontrolExecptionTextInputScr.DelegateOnClear = DelegateOnClearEditBox;
	uicontrolExecptionTextInputScr.DelegateOnChangeEdited = DelegateOnEditEditBox;
	uicontrolExecptionTextInputScr.DelegateOnKeyUP = DelegateOnKeyUP;
	uicontrolExecptionTextInputScr.SetEdtiable(true);	
}

function FillOutComboBoxHandle()
{
	m_hComboBox.Clear();
	m_hComboBox.AddStringWithReserved(GetSystemString( 691 ),   EListType.LISTTYPE_ITEM );
	m_hComboBox.AddStringWithReserved(GetSystemString( 690 ),   EListType.LISTTYPE_NPC );
	m_hComboBox.AddStringWithReserved(GetSystemString( 699 ),   EListType.LISTTYPE_QUEST );
	m_hComboBox.AddStringWithReserved(GetSystemString( 692 ),   EListType.LISTTYPE_SKILL );
	m_hComboBox.AddStringWithReserved(GetSystemString( 2290 ),  EListType.LISTTYPE_CLASS );		
	m_hComboBox.AddStringWithReserved("SystemString",           EListType.LISTTYPE_SYSTEMSTRING );
	m_hComboBox.AddStringWithReserved("SystemMessage",          EListType.LISTTYPE_SYSTEMMESSAGE );
}

event OnShow ()
{
	SetWindowSize();
}

event OnHide () 
{
	m_GMFindTreeWndItem.HideWindow();
	m_GMFindTreeWndSkill.HideWindow();
	Me.DisableTick();
}

event OnComboBoxItemSelected(string StrID, int IndexID)
{
	local string EditBoxString;

	EditBoxString = uicontrolFindTextInputScr.GetString();
	//Debug( "OnComboBoxItemSelected" @ EListType ( IndexID ) );
	switch(strID)
	{
		case "ComboBox" :			
			ShowList( EditBoxString , EListType( IndexID ));			
		break;
	}
}

event OnEvent( int a_EventID, String a_Param )
{
	switch( a_EventID )
	{
		case EV_DialogOK  :
			HandleDialogOK();
		break;
		case EV_DialogCancel :
		break;
	}
}

event OnDBClickListCtrlRecord( string ListCtrlID)
{
	summon(1);
}

event OnClickListCtrlRecord (string strID)
{
  local LVDataRecord Record;

  m_hBtnSummon.EnableWindow();
  if ( curListType == EListType.LISTTYPE_ITEM )
  {
    if ( GetSelectedListCtrlItem(Record) )
    {
      GMFindTreeWndItem(GetScript("GMFindTreeWndItem")).SetItemID(int(Record.LVDataList[1].szData));
    }
  }
  if ( (curListType == EListType.LISTTYPE_ITEM) || (curListType == EListType.LISTTYPE_SKILL) )
  {
    if ( GetSelectedListCtrlItem(Record) )
    {
      GetTextureHandle("GMFindTreeWnd.Texture64").SetTexture(Record.LVDataList[0].szTexture);
      GetTextureHandle("GMFindTreeWnd.Texture48").SetTexture(Record.LVDataList[0].szTexture);
      GetTextureHandle("GMFindTreeWnd.Texture32").SetTexture(Record.LVDataList[0].szTexture);
      GetTextureHandle("GMFindTreeWnd.Texture24").SetTexture(Record.LVDataList[0].szTexture);
      GetTextureHandle("GMFindTreeWnd.Texture16").SetTexture(Record.LVDataList[0].szTexture);
			Debug(Record.LVDataList[0].szTexture);
    }
  }
}

event OnRClickListCtrlRecord(string strID)
{
	m_hBtnSummon.EnableWindow();
	switch(curListType)
	{
		case EListType.LISTTYPE_SKILL:
			HandleShowSkillDetail();
			break;
		default:
			HandleBtnSummon();
			break;
	}
}

private function HandleShowSkillDetail()
{
	local LVDataRecord Record;

	if(! GetSelectedListCtrlItem(Record))
	{
		return;
	}
	UISkillToolWnd(GetScript("UISkillToolWnd"))._Show(Record.LVDataList[1].szData);	
}

event OnClickCheckBox(string strID)
{
	SetINIBool(m_hOwnerWnd.m_WindowNameWithFullPath, strID, GetCheckBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ "." $ strID).IsChecked(), "l2.ini");
	switch(strID)
	{
		case "ChkBox64":
			SetWindowSize();
			break;
		case "unLimitCheckBox":
			showListNum_OnTick = SHOWLISTNUM_ONTICK_10;
			break;
		case "ChkBoxLv1":
			FindByListTypeWithClear();
			showListNum_OnTick = 3000;
			break;
	}	
}

event OnClickButton( string strID )
{
	switch( strID )
	{
		case "btnSummon":
			HandleBtnSummon();
			break;
		case "btnFind":
			FindByListTypeWithClear();
			showListNum_OnTick = 3000;
			break;
		case "btnDetailInfo":
			ToggleItemDescWNd();
			break;
	}
}

/**
 * 윈도우 ESC 키로 닫기 처리 
 * "Esc" Key
 ***/
event OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	GetWindowHandle(m_WindowName).HideWindow();
}

event OnTick()
{
	Me.DisableTick();
	OnTickNum++;
	FindByListType();	
}

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

private function HandleSetDescWindow(int Index)
{
	local ItemDescWnd itemDescWndScr;
	local string HtmlString;
	local LVDataRecord overRec;

	itemDescWndScr = ItemDescWnd(GetScript("ItemDescWnd"));
	if(! itemDescWndScr.m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	m_hFindTreeList.GetRec(Index, overRec);
	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			itemDescWndScr.setWindowTitleByString(GetSystemString(564));
			break;
		case EListType.LISTTYPE_SKILL/*3*/:
			itemDescWndScr.setWindowTitleByString(GetSystemString(14159));
			break;
		case EListType.LISTTYPE_NPC/*1*/:
			itemDescWndScr.setWindowTitleByString(GetSystemString(2573) @ GetSystemString(491));
			break;
	}
	HtmlString = GetHtmlDetailInfo(Index);
	HtmlString = HtmlMakeTitleNDesc("ID", overRec.LVDataList[1].szData) $ HtmlString;
	HtmlString = MakeHtmlTable(HtmlString);
	HtmlString = GethtmlDetailInfoTitle(Index) $ "</br>" $ HtmlString;
	itemDescWndScr._LoadHtmlFromString(htmlSetHtmlStart(HtmlString));	
}

private function string GethtmlDetailInfoTitle(int Index)
{
	local LVDataRecord overRec;

	m_hFindTreeList.GetRec(Index, overRec);
	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			return HtmlItemnFullName(int(overRec.LVDataList[1].szData));
		case EListType.LISTTYPE_SKILL/*3*/:
			return HtmlQuestnameTableAdd(overRec.LVDataList[0].szData);
		case EListType.LISTTYPE_NPC/*1*/:
			return HtmlQuestnameTableAdd(overRec.LVDataList[0].szData);
	}
	return "";
}

private function string GetHtmlDetaiInfoItem(int Index)
{
	local string HtmlString;
	local LVDataRecord overRec;
	local ItemInfo iInfo;
	local array<int> IDs;

	m_hFindTreeList.GetRec(Index, overRec);
	class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(int(overRec.LVDataList[1].szData)), iInfo);
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Item type", string(iInfo.ItemType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Slot bit type", string(iInfo.SlotBitType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Consume type", string(iInfo.ConsumeType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Weapon type", string(iInfo.WeaponType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Collection use", string(CollectionSystem(GetScript("collectionSystem")).API_GetCollectionIdByItemId(IDs, iInfo.Id.ClassID))));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Item skill", "ItemData - server"));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Item option", "ItemData - server"));
	return HtmlString;	
}

function string _GetHtmlDetaiInfoSkill(int SkillID, int skillLv, int skillSubLv)
{
	local string HtmlString;
	local SkillInfo sInfo;

	GetSkillInfo(SkillID, skillLv, skillSubLv, sInfo);
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Level", string(sInfo.SkillLevel)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Operate type", string(sInfo.OperateType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Target type", string(sInfo.TargetType)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Affect scope", string(sInfo.AffectScope)));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Operate cond", "SkillData - server"));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Abnormal type", "SkillData - server"));
	return HtmlString;	
}

private function string GetHtmlDetaiInfoSkill(int Index)
{
	local LVDataRecord overRec;

	m_hFindTreeList.GetRec(Index, overRec);
	return _GetHtmlDetaiInfoSkill(int(overRec.nReserved1), int(overRec.nReserved2), 0);	
}

private function string GetHtmlDetailInfoNPC(int Index)
{
	local string HtmlString;
	local LVDataRecord overRec;
	local UserInfo uInfo;

	m_hFindTreeList.GetRec(Index, overRec);
	GetUserInfo(int(overRec.LVDataList[1].szData), uInfo);
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Clan", "NPCData - server"));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Clan hep range", "NPCData - server"));
	AddHtmlString(HtmlString, HtmlMakeTitleNDesc("Npc ai", "NPCData - server"));
	return HtmlString;	
}

private function string GetHtmlDetailInfo(int Index)
{
	local string HtmlString;

	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			return GetHtmlDetaiInfoItem(Index);
		case EListType.LISTTYPE_SKILL/*3*/:
			return GetHtmlDetaiInfoSkill(Index);
		case EListType.LISTTYPE_NPC/*1*/:
			return GetHtmlDetailInfoNPC(Index);
	}
	return HtmlString;
}

private function AddHtmlString(out string HtmlString, string AddString)
{
	if(AddString == "")
	{
		return;
	}
	HtmlString = HtmlString $ AddString;	
}

function string _HtmlQuestnameTableAdd(string Title)
{
	return HtmlQuestnameTableAdd(Title);
}

private function string HtmlQuestnameTableAdd(string Title)
{
	local string hex;

	hex = class'L2UIColor'.static.Inst()._Color2ZeroX(class'L2UIColor'.static.Inst().Sandrift);
	return "<table width=290 cellpadding=0 border=0 cellspacing=0><tr><td width=26><img src = \"L2UI_CT1.Minimap.Minimap_OpenGuideWnd\" width=26 height=26></td><td width=254 align=left valign=bottom><font color=\"" $ hex $ "\" name=GameDefault>" $ Title $ "</font></td></tr></table>";	
}

private function string HtmlItemnFullName(int ClassID)
{
	local int W;
	local ItemInfo iInfo;
	local string HTML, textureHtml;

	class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(ClassID), iInfo);
	if(iInfo.Enchanted > 0)
	{
		HTML = GetEnchantedHtml(iInfo.Enchanted) $ " ";
	}
	if(iInfo.IsBlessedItem)
	{
		HTML = HTML $ GetNameHtmlBress() $ " ";
	}
	HTML = HTML $ GetNameHtml(iInfo);
	if(Len(iInfo.AdditionalName) > 0)
	{
		HTML = HTML @ (GetNameHtmlAddionalName(iInfo.AdditionalName));
	}
	HTML = htmlAddTableTD(HTML, "left", "bottom", 0, 22);
	textureHtml = GetNameHtmlGradeIcon(iInfo.CrystalType, W);
	if(textureHtml != "")
	{
		HTML = HTML $ htmlAddTableTD(" " $ textureHtml, "right", "bottom", W + 5, 24, "", true);
	}
	return "<table width=0 cellpadding=0 border=0 cellspacing=0><tr><td width=26><img src = \"L2UI_CT1.Minimap.Minimap_OpenGuideWnd\" width=26 height=26></td>" $ HTML $ "</tr></table>";	
}

function string _MakeHtmlTable(string HTML)
{
	return MakeHtmlTable(HTML);	
}

private function string MakeHtmlTable(string HTML)
{
	return "<table width=294 border=0 cellpadding=1 cellspacing=2 background=L2UI_CT1.GroupBox.GroupBox_DF>" $ HTML $ "</table>";	
}

function string _HtmlMakeTitleNDesc(string Title, string Desc)
{
	return HtmlMakeTitleNDesc(Title, Desc);	
}

private function string HtmlMakeTitleNDesc(string Title, string Desc)
{
	return "<tr><td width=140 align=right>" $ Title $ "</td><td width=1></td><td width=150 align=left>" $ htmlfontAdd(" " $ Desc, class'L2UIColor'.static.Inst()._Color2ZeroX(class'L2UIColor'.static.Inst().Sandrift)) $ "</td></tr>";	
}

private function string htmlfontAdd(string strText, optional string FontColor)
{
	local string targetHtml;

	if(FontColor == "")
	{
		FontColor = "d3c5ae";
	}
	targetHtml = "<font color=\"" $ FontColor $ "\"" $ ">" $ strText $ "</font>";
	return targetHtml;	
}

private function DelegateOnKeyUP(EInputKey nKey)
{
	switch(nKey)
	{
		case EInputKey.IK_Enter/*13*/:
			FindByListTypeWithClear();
			showListNum_OnTick = 3000;
			break;
	}	
}

private function DelegateESCKey()
{
	OnReceivedCloseUI();
}

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

private function DelegateOnClearEditBox()
{
	DelegateOnEditEditBox("");
}

private function DelegateOnEditEditBox(string Text)
{
	showListNum_OnTick = SHOWLISTNUM_ONTICK_10;
	ClearOnTickInfos();
	ClearList();
	FindByListType();	
}

private function ShowSubWindow()
{
	m_ChkBoxLv1.HideWindow();
	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			m_GMFindTreeWndItem.ShowWindow();
			m_GMFindTreeWndSkill.HideWindow();
			m_hbtnDetailInfo.EnableWindow();
			break;
		case EListType.LISTTYPE_SKILL/*3*/:
			m_GMFindTreeWndItem.HideWindow();
			m_GMFindTreeWndSkill.ShowWindow();
			m_hbtnDetailInfo.EnableWindow();
			m_ChkBoxLv1.ShowWindow();
			break;
		case EListType.LISTTYPE_NPC/*1*/:
			m_GMFindTreeWndItem.HideWindow();
			m_GMFindTreeWndSkill.HideWindow();
			m_hbtnDetailInfo.EnableWindow();
			break;
		default:
			m_GMFindTreeWndItem.HideWindow();
			m_GMFindTreeWndSkill.HideWindow();
			m_hbtnDetailInfo.DisableWindow();
			break;
	}
}

function ShowList(string findString, EListType listType)
{
	curListType = listType;
	if(uicontrolFindTextInputScr.GetString() != findString)
	{
		uicontrolFindTextInputScr.SetString(findString);
		return;
	}
	m_hComboBox.SetSelectedNum(curListType);
	FindByListTypeWithClear();
	if(findString != "")
	{
		showListNum_OnTick = 3000;
	}
	ShowSubWindow();
	Me.ShowWindow();
	Me.SetFocus();	
}

private function FindByListTypeWithClear()
{
	bFindStop = false;
	Me.DisableTick();
	ClearOnTickInfos();
	showListNum_OnTick = SHOWLISTNUM_ONTICK_10;
	ClearList();
	FindByListType();	
}

private function bool FindByListType()
{
	local string findString;
	local bool isEnd;

	findString = uicontrolFindTextInputScr.GetString();
	switch(curListType)
	{
		case LISTTYPE_ITEM/*0*/:
			isEnd = FindAllItem(findString);
			break;
		case LISTTYPE_QUEST/*2*/:
			if(getInstanceUIData().getIsClassicServer())
			{
				isEnd = FindAllNQuest(findString);				
			}
			else
			{
				isEnd = FindAllQuest(findString);
			}
			break;
		case LISTTYPE_NPC/*1*/:
			isEnd = FindAllNPC(findString);
			break;
		case LISTTYPE_SKILL/*3*/:
			isEnd = FindAllSkill(findString);
			break;
		case LISTTYPE_CLASS/*4*/:
			isEnd = FindAllCLASS(findString);
			break;
		case LISTTYPE_SYSTEMSTRING/*5*/:
			isEnd = FindAllSystemString(findString);
			break;
		case LISTTYPE_SYSTEMMESSAGE/*6*/:
			isEnd = FindAllSystemMessage(findString);
			break;
	}
	if(isEnd)
	{
		SetListNum();		
	}
	else
	{
		if(IsFindStop())
		{
			SetListFindingStop();
		}
		else
		{
			SetListFindingNum();
		}
	}
	return isEnd;	
}

private function bool ChkOver500()
{
	local bool isOver;

	isOver = findOnTickNum >= (OnTickNum + 1) * showListNum_OnTick;
	findOnTickNum++;
	return isOver;	
}

private function SetListNum()
{
	m_hTbTotal.SetText(GetSystemString(14135));
	m_hTbFoundResultCnt.SetText(string(m_hFindTreeList.GetRecordCount()));
	GetWindowHandle(m_WindowName $ ".AutoAllArrowOn_texture").HideWindow();
	GetWindowHandle(m_WindowName $ ".findingIcon").HideWindow();	
}

private function SetListFindingNum()
{
	m_hTbTotal.SetText(GetSystemString(3513));
	m_hTbFoundResultCnt.SetText(string(findOnTickNum - 1));
	if(showListNum_OnTick == SHOWLISTNUM_ONTICK_10)
	{
		GetWindowHandle(m_WindowName $ ".AutoAllArrowOn_texture").HideWindow();		
	}
	else
	{
		GetWindowHandle(m_WindowName $ ".AutoAllArrowOn_texture").ShowWindow();
	}
	GetWindowHandle(m_WindowName $ ".findingIcon").ShowWindow();	
}

private function SetListFindingStop()
{
	m_hTbTotal.SetText("??? ????");
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".findingIcon").HideWindow();
	GetWindowHandle(m_WindowName $ ".findingIcon").HideWindow();
	GetWindowHandle(m_WindowName $ ".AutoAllArrowOn_texture").HideWindow();	
}

private function bool FindAllCLASS(string a_Param)
{
	local int classIndex;
	local String fullNameString;

	if(findOnTickCurrentID != 0)
	{
		classIndex = findOnTickCurrentID;
	}

	for ( classIndex = classIndex ; classIndex < 500 ; classIndex++ )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = classIndex;
			Me.EnableTick();
			return false;
		}
		fullNameString = GetSystemString ( class'UIDataManager'.static.GetClassnameSysstringIndexByClassIndex( classIndex ));
		if(fullNameString == "")
		{
			continue;
		}
		InsertRecordFindTreeList(fullNameString, a_Param, classIndex, GetClassRoleIconName(classIndex));
	}
	findOnTickCurrentID = -1;
	return true;
}

private function bool FindAllSystemString(string a_Param)
{
	local int i;
	local String fullNameString;

	if(findOnTickCurrentID != 0)
	{
		i = findOnTickCurrentID;
	}

	for ( i = i ; i < 20000 ; i++ )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = i;
			Me.EnableTick();
			return false;
		}
		fullNameString = GetSystemString ( i ) ;	
		if(fullNameString == "")
		{
			continue;
		}
		InsertRecordFindTreeList(fullNameString, a_Param, i, "");
	}
	findOnTickCurrentID = -1;
	return true;
}

private function bool FindAllSystemMessage(string a_Param)
{
	local int i;
	local String fullNameString;

	if(findOnTickCurrentID != 0)
	{
		i = findOnTickCurrentID;
	}

	for ( i = i ; i < 20000 ; i++ )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = i;
			Me.EnableTick();
			return false;
		}
		fullNameString = GetSystemMessage ( i ) ;
		if(fullNameString == "")
		{
			continue;
		}
		InsertRecordFindTreeList(fullNameString, a_Param, i, "");
	}
	return true;
	findOnTickCurrentID = -1;
}

private function bool FindAllNQuest(string a_Param)
{
	local int qid;
	local string Type;
	local NQuestUIData questUIData;

	if(findOnTickCurrentID == 0)
	{
		qid = class'UIDATA_QUEST'.static.GetNFirstID();		
	}
	else
	{
		qid = findOnTickCurrentID;
	}
	
	for(qid = qid; -1 != qid; qid = class'UIDATA_QUEST'.static.GetNNextID())
	{
		if(! API_GetNQuestData(qid, questUIData))
		{			
		}
		else
		{
			if(ChkOver500())
			{
				findOnTickCurrentID = qid;
				Me.EnableTick();
				return false;
			}
			switch(questUIData.Type)
			{
				case NQT_ONETIME:
					Type = GetSystemString(862);
					break;
				case NQT_DAILY:
					Type = GetSystemString(2788);
					break;
				case NQT_WEEKLY:
					Type = GetSystemString(14389);
					break;
				case NQT_REPEAT:
					Type = GetSystemString(861);
					break;
			}
			InsertRecordFindTreeList(questUIData.Name @ "(" $ Type $ ")", a_Param, questUIData.Id, GetMainTypeTexture(questUIData));
		}
	}
	findOnTickCurrentID = -1;
	return true;	
}

private function string GetMainTypeTexture(NQuestUIData questUIData)
{
	if(questUIData.Id < 20001)
	{
		return "L2UI_NEWTEX.QUESTWND.QICON_MAIN";		
	}
	else if(questUIData.Id < 30001)
	{
		return "L2UI_NEWTEX.QUESTWND.QICON_SUB";
	}
	return "L2UI_NEWTEX.QUESTWND.QICON_ESPECIAL";	
}

private function bool FindAllQuest(string a_Param)
{
	local int Id;
	local string fullNameString, IconName;
	local int QuestType;

	if(findOnTickCurrentID == 0)
	{
		Id = class'UIDATA_QUEST'.static.GetFirstID();		
	}
	else
	{
		Id = findOnTickCurrentID;
	}
	
	for( Id = Id; -1 != Id ; Id = class'UIDATA_QUEST'.static.GetNextID() )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = Id;
			Me.EnableTick();
			return false;
		}
		fullNameString = class'UIDATA_QUEST'.static.GetQuestName( Id );
		QuestType = class'UIDATA_QUEST'.static.GetQuestType(Id, 1);

		switch( QuestType )
		{
			case 0:
			case 2:
				IconName = "L2UI_CH3.QUESTWND.QuestWndInfoIcon_1";
				break;
			case 1:
			case 3:
				IconName = "L2UI_CH3.QUESTWND.QuestWndInfoIcon_2";
				break;
			case 4:
			case 5:
				IconName = "L2UI_CH3.QUESTWND.QuestWndInfoIcon_3";
				break;
		}
		InsertRecordFindTreeList(fullNameString, a_Param, Id, IconName);
	}
	findOnTickCurrentID = -1;
	return true;
}

private function bool FindAllSkill (string a_Param)
{
	local ItemID cID;
	local string fullNameString, IconName;
	local array<int> subLevelInfos;
	local bool canSkillEnchant;

	m_hFindTreeList.SetTooltipType("Skill");
	if(bFindStop)
	{
		return false;		
	}
	else
	{
		UISkillToolWnd(GetScript("UISkillToolWnd"))._SetFindStop();
	}
	if(findOnTickCurrentID == 0)
	{
		cID = class'UIDATA_SKILL'.static.GetFirstID();		
	}
	else
	{
		cID.ClassID = findOnTickCurrentID;
	}
	
	for ( cID = cID; IsValidItemID(cID); cID = class'UIDATA_SKILL'.static.GetNextID() )
	{
		if(lastSkillClassiD != cID.ClassID)
		{
			lastSkillClassiD = cID.ClassID;
			SkillLevel = 1;
		}
		if(ChkOver500())
		{
			findOnTickCurrentID = cID.ClassID;
			Me.EnableTick();
			return false;
		}
		// !???| ??????????? ??????
		fullNameString = class'UIDATA_SKILL'.static.GetName( cID, SkillLevel, 0 );
		IconName = class'UIDATA_SKILL'.static.GetIconName( cID, SkillLevel, 0 );
		IconName = Caps(IconName);
		canSkillEnchant = GetSkillSubLevelList(lastSkillClassiD, SkillLevel, subLevelInfos) > 1;
		InsertRecordFindTreeList(fullNameString, a_Param, lastSkillClassiD, IconName, canSkillEnchant);
		SkillLevel++;
		if(canSkillEnchant)
		{
			SkillNextIDByEnchantLevel(subLevelInfos.Length);
		}
	}
	findOnTickCurrentID = -1;
	lastSkillClassiD = -1;
	return true;
}

private function SkillNextIDByEnchantLevel(int subSkillNum)
{
	local int i;

	for(i = 1; i < subSkillNum; i++)
	{
		lastSkillClassiD = class'UIDATA_SKILL'.static.GetNextID().ClassID;
	}	
}

private function bool FindAllNPC(string a_Param)
{
	local int Id;
	local string fullNameString, nickname;

	m_hFindTreeList.SetTooltipType("SellItemList");
	if(findOnTickCurrentID == 0)
	{
		Id = class'UIDATA_NPC'.static.GetFirstID();		
	}
	else
	{
		Id = findOnTickCurrentID;
	}
	
	for( Id = Id; -1 != ID ; Id = class'UIDATA_NPC'.static.GetNextID() )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = Id;
			Me.EnableTick();
			return false;
		}
		fullNameString = class'UIDATA_NPC'.static.GetNPCName(Id);
		nickname = class'UIDATA_NPC'.static.GetNPCNickName(Id);
		if(nickname != "")
		{
			fullNameString = fullNameString @ "(" $ nickname $ ")";
		}
		InsertRecordFindTreeList(fullNameString, a_Param, Id + 1000000, "");
	}
	findOnTickCurrentID = -1;
	return true;
}

private function bool FindAllItem(string a_Param)
{
	local ItemID cID;
	local string fullNameString;
	local string AdditionalName;
	local int itemNameClass;
	local string IconName;

	m_hFindTreeList.SetTooltipType("SellItemList");
	if(findOnTickCurrentID == 0)
	{
		cID = class'UIDATA_ITEM'.static.GetFirstID();		
	}
	else
	{
		cID.ClassID = findOnTickCurrentID;
	}
	
	for ( cID = cID; IsValidItemID(cID); cID = class'UIDATA_ITEM'.static.GetNextID() )
	{
		if(ChkOver500())
		{
			findOnTickCurrentID = cID.ClassID;
			Me.EnableTick();
			return false;
		}
		fullNameString = Class'UIDATA_ITEM'.static.GetItemName(cID);
		itemNameClass = Class'UIDATA_ITEM'.static.GetItemNameClass(cID);
		AdditionalName = Class'UIDATA_ITEM'.static.GetItemAdditionalName(cID);
		IconName = Class'UIDATA_ITEM'.static.GetItemTextureName(cID);
		IconName = Caps(IconName);
		if ( itemNameClass == 0 )
		{
			fullNameString = MakeFullSystemMsg(GetSystemMessage(2332),fullNameString);
		}
		else if ( itemNameClass == 2 )
		{
			fullNameString = MakeFullSystemMsg(GetSystemMessage(2331),fullNameString);
		}
		if ( Len(AdditionalName) > 0 )
		{
			fullNameString = fullNameString $ "(" $ AdditionalName $ ")";
		}
		InsertRecordFindTreeList(fullNameString, a_Param, cID.ClassID, IconName);
	}
	findOnTickCurrentID = -1;
	return true;
}


private function LVDataRecord InsertIconData(LVDataRecord record, string iconName)
{
	local int nTextureWH;
	local int nTextureUV;
	local ItemInfo Info;

	record.LVDataList[0].hasIcon = true;

	switch (curListType)
	{
		case LISTTYPE_ITEM:
			nTextureWH = 32;
			if ( m_checkbox64size.IsChecked() )
			{
				nTextureUV = 64;
			} else {
				nTextureUV = 32;
			}
			Info = GetItemInfoByClassID(int(Record.nReserved1));
			if(Info.IconPanel != "")
			{
				Record.LVDataList[0].panelWidth = nTextureWH;
				Record.LVDataList[0].panelHeight = nTextureWH;
				Record.LVDataList[0].panelUL = nTextureUV;
				Record.LVDataList[0].panelVL = nTextureUV;
				Record.LVDataList[0].panelOffsetXFromIconPosX = 0;
				Record.LVDataList[0].panelOffsetYFromIconPosY = 0;
				Record.LVDataList[0].iconPanelName = Info.IconPanel;
			}
			break;
		case LISTTYPE_SKILL:
			nTextureWH = 32;
			if ( m_checkbox64size.IsChecked() )
			{
				nTextureUV = 64;
			} else {
				nTextureUV = 32;
			}
			break;
		case LISTTYPE_CLASS:
			nTextureWH = 32;
			nTextureUV = 16;
			break;
		case LISTTYPE_QUEST:
			nTextureWH = 32;
			nTextureUV = 16;
			break;
		default:
			nTextureWH = 32;
			nTextureUV = 16;
			break;
	}		

	record.LVDataList[0].nTextureWidth  = nTextureWH;
	record.LVDataList[0].nTextureHeight = nTextureWH;
	record.LVDataList[0].nTextureU = nTextureUV;
	record.LVDataList[0].nTextureV = nTextureUV;
		
	record.LVDataList[0].szTexture = iconName; 
	record.LVDataList[0].IconPosX= 2 ;
	record.LVDataList[0].FirstLineOffsetX= 2;

	return record;
}

private function int InsertRecordFindTreeList( string fullNameString , string a_param, int Id, string iconName, optional bool canSkillEnchant) 
{
	local string modifiedString, itemParam;
	local LVDataRecord record;
	local int IconType, skillIconType;
	local string iconTypeString;
	local ItemID localItemId;
	local int Length;
	local string groupTypeString;
	local int groupType, skillGroupType;
	
	modifiedString = Substitute(fullNameString, " ", "", false);
	if((FindMatchString(modifiedString, uicontrolExecptionTextInputScr.GetString())) != -1)
	{
		return 0;
	}
	
	
	if (!(((FindMatchString(modifiedString, a_Param) != -1) || (a_Param == "")) || (a_Param == string(Id))))
	{
		return 0;
	}
	
	Record.LVDataList.Length = 2;
	Record.LVDataList[0].bUseTextColor = true;
	Record.LVDataList[0].TextColor = GetColor(176, 153, 121, 255);
	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			// ??????? ??????????? ??????? ??????????? ?????????????????????? ????????? ???????  <- ??????????????? ????? api
			class'UIDATA_ITEM'.static.GetItemInfoString(Id, itemParam);
			Record.szReserved = itemParam;
			Record.nReserved1 = Id;
			break;
		case EListType.LISTTYPE_SKILL/*3*/:
			if(m_ChkBoxLv1.IsChecked())
			{
				if(SkillLevel > 1)
				{
					return 0;
				}
			}
			iconTypeString = GMFindTreeWndSkill(m_GMFindTreeWndSkill.GetScript()).m_ebIconType.GetString();
			Length = Len(iconTypeString);
			if(Length > 0)
			{
				IconType = int(iconTypeString);
				localItemId.ClassID = Id;
				skillIconType = class'UIDATA_SKILL'.static.GetIconType(localItemId, SkillLevel, 0);
				if(IconType != skillIconType)
				{
					return m_hFindTreeList.GetRecordCount();
				}
			}
			groupTypeString = GMFindTreeWndSkill(m_GMFindTreeWndSkill.GetScript()).m_ebGroupType.GetString();
			if(Len(groupTypeString) > 0)
			{
				groupType = int(groupTypeString);
				localItemId.ClassID = Id;
				skillGroupType = class'UIDATA_SKILL'.static.GetGroupType(localItemId, SkillLevel, 0);
				if(groupType != skillGroupType)
				{
					return m_hFindTreeList.GetRecordCount();
				}
			}
			Record.LVDataList[1].nReserved1 = SkillLevel;
			fullNameString = fullNameString @ "Lv" $ string(Record.LVDataList[1].nReserved1);
			Record.nReserved1 = Id;
			Record.nReserved2 = SkillLevel;
			Record.nReserved3 = 0;
			break;
		case EListType.LISTTYPE_NPC/*1*/:
			break;
	}
	if(IconName != "")
	{
		Record = InsertIconData(Record, IconName);
	}
	Record.LVDataList[0].szData = fullNameString;
	Record.LVDataList[1].szData = string(Id);
	Record.LVDataList[1].bUseTextColor = canSkillEnchant;
	Record.LVDataList[1].TextColor = class'L2UIColor'.static.Inst().VIOLET01;
	m_hFindTreeList.InsertRecord(Record);
	return m_hFindTreeList.GetRecordCount();
}

private function SummonItemCheck(INT64 Cnt)
{
	local LVDataRecord Record;
	local ItemInfo iInfo;

	if(! GetSelectedListCtrlItem(Record))
	{
		return;
	}
	class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(int(Record.LVDataList[1].szData)), iInfo);
	if(EEtcItemType(iInfo.EtcItemType) == ITEME_PET_COLLAR)
	{
		ProcessChatMessage(("//summon2" @ string(Cnt)) @ Record.LVDataList[1].szData);		
	}
	else
	{
		ProcessChatMessage(("//summon" @ Record.LVDataList[1].szData) @ string(Cnt));
	}	
}

private function Summon(Int64 cnt)
{
	local LVDataRecord Record;

	if(GetSelectedListCtrlItem(Record))
	{
		if ( Record.LVDataList[1].szData != "")
		{
			switch( curListType )
			{
				case LISTTYPE_ITEM:
					SummonItemCheck(Cnt);
					break;
				case LISTTYPE_NPC:
					ProcessChatMessage("//summon" @ Record.LVDataList[1].szData @ cnt);
					break;
				case LISTTYPE_SKILL:				
					ProcessChatMessage("//setskill" @ Record.LVDataList[1].szData @ Record.LVDataList[1].nReserved1 @ 0);
					break;
				case LISTTYPE_CLASS:				
					ProcessChatMessage("//setclass" @ Record.LVDataList[1].szData);
					break;
				case LISTTYPE_QUEST:
					if(getInstanceUIData().getIsClassicServer())
					{
						UIQuestToolWnd(GetScript("UIQuestToolWnd"))._ShowNQuestContextMenu(int(Record.LVDataList[1].szData));						
					}
					else
					{
						ProcessChatMessage(("//setquest" @ Record.LVDataList[1].szData) @ string(0));
					}
					break;
				case LISTTYPE_SYSTEMSTRING:
					ClipboardCopy(Record.LVDataList[0].szData);
					ProcessChatMessage("///ss" @ Record.LVDataList[1].szData);
					break;
				case LISTTYPE_SYSTEMMESSAGE:
					ClipboardCopy(Record.LVDataList[0].szData);
					ProcessChatMessage("///sm" @ Record.LVDataList[1].szData);
					break;
			}
		}
	}
}

private function ToggleItemDescWNd()
{
	local WindowHandle wnd;

	wnd = GetWindowHandle("ItemDescWnd");
	if(wnd.IsShowWindow())
	{
		GetWindowHandle("ItemDescWnd").HideWindow();		
	}
	else
	{
		HandleSetDescWindow(m_hFindTreeList.GetSelectedIndex());
		GetWindowHandle("ItemDescWnd").ShowWindow();
	}	
}

private function HandleBtnSummon()
{
	switch(curListType)
	{
		case EListType.LISTTYPE_ITEM/*0*/:
			openDialogNumpad(1);
			break;
		case EListType.LISTTYPE_QUEST/*2*/:
			Summon(1);
			break;
		case EListType.LISTTYPE_NPC/*1*/:
			openDialogNumpad(1);
			break;
		case EListType.LISTTYPE_SKILL/*3*/:
			Summon(1);
			break;
		case EListType.LISTTYPE_SYSTEMSTRING/*5*/:
			Summon(1);
			break;
		case EListType.LISTTYPE_SYSTEMMESSAGE/*6*/:
			Summon(1);
			break;
	}	
}

private function bool GetSelectedListCtrlItem(out LVDataRecord record)
{
	local int index;

	index = m_hFindTreeList.GetSelectedIndex();
	if( index >= 0 )
	{
		m_hFindTreeList.GetRec(index, record);
		return true;
	}
	return false;
}

// List related operations
private function ClearList()
{
	m_hFindTreeList.DeleteAllItem();
	m_hFindTreeList.ClearTooltip();
	m_hFindTreeList.SetTooltipType("");
	m_hBtnSummon.DisableWindow();
}

private function ClearOnTickInfos()
{
	OnTickNum = 0;
	findOnTickNum = 0;
	findOnTickCurrentID = 0;	
}

/** Desc : 다이얼로그 윈도우를 열고 OK 버튼 누른 경우 */
private function HandleDialogOK()
{	
	//local ItemInfo scInfo;
	local INT64 inputNum;
	local int id;

	if(!class'UICommonAPI'.static.DialogIsOwnedBy( string(Self) ))	return;	

	id = class'UICommonAPI'.static.DialogGetID();
	
	if ( id == ASK_SUMMON_NUMBER )
	{
		inputNum = INT64( class'UICommonAPI'.static.DialogGetString() );
		//summonCnt=m_hEdSummonCnt.GetString();
		Summon( inputNum );		
	}	
} 

private function openDialogNumpad(int64 num)
{
	local LVDataRecord selectedRec;
	local string dialogMsg;
	local ItemInfo iInfo;

	m_hFindTreeList.GetSelectedRec(selectedRec);

	// Ask price
	class'UICommonAPI'.static.DialogSetID( ASK_SUMMON_NUMBER );
	//DialogSetReservedItemID( info.ID );				// ServerID
	//DialogSetReservedInt3( int(bAllItem) );		// 전체이동이면 개수 묻는 단계를 생략한다
	class'UICommonAPI'.static.DialogSetEditType("number");
	class'UICommonAPI'.static.DialogSetParamInt64( num );
	class'UICommonAPI'.static.DialogSetDefaultOK();
	dialogMsg = selectedRec.LVDataList[0].szData;
	if(curListType == EListType.LISTTYPE_ITEM/*0*/)
	{
		class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(int(selectedRec.LVDataList[1].szData)), iInfo);
		if(byte(iInfo.EtcItemType) == 7)
		{
			dialogMsg = selectedRec.LVDataList[0].szData $ "\\n\\n - 레벨";
		}
	}
	class'UICommonAPI'.static.DialogShow(DialogModalType_Modalless, DialogType_NumberPad, dialogMsg, string(self));
}

private function int FindMatchString(string modifiedString, string a_Param)
{
	local string delim;

	delim = " ";
	if(StringMatching(modifiedString, a_Param, delim))
	{
		return 1;		
	}
	else
	{
		return -1;
	}
	return 1;	
}

function _SetFindStop()
{
	bFindStop = true;	
}

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

private function SetWindowSize()
{
	if(m_checkbox64size.IsChecked())
	{
		Me.SetWindowSize(369, 666);		
	}
	else
	{
		Me.SetWindowSize(369, 586);
	}	
}

private function bool API_GetNQuestData(int a_QuestID, out NQuestUIData o_data)
{
	return GetNQuestData(a_QuestID, o_data);	
}

defaultproperties
{
	m_WindowName="GMFindTreeWnd"
}
