class UIPortraitSetter extends UICommonAPI;

var EditBoxHandle NCEditBox0;
var CharacterViewportWindowHandle characterViewport;

event OnLoad()
{
	SetClosingOnESC();
	NCEditBox0 = GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".NCEditBox0");
	characterViewport = GetCharacterViewportWindowHandle("QuestDialogWnd.viewport");	
}

event OnClickButton(string strBtn)
{
	Debug("strBtn :" @ strBtn);
	switch(strBtn)
	{
		case "spawn":
			characterViewport.SetNPCInfo(int(NCEditBox0.GetString()));
			characterViewport.SpawnNPC();
			break;
		case "ResetBtn":
			Reset();
			break;
	}	
}

event OnModifyCurrentTickSliderCtrl(string strID, int iCurrentTick)
{
	local float fTick;

	Debug("OnModifyCurrentTickSliderCtrl" @ strID @ string(iCurrentTick));
	switch(strID)
	{
		case "SliderCtrlScale":
			fTick = float(iCurrentTick) / 100;
			if(fTick == 0)
			{
				fTick = 0.010f;
			}
			SetFValueString(strID, fTick);
			characterViewport.SetCharacterScale(fTick);
			characterViewport.SetNPCInfo(int(NCEditBox0.GetString()));
			characterViewport.SpawnNPC();
			break;
		case "SliderCtrlCameraDistance":
			characterViewport.SetCameraDistance(iCurrentTick);
			SetValueString(strID, iCurrentTick);
			break;
		case "SliderCtrlYaw":
			characterViewport.SetCurrentRotation(iCurrentTick);
			SetValueString(strID, iCurrentTick);
			break;
		case "SliderCtrlOffSetX":
			iCurrentTick = iCurrentTick - 50;
			characterViewport.SetCharacterOffsetX(iCurrentTick);
			SetValueString(strID, iCurrentTick);
			break;
		case "SliderCtrlOffSetY":
			iCurrentTick = iCurrentTick - 50;
			characterViewport.SetCharacterOffsetY(iCurrentTick);
			SetValueString(strID, iCurrentTick);
			break;
		case "SliderCtrlOffSetZ":
			iCurrentTick = iCurrentTick - 50;
			characterViewport.SetCharacterOffsetZ(iCurrentTick);
			SetValueString(strID, iCurrentTick);
			break;
	}	
}

event OnCompleteEditBox(string strID)
{
	local SliderCtrlHandle slider;
	local int Value;

	switch(strID)
	{
		case "NCEditBox0":
			OnClickButton("spawn");
			return;
		case "txtICurTickOffSetX":
		case "txtICurTickOffSetY":
		case "txtICurTickOffSetZ":
			Value = int(GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ "." $ strID).GetString()) + 50;
			break;
		case "txtIcurTickScale":
			Value = int(float(GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ "." $ strID).GetString()) * 100);
			break;
		default:
			Value = int(GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ "." $ strID).GetString());
			break;
	}
	slider = GetSliderCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".SliderCtrl" $ Right(strID, Len(strID) - 11));
	slider.SetCurrentTick(Value);	
}

event OnShow()
{
	if(! IsBuilderPC())
	{
		return;
	}
	if(GetReleaseMode() != RM_DEV)
	{
		return;
	}	
}

function _SetCurrentQuestInfo()
{
	local NQuestUIData questUIData;

	if(API_GetNQuestData(QuestDialogWnd(GetScript("QuestDialogWnd"))._currentQuestID, questUIData))
	{
		SetPortrait(questUIData);
		m_hOwnerWnd.ShowWindow();
	}	
}

private function SetPortrait(NQuestUIData questUIData)
{
	local NQuestNpcPortraitUIData questNpcPortraitUIData;
	local int questPortraitID;
	local QuestDialogWnd QuestDialogWndScr;

	QuestDialogWndScr = QuestDialogWnd(GetScript("QuestDialogWnd"));
	switch(QuestDialogWndScr._currentDialogType)
	{
		case QuestDialogWndScr.QUESTDialogType.Start:
		case QuestDialogWndScr.QUESTDialogType.Accept:
			questPortraitID = questUIData.StartNPC.Id;
			break;
		case QuestDialogWndScr.QUESTDialogType.complete:
		case QuestDialogWndScr.QUESTDialogType.End:
			questPortraitID = questUIData.EndNPC.Id;
			break;
	}
	API_GetNQuestNpcPortraitData(questPortraitID, questNpcPortraitUIData);
	Debug("SetPortrait --- " @ string(questPortraitID));
	NCEditBox0.SetString(string(questPortraitID));
	SetFValue("scale", questNpcPortraitUIData.ViewScale);
	SetFValue("Yaw", float(questNpcPortraitUIData.ViewRotationYaw));
	SetFValue("CameraDistance", float(questNpcPortraitUIData.ViewDist));
	SetFValue("OffsetX", float(questNpcPortraitUIData.ViewOffsetX));
	SetFValue("OffsetY", float(questNpcPortraitUIData.ViewOffsetY));
	SetFValue("OffsetZ", float(questNpcPortraitUIData.ViewOffsetZ));
	characterViewport.SetNPCInfo(questPortraitID);
	characterViewport.SpawnNPC();
	SetAllCurTick();	
}

private function bool API_GetNQuestNpcPortraitData(int a_NpcID, out NQuestNpcPortraitUIData o_data)
{
	return GetNQuestNpcPortraitData(1000000 + a_NpcID, o_data);	
}

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

private function Reset()
{
	_SetCurrentQuestInfo();
	SetAllCurTick();	
}

private function SetAllCurTick()
{
	OnCompleteEditBox("txtICurTickScale");
	OnCompleteEditBox("txtICurTickCameraDistance");
	OnCompleteEditBox("txtICurTickYaw");
	OnCompleteEditBox("txtICurTickOffSetX");
	OnCompleteEditBox("txtICurTickOffSetY");
	OnCompleteEditBox("txtICurTickOffSetZ");
	OnCompleteEditBox("txtICurTickOffSetX");
	OnCompleteEditBox("txtICurTickTimer");	
}

private function SetValueString(string strID, int Value)
{
	SetValue(Right(strID, Len(strID) - 10), Value);	
}

private function SetValue(string typeString, int Value)
{
	GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtICurTick" $ typeString).SetString(string(Value));	
}

private function SetFValueString(string strID, float Value)
{
	SetFValue(Right(strID, Len(strID) - 10), Value);	
}

private function SetFValue(string typeString, float Value)
{
	GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtICurTick" $ typeString).SetString(string(Value));	
}

private function string GetValueString(string strID)
{
	return GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtICurTick" $ Right(strID, Len(strID) - 10)).GetString();	
}

private function string GetEditorString(string typeString)
{
	return GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".txtICurTick" $ typeString).GetString();	
}

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