/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "core/core_profiler.h"
		#include "core/raster_font.h"
		#include "core/renderer.h"

		using namespace GS;

#if	__ENABLE_ALLOCATION_STAT__
static	Time	time_last;
#endif

//--------------------------------------------------------------------------
void			Core::DrawAllocProfilerText(Render::Renderer &render, Render::RasterFont *font[2], float &x, float &y, float w, float h)
{
#if		__ENABLE_ALLOCATION_STAT__

	Time time = Platform::Get().GetTime();
	Time time_elapsed = Types::Max(nTime(1), time - Platform::Get().GetStartTime());

	// Update averages.
	Time time_delta = time - time_last;

	if	(time_delta.toSec() > 1)
	{
		for	(uint n = 0; n < nIAlloc::SystemCount; ++n)
		{
			nIAlloc::Stat &stat = nIAlloc::system_stat[n];
			stat.alloc_avg = stat.alloc_count / time_delta.toSec();
			stat.alloc_count = 0;
		}
		time_last = time;
	}

	//
	nColor title_color(1.f, 0.9f, 0);
	nRenderer::WriterConfig config(false);

	render.Write(*font[1], "Allocation System\n\n", x, y, config, 1, &title_color);
	float col, _col;

	// Output legend.
	#define	MOVE_COL(EXP)	{ EXP; _col = col; }
	MOVE_COL(col = x)

	render.Write(*font[0], "System", _col, y, config);
	MOVE_COL(col += 120)

	render.Write(*font[0], "| Commit", _col, y, config);
	MOVE_COL(col += 90)
	render.Write(*font[0], "| Commit Peak", _col, y, config);
	MOVE_COL(col += 90)

	render.Write(*font[0], "| Alive", _col, y, config);
	MOVE_COL(col += 90)
	render.Write(*font[0], "| Alive Peak", _col, y, config);

	MOVE_COL(col += 90)
	render.Write(*font[0], "| Pressure (a/s)", _col, y, config);

	y += 16 + 8;

	// Output per system stats.
	String current_group;

	for	(uint n = 0; n < nIAlloc::SystemCount; ++n)
	{
		// Output allocation group on change.
		if	(current_group != nIAlloc::system_desc[n].group)
		{
//				y += 4;
//				MOVE_COL(col = x)
//				render.Write(*font[0], nIAlloc::system_desc[n].group, _col, y, config, 1, &title_color);
//				y += 16 + 8;
			current_group = nIAlloc::system_desc[n].group;
		}

		// Output statistics.
		nIAlloc::Stat &stat = nIAlloc::system_stat[n];
		MOVE_COL(col = x)

		render.Write(*font[0], nIAlloc::system_desc[n].name, _col, y, config);
		MOVE_COL(col += 120)

		render.Write(*font[0], String::Format("| %s", FormatNumber((float)stat.size, MemorySize).c_str()), _col, y, config);
		MOVE_COL(col += 90)
		render.Write(*font[0], String::Format("| %s", FormatNumber((float)stat.size_peak, MemorySize).c_str()), _col, y, config);
		MOVE_COL(col += 90)

		render.Write(*font[0], String::Format("| %d", stat.alive_count), _col, y, config);
		MOVE_COL(col += 90)
		render.Write(*font[0], String::Format("| %d", stat.alive_count_peak), _col, y, config);

		MOVE_COL(col += 90)
		render.Write(*font[0], String::Format("| %d", stat.alloc_avg), _col, y, config);

		y += 16;
	}

	// Totals.
	size_t	total_size = 0,
			total_size_peak = 0;
	uint	total_count = 0,
			total_count_peak = 0,
			total_pressure = 0;

	for	(uint n = 0; n < nIAlloc::SystemCount; ++n)
	{
		nIAlloc::Stat &stat = nIAlloc::system_stat[n];

		total_size += stat.size;
		total_size_peak += stat.size_peak;
		total_count += stat.alive_count;
		total_count_peak += stat.alive_count_peak;
		total_pressure += stat.alloc_avg;
	}

	y += 8;

	MOVE_COL(col = x)

	render.Write(*font[0], "Total", _col, y, config);
	MOVE_COL(col += 120)

	render.Write(*font[0], String::Format("| %s", FormatNumber((float)total_size, MemorySize).c_str()), _col, y, config);
	MOVE_COL(col += 90)
	render.Write(*font[0], String::Format("| %s", FormatNumber((float)total_size_peak, MemorySize).c_str()), _col, y, config);
	MOVE_COL(col += 90)

	render.Write(*font[0], String::Format("| %d", total_count), _col, y, config);
	MOVE_COL(col += 90)
	render.Write(*font[0], String::Format("| %d", total_count_peak), _col, y, config);

	MOVE_COL(col += 90)
	render.Write(*font[0], String::Format("| %d", total_pressure), _col, y, config);

#endif
}
//--------------------------------------------------------------------------
