class QuestConfirmWnd extends UICommonAPI;

const DIALOG_TOOLTIP_KEY0 = "\\#$Tooltip0";

var private TextBoxHandle txtQuestTitle;
var private TextBoxHandle txtQuestCategory;
var private TextBoxHandle txtRecommandedLevel;
var private TextBoxHandle txtRecommandedLevelText;
var private TextBoxHandle txtQuestType;
var private TextBoxHandle txtQuestTarget;
var private TextBoxHandle txtQuestRewardItemTreeTitle;
var private TreeHandle QuestRewardItemTree;
var private HtmlHandle txtQuestInfo;
var L2Util util;

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

event OnLoad()
{
	Initialize();
	util = L2Util(GetScript("L2Util"));	
}

function Initialize()
{
	txtQuestTitle = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestTitle");
	txtQuestCategory = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestCategory");
	txtRecommandedLevel = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtRecommandedLevel");
	txtRecommandedLevelText = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtRecommandedLevelText");
	txtQuestType = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestType");
	txtQuestTarget = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestTarget");
	txtQuestRewardItemTreeTitle = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestRewardItemTreeTitle");
	QuestRewardItemTree = GetTreeHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".QuestRewardItemTree");
	txtQuestInfo = GetHtmlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtQuestInfo");	
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "confirmlBtn":
			HandoeClickConfirm();
			break;
		case "cancelBtn":
			m_hOwnerWnd.HideWindow();
			break;
	}	
}

event OnHide()
{
	allclear();	
}

event OnShow()
{
	m_hOwnerWnd.SetFocus();	
}

private function HandoeClickConfirm()
{
	Debug("퀘스트 수락");
	m_hOwnerWnd.HideWindow();
	class'QuestDialogWnd'.static.Inst()._ConfirmQuest();	
}

function _ShowQuestEnd()
{
	GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".confirmlBtn").SetButtonName(898);
	SetCurrentQuestInfos(false);	
}

function _ShowQuestConfirm()
{
	GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".confirmlBtn").SetButtonName(7422);
	SetCurrentQuestInfos(true);	
}

private function SetCurrentQuestInfos(bool bConfirm)
{
	local NQuestUIData qUIData;
	local QuestDialogWnd QuestDialogWndScr;
	local NQuestDialogUIData questDialogUIData;
	local string QuestInfo;

	QuestDialogWndScr = class'QuestDialogWnd'.static.Inst();
	if(! GetNQuestData(QuestDialogWndScr._currentQuestID, qUIData))
	{
		return;
	}
	if(QuestDialogWndScr._currentQuestID < QuestDialogWndScr.const.QUEST_CATEGORY_MAIN)
	{
		return;
	}
	txtQuestTitle.SetText(qUIData.Name);
	if(QuestDialogWndScr._currentQuestID < QuestDialogWndScr.const.QUEST_CATEGORY_SUB)
	{
		txtQuestCategory.SetText(GetSystemString(2738));		
	}
	else if(QuestDialogWndScr._currentQuestID < QuestDialogWndScr.const.QUEST_CATEGORY_SPECIAL)
	{
		txtQuestCategory.SetText(GetSystemString(2341));			
	}
	else
	{
		txtQuestCategory.SetText(GetSystemString(1796));
	}
	txtRecommandedLevel.SetText(GetSystemString(922) @ ":");
	if(qUIData.LevelMax > 0 && qUIData.LevelMin > 0)
	{
		txtRecommandedLevelText.SetText(string(qUIData.LevelMin) $ "~" $ string(qUIData.LevelMax));		
	}
	else if(qUIData.LevelMin > 0)
	{
		txtRecommandedLevelText.SetText(string(qUIData.LevelMin) $ " " $ GetSystemString(859));			
	}
	else
	{
		txtRecommandedLevelText.SetText(GetSystemString(866));
	}

	switch(qUIData.Type)
	{
		case NQT_ONETIME:
			txtQuestType.SetText(GetSystemString(862));
			break;
		case NQT_DAILY:
			txtQuestType.SetText(GetSystemString(2788));
			break;
		case NQT_WEEKLY:
			txtQuestType.SetText(GetSystemString(14389));
			break;
		case NQT_REPEAT:
			txtQuestType.SetText(GetSystemString(861));
			break;
	}
	if(qUIData.Goal.Num > 1)
	{
		txtQuestTarget.SetText(qUIData.Goal.Name $ "x" $ string(qUIData.Goal.Num));		
	}
	else
	{
		txtQuestTarget.SetText(qUIData.Goal.Name);
	}
	API_GetNQuestDialogData(QuestDialogWndScr._currentQuestID, questDialogUIData);
	QuestInfo = questDialogUIData.QuestInfo;
	ReplaceText(QuestInfo, DIALOG_TOOLTIP_KEY0 $ "S", "");
	ReplaceText(QuestInfo, DIALOG_TOOLTIP_KEY0 $ "E", "");
	txtQuestInfo.LoadHtmlFromString(htmlSetHtmlStart(QuestInfo));
	setRewardItem(qUIData.Reward);
	m_hOwnerWnd.ShowWindow();	
}

private function InsertRewardItems(array<NQuestRewardItemData> Items)
{
	local string treeName;
	local int i;
	local string setTreeName, strRetName, IconName, ItemName;

	treeName = m_hOwnerWnd.m_WindowNameWithFullPath $ ".QuestRewardItemTree";
	TreeClear(treeName);
	util.TreeInsertRootNode(treeName, "List", "", 0, 2);
	setTreeName = "List";

	for(i = 0; i < Items.Length; i++)
	{
		strRetName = util.TreeInsertItemNode(treeName, string(i), setTreeName, false, -4, -2);
		if((float(i) % 2) == 1)
		{
			util.TreeInsertTextureNodeItem(treeName, strRetName, "L2UI_CH3.etc.textbackline", 244, 38,,,,, 14);			
		}
		else
		{
			util.TreeInsertTextureNodeItem(treeName, strRetName, "L2UI_CT1.EmptyBtn", 244, 38);
		}
		util.TreeInsertTextureNodeItem(treeName, strRetName, "L2UI_ct1.ItemWindow.ItemWindow_df_slotbox_2x2", 36, 36, -238, 2);
		IconName = class'UIDATA_ITEM'.static.GetItemTextureName(GetItemID(Items[i].ItemClassID));
		ItemName = class'UIDATA_ITEM'.static.GetItemName(GetItemID(Items[i].ItemClassID));
		util.TreeInsertTextureNodeItem(treeName, strRetName, IconName, 32, 32, -34, 3);
		util.TreeInsertTextNodeItem(treeName, strRetName, ItemName, 5, 6, util.ETreeItemTextType.COLOR_DEFAULT, true,, Items[i].ItemClassID);
		if(Items[i].Amount == 0)
		{
			util.TreeInsertTextNodeItem(treeName, strRetName, GetSystemString(584), 48, -18, util.ETreeItemTextType.COLOR_GOLD,, true);
			// [Explicit Continue]
			continue;
		}
		switch(Items[i].ItemClassID)
		{
			case 57:
				util.TreeInsertTextNodeItem(treeName, strRetName, MakeFullSystemMsg(GetSystemMessage(2932), MakeCostString(string(Items[i].Amount)), ""), 44, -17, util.ETreeItemTextType.COLOR_GOLD,, true);
				break;
			case 15623:
			case 15624:
			case 15625:
			case 15626:
			case 15627:
			case 15628:
			case 15629:
			case 15630:
			case 15631:
			case 15632:
			case 15633:
			case 47130:
				util.TreeInsertTextNodeItem(treeName, strRetName, MakeCostString(string(Items[i].Amount)), 44, -17, util.ETreeItemTextType.COLOR_GOLD,, true);
				break;
			case 95641:
				util.TreeInsertTextNodeItem(treeName, strRetName, MakeFullSystemMsg(GetSystemMessage(13405), MakeCostString(string(Items[i].Amount)), ""), 44, -17, util.ETreeItemTextType.COLOR_GOLD,, true);
				break;
			default:
				util.TreeInsertTextNodeItem(treeName, strRetName, MakeFullSystemMsg(GetSystemMessage(1983), MakeCostString(string(Items[i].Amount)), ""), 44, -17, util.ETreeItemTextType.COLOR_GOLD,, true);
				break;
		}
	}	
}

private function setRewardItem(NQuestRewardData rewardDatas)
{
	local int i;
	local array<NQuestRewardItemData> Items;
	local NQuestRewardItemData rewardItemData;

	if(rewardDatas.Level > 0)
	{
		rewardItemData.ItemClassID = 95641;
		rewardItemData.Amount = rewardDatas.Level;
		Items[Items.Length] = rewardItemData;
	}
	if(rewardDatas.Exp > 0)
	{
		rewardItemData.ItemClassID = 15623;
		rewardItemData.Amount = rewardDatas.Exp;
		Items[Items.Length] = rewardItemData;
	}
	if(rewardDatas.Sp > 0)
	{
		rewardItemData.ItemClassID = 15624;
		rewardItemData.Amount = rewardDatas.Sp;
		Items[Items.Length] = rewardItemData;
	}

	for(i = 0; i < rewardDatas.Items.Length; i++)
	{
		Items[Items.Length] = rewardDatas.Items[i];
	}
	InsertRewardItems(Items);	
}

function TreeClear(string Str)
{
	class'UIAPI_TREECTRL'.static.Clear(Str);	
}

function allclear()
{
	util.TreeClear(m_hOwnerWnd.m_WindowNameWithFullPath $ ".QuestRewardItemTree");
	txtQuestTitle.SetText("");
	txtRecommandedLevel.SetText("");
	txtRecommandedLevelText.SetText("");
	txtQuestType.SetText("");
	txtQuestTarget.SetText("");	
}

private function bool API_GetNQuestDialogData(int a_QuestID, out NQuestDialogUIData o_data)
{
	return GetNQuestDialogData(a_QuestID, o_data);	
}

private function string GetCurrentTeleportName()
{
	local int tID;
	local TeleportListAPI.TeleportListData tInfo;

	tID = class'QuestDialogWnd'.static.Inst()._GetCurrentTeleportID();

	for(tInfo = class'TeleportListAPI'.static.GetFirstTeleportListData(); "" != tInfo.Name; tInfo = class'TeleportListAPI'.static.GetNextTeleportListData())
	{
		if(tInfo.Id == tID)
		{
			return tInfo.Name;
		}
	}
	return "";	
}
