#include "MW4Headers.hpp"

#include "Narc.hpp"
#include "MWMission.hpp"
#include <Adept\EntityManager.hpp>
#include "mech.hpp"
#include "vehicleinterface.hpp"

//#############################################################################
//###############################    Narc    ###############################
//#############################################################################

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Narc::ClassData*
	Narc::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Narc::InitializeClass()
{
	Check_Object(Narc::ExecutionStateEngine::DefaultData);
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			NarcClassID,
			"MechWarrior4::Narc",
			BaseClass::DefaultData,
			0, NULL,
			(Entity::Factory)Make,
			(Entity::CreateMessage::Factory)CreateMessage::ConstructCreateMessage,
			ExecutionStateEngine::DefaultData,
			(Entity::GameModel::Factory)GameModel::ConstructGameModel,
			NULL,
			(Entity::GameModel::ReadAndVerifier)GameModel::ReadAndVerify,
			(Entity::GameModel::ModelWrite)GameModel::WriteToText,
			(Entity::GameModel::ModelSave)GameModel::SaveGameModel
		);
	Register_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Narc::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Narc*
	Narc::Make(
		CreateMessage *message,
		ReplicatorID *base_id
	)
{
	Check_Object(message);
	gos_PushCurrentHeap(Heap);
	Check_Object(EntityManager::GetInstance());
	Narc *new_entity;
	Entity *entity = EntityManager::GetInstance()->RequestFromArmory(message->dataListID);
	if (entity)
	{
		new_entity = Cast_Object(Narc*, entity);
		new_entity->Reuse(message, base_id);
	}
	else
		new_entity = new Narc(DefaultData, message, base_id, NULL);
	gos_PopCurrentHeap();
	Check_Object(new_entity);
	return new_entity;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Narc::Narc(
	ClassData *class_data,
	CreateMessage *message,
	ReplicatorID *base_id,
	ElementRenderer::Element *element
):
	StickyMover(class_data, message, base_id, element)
{
	Check_Pointer(this);
	Check_Object(message);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Narc::~Narc()
{
	Check_Object(this);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Narc::OnActiveTimeStart()
{
	Check_Object(this);
	Check_Object(Mission::GetInstance());

	if (attachedEntity.GetCurrent ())
	{
		if (attachedEntity.GetCurrent ()->IsDerivedFrom(MWMover::DefaultData) == true)
		{
			MWMover* mover = Cast_Object(MWMover*,attachedEntity.GetCurrent ());
			MWObject *parent;
			parent = mover->GetParentVehicle ();
			if (parent)
			{
				if (parent->IsDerivedFrom (Mech::DefaultData))
				{
					Mech *mech;
					mech = Cast_Object (Mech *,parent);
					if (mech->vehicleInterface)
					{
						mech->vehicleInterface->ReactToEvent (VehicleInterface::NARC_HIT);
					}
				}
			}
		}
	}
	if(Mission::GetInstance()->IsDerivedFrom(MWMission::DefaultData))
	{
		MWMission *temp_mission;
		temp_mission = Cast_Object(MWMission *, Mission::GetInstance());
		temp_mission->AddNarc(this,GetAlignment());
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Narc::OnActiveTimeEnd()
{
	Check_Object(this);
	Check_Object(Mission::GetInstance());

	if(Mission::GetInstance()->IsDerivedFrom(MWMission::DefaultData))
	{
		MWMission *temp_mission;
		temp_mission = Cast_Object(MWMission *, Mission::GetInstance());
		temp_mission->m_narcs.Remove(this);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Narc::TestInstance() const
{
	Verify(IsDerivedFrom(DefaultData));
}


