#include "MW4Headers.hpp"
#include "hudcomp.hpp"
#include "huddebug.hpp"
#include "mwguimanager.hpp"

using namespace MechWarrior4;

HUDDebug::HUDDebug()
{
	m_Messages.clear ();
	Location (Point3D (0,0,0.9f));
	Color (0,125,0,250);
}

HUDDebug::~HUDDebug()
{
	ClearData ();
}

void HUDDebug::ClearData (void)
{
	stlport::list<DebugData>::iterator iter;

	for (iter = m_Messages.begin ();iter != m_Messages.end ();iter++)
	{
		delete iter->text;
	}
	m_Messages.clear ();
}

void HUDDebug::AddData (char *name,void *data,HUDDEBUG_DATA_TYPE type)
{
#ifdef LAB_ONLY
	RemoveData (data);
	HUDText *text;
	text = new HUDText ();

	text->UpdateText (name);
	text->Color (255,255,255,255);
	text->Justification (HUDText::LEFT_ALIGN);
	m_Messages.push_back (DebugData (text,name,data,type));
#endif
}

void HUDDebug::RemoveData (void *data)
{
	stlport::list<DebugData>::iterator iter;

	for (iter = m_Messages.begin ();iter != m_Messages.end ();iter++)
	{
		if (iter->data == data)
		{
			delete iter->text;
			m_Messages.erase (iter);
			return;
		}
	}
}

void HUDDebug::DrawImplementation(void)
{
#ifdef LAB_ONLY
	Point3D loc,size;

	if (!m_Messages.size ())
	{
		return;
	}
	Size (Point3D (192,m_Messages.size ()*16.0f,0.9f));
	loc = Location ();
	size = Size ();

	DWORD bcolor = BrighterColor (Color ());
	DrawRect (loc,size,MakeColor (5,15,5,200));
	DrawFrame (loc,size,bcolor);

	loc.y += 2;
	loc.x += 2;
	size.x -= 4;
	Point3D r,s;
	r.z = 0.0f;
	DWORD chatx,chaty;
	stlport::list<DebugData>::iterator iter;

	r.x = loc.x;
	r.y = loc.y;
	for (iter = m_Messages.begin ();iter != m_Messages.end ();iter++)
	{
		char buf[512];
		switch (iter->datatype)
		{
			case hud_int_type:
				{
					int *temp = (int *) iter->data;
					sprintf (buf,"%s: %d",iter->name,*temp);
				}
				break;
			case hud_float_type:
				{
					float *temp = (float *) iter->data;
					sprintf (buf,"%s: %5.2f",iter->name,*temp);
				}
				break;
			case hud_double_type:
				{
					double *temp = (double *) iter->data;
					sprintf (buf,"%s: %5.2f",iter->name,*temp);
				}
				break;
			case hud_string_type:
				{
					char *temp = (char *) iter->data;
					sprintf (buf,"%s: %s",iter->name,temp);
				}
				break;
		}
		iter->text->UpdateText (buf);
		iter->text->TopLeft (loc.x,loc.y);
		iter->text->BottomRight (loc.x+size.x-2,loc.y+size.y-2);
		iter->text->Color (255,255,255,255);
		iter->text->DrawSize (chatx,chaty);
		iter->text->Draw (r);
		r.y += chaty;
	}
#endif
}


namespace MechWarrior4
{
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void AddDebugData (char *name,void *data,HUDDEBUG_DATA_TYPE datatype)		// see huddebug.hpp for data types
	{
#ifdef LAB_ONLY
		MWGUIManager *gui;
		gui = MWGUIManager::GetInstance ();
		if (!gui)
			return;
		HUDDebug *hud;
		hud = (HUDDebug *) gui->Component (MWGUIManager::HUD_DEBUG);
		if (!hud)
			return;
		hud->AddData (name,data,(HUDDebug::HUDDEBUG_DATA_TYPE) datatype);
#endif
	}
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void ClearAllDebugData (void)
	{
#ifdef LAB_ONLY
		MWGUIManager *gui;
		gui = MWGUIManager::GetInstance ();
		if (!gui)
			return;
		HUDDebug *hud;
		hud = (HUDDebug *) gui->Component (MWGUIManager::HUD_DEBUG);
		if (!hud)
			return;
		hud->ClearData ();
#endif
	}
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void RemoveDebugData (void *data)
	{
#ifdef LAB_ONLY
		MWGUIManager *gui;
		gui = MWGUIManager::GetInstance ();
		if (!gui)
			return;
		HUDDebug *hud;
		hud = (HUDDebug *) gui->Component (MWGUIManager::HUD_DEBUG);
		if (!hud)
			return;
		hud->RemoveData (data);
#endif
	}
}
