#include "MW4Headers.hpp"
#include "hudcomp.hpp"
#include "hudobj.hpp"
#include "objective.hpp"

// MSL 5.05 Team Objective Display
//#include "mwapplication.hpp"
#include "vehicleinterface.hpp"

using namespace MechWarrior4;

HUDObjective::HUDObjective()
{
// MSL 5.05 Shift Main Screen Objective Location
//	Location (Point3D (242.0f,78.0f,0.9f));
	Location (Point3D (242.0f,8.0f,0.9f));
	Size (Point3D (316.0f,150.0f,0.9f));
	Color (0,125,0,250);
}

HUDObjective::~HUDObjective()
{
	KillList ();
}

void HUDObjective::KillList (void)
{
	stlport::vector<ObjectiveData>::iterator iter;
	for (iter = m_Objectives.begin ();iter != m_Objectives.end ();iter++)
	{
		delete iter->m_Text;
		iter->m_Text = NULL;
		iter->m_Objective = NULL;
	}
	m_Objectives.clear ();
}

void HUDObjective::AddObjective (Objective *obj)
{
	Verify (obj);
	HUDText *text;
	text = new HUDText ();
	text->UpdateText (obj->CurrentText(true));
	text->Justification(HUDText::LEFT_ALIGN);
	text->SetSize (HUDText::SMALL_SIZE);
	m_Objectives.push_back (ObjectiveData (text,obj));
}

void HUDObjective::Reset (void)
{
	
	KillList ();
	int tablescan[] =
	{
		NameTable::ObjectiveArray
	};
	int size,i,j;
	NameTable *table = NameTable::GetInstance();
	Check_Object (table);
	for (i=0;i<sizeof (tablescan)/sizeof (int);i++)
	{
		size = table->nameTableArray[tablescan[i]].GetLength ();
		for (j=0;j<size;j++)
		{
			NameTableEntry *data;
			Entity *who;

			data = &table->nameTableArray [tablescan[i]][j];

			who = data->dataPointer->GetCurrent ();
			Verify (who->IsDerivedFrom (Objective::DefaultData));
			Objective *obj;
			obj = Cast_Object (Objective *,who);
			if (!obj->HelpMessage ())
			{
				AddObjective (Cast_Object (Objective *,who));
			}
		}
	}

	inherited::Reset ();
}

void HUDObjective::DrawImplementation(void)
{
	int count;
	Point3D loc,size;
	Scalar yvalue;
	stlport::vector<ObjectiveData>::iterator iter;

	loc = Location ();
	count = 0;
	yvalue = 0;
	DWORD bcolor = BrighterColor (Color ());
	DWORD dcolor = DarkerColor (Color ());
	Size (Point3D (316.0f,400.0f,0.9f));

	size = Size ();
	for (iter = m_Objectives.begin ();iter != m_Objectives.end ();iter++)
	{
		Verify (iter->m_Objective);
		Verify (iter->m_Text);
// MSL 5.05 Team Based Objective
		Vehicle* vehicle = Cast_Object(Vehicle*,VehicleInterface::GetInstance()->vehicle);
		if ((vehicle->GetAlignment() == iter->m_Objective->GetAlignment()) || (iter->m_Objective->GetAlignment() == Entity::DefaultAlignment))
		{
			if (iter->m_Objective->Visible ())
			{
				const char *text;
				text = iter->m_Objective->CurrentText (true);
				if (text[0] == 0)
					continue;
				iter->m_Text->UpdateText (text);
				switch (iter->m_Objective->Status ())
				{
					case Objective::Objective_Failed:
						iter->m_Text->Color (255,0,0,255); // Red
						break;
					case Objective::Objective_Succeeded:
						iter->m_Text->Color (bcolor); // Bright Green
						break;
					case Objective::Objective_Neutral:
						iter->m_Text->Color (255,255,255,255); // White
						break;
				}
				iter->m_Text->TopLeft (loc.x+2,loc.y+2);
				iter->m_Text->BottomRight (loc.x+size.x-4,loc.y+size.y-4);
				iter->m_Text->Wrap (true);
				DWORD drawx,drawy;
				iter->m_Text->DrawSize (drawx,drawy);
				yvalue += drawy;
				yvalue += 3;
				count++;
			}
		}
	}
	if (count == 0)
		return;

	Size (Point3D (316.0f,yvalue,0.9f));

	size = Size ();
	DrawRect (loc,size,MakeColor (5,15,5,200));
	DrawSpecFrame (loc,size,bcolor);
	yvalue = loc.y;

	yvalue += 3;
	loc.x += 3;
	size.x -= 6;
//	int newx,newy;
	for (iter = m_Objectives.begin ();iter != m_Objectives.end ();iter++)
	{
		Verify (iter->m_Objective);
		Verify (iter->m_Text);
// MSL 5.05 Team Based Objective
		Vehicle* vehicle = Cast_Object(Vehicle*,VehicleInterface::GetInstance()->vehicle);
		if ((vehicle->GetAlignment() == iter->m_Objective->GetAlignment()) || (iter->m_Objective->GetAlignment() == Entity::DefaultAlignment))
		{
			if (iter->m_Objective->Visible ())
			{
				DWORD textx,texty;
				const char *text;
				text = iter->m_Objective->CurrentText (true);
				if (text[0] == 0)
					continue;

				iter->m_Text->DrawSize (textx,texty);
				iter->m_Text->Draw (Point3D (loc.x+2,yvalue,0.9f));
//				gos_TextGetPrintPosition (&newx,&newy);
//				newy = (int) (newy / g_AdjustMultY);
				yvalue +=texty+3;
//				yvalue+=15;
			}
		}
	}
}


