////////////////////////////////
//~ sixten: Debug Info Functions

per_thread debug_info *ThreadLocal_DebugInfo = 0;

//- sixten: manage state
static debug_info *DI_DebugInfoAlloc(void)
{
	arena *Arena = ArenaAlloc(Kilobytes(1), true, "Debug Info Arena");
	debug_info *Result = PushStruct(Arena, debug_info);
	Result->Arena = Arena;
	return(Result);
}

static void DI_BeginFrame(debug_info *DebugInfo)
{
	ThreadLocal_DebugInfo = DebugInfo;
	DebugInfo->FrameTemp = BeginTemp(DebugInfo->Arena);
}

static void DI_EndFrame()
{
	
}

//- sixten: user interface
static void DI_Info(string Message)
{
	debug_info *DebugInfo = ThreadLocal_DebugInfo;
	debug_info_node *Node = PushStruct(DebugInfo->Arena, debug_info_node);
	Node->Message = PushString(DebugInfo->Arena, Message);
	DLLInsertLast(DebugInfo->First, DebugInfo->Last, Node);
}

static void DI_InfoF(char *Format, ...)
{
	debug_info *DebugInfo = ThreadLocal_DebugInfo;
	va_list Arguments;
	va_start(Arguments, Format);
	temp Scratch = GetScratch();
	string Message = PushFormatVariadic(DebugInfo->Arena, Format, Arguments);
	DI_Info(Message);
	ReleaseScratch(Scratch);
	va_end(Arguments);
}

static void DI_BuildInfo(void)
{
	debug_info *DebugInfo = ThreadLocal_DebugInfo;
	AC_AnimateValueDirect(DebugInfo->Open, 0.3f, &DebugInfo->OpenT);
	if(!DLLIsEmpty(DebugInfo->First))
	{
		UI_Tooltip
		{
			UI_SetNextBackgroundColor(SetAlpha(Theme_BackgroundColor, 0.5f));
			UI_SetNextSize(UI_Em(15, 1), UI_ChildrenSum(1, 1));
			UI_SetNextCornerRadius(4.0f);
			UI_SetNextHoverCursor(PlatformCursor_ArrowAll);
			ui_box *Box = UI_MakeBox(UI_BoxFlag_DrawBackground|UI_BoxFlag_DrawBorder|UI_BoxFlag_Clickable|
                                     UI_BoxFlag_HotAnimation|UI_BoxFlag_FloatingX|UI_BoxFlag_FloatingY|UI_BoxFlag_Clip,
                                     StrLit("DI Container"));
			UI_Parent(Box)
			{
				UI_Size(UI_Percent(1, 1), UI_ChildrenSum(1, 1)) UI_Row() UI_Width(UI_TextContent(15, 1)) UI_Height(UI_TextContent(15, 1))
				{
					UI_Font(Font_Bold) UI_LabelF("Debug Info");
					UI_Spacer(UI_Percent(1, 0));
					UI_Font(Font_Icons)
					{
						ui_box *ExpandBox = UI_MakeBoxF(UI_BoxFlag_DrawText|UI_BoxFlag_Clickable|UI_BoxFlag_HotAnimation, "%U", DebugInfo->Open?FontIcon_DownDir:FontIcon_RightDir);
						ui_signal ExpandBoxSignal = UI_SignalFromBox(ExpandBox);
						if(ExpandBoxSignal.Pressed)
						{
							DebugInfo->Open = !DebugInfo->Open;
						}
					}
				}
				
				UI_Height(UI_ChildrenSum(DebugInfo->OpenT, 1)) UI_Column()
				{
					UI_Width(UI_TextContent(15, 1)) UI_Height(UI_TextContent(15, 1))
						for(debug_info_node *Node = DebugInfo->First; Node != 0; Node = Node->Next)
					{
						ui_box *MessageBox = UI_MakeBoxF(UI_BoxFlag_DrawText, "%p", Node);
						UI_EquipBoxText(MessageBox, Node->Message);
					}
				}
			}
			
			ui_signal Signal = UI_SignalFromBox(Box);
			if(Signal.Dragging)
			{
				if(Signal.Pressed)
				{
					UI_StoreDragV2(DebugInfo->RelativeP);
				}
				
				v2_r32 StartP = UI_GetDragV2();
				v2_r32 EndP = StartP + Signal.DragDelta;
				DebugInfo->RelativeP = EndP;
			}
			Box->FixedP = DebugInfo->RelativeP;
		}
	}
	// sixten: after building we assume that the data should be released
	EndTemp(DebugInfo->FrameTemp);
	DebugInfo->First = DebugInfo->Last = 0;
}