#include "stdafx.h"
#include "MW4GameEd.h"
#include "EditorChildWnd.h"
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <Adept\Map.hpp>
#include "MoveCommand.h"
#include "AddCommand.h"
#include <Adept\EntityManager.hpp>
#include <Adept\Mission.hpp>
#include <Adept\Connection.hpp>
#include <Adept\Application.hpp>
#include <MW4\Mech.hpp>
#include <MW4\Vehicle.hpp>
#include "InstanceWindow.h"
#include "GenericTextDlg.h"
#include "transDlg.h"
#include "DeleteCommand.h"
#include <Adept\Player.hpp>
#include "EditorWaypoint.h"
#include "GenericListDlg.h"
#include <mw4\AI.hpp>
#include <Adept\DropZone.hpp>
#include <Adept\NameTable.hpp>
#include <Adept\CollisionGrid.hpp>
#include "LatticeLinkDlg.h"
#include <MW4\CombatAI.hpp>
#include <MW4\MWTool.hpp>
#include <mw4\planeai.hpp>
#include <mw4\noncomai.hpp>
#include <mw4\noncom.hpp>
#include "ResourceWindow.h"


extern CMW4GameEdApp theApp;

void EditAIDriver (Entity *entity)
{
	Entity *AI_Entity;
	CGenericListDlg dlg;
	MWObject *obj;

	if (entity->IsDerivedFrom (MWObject::DefaultData))
		obj = Cast_Object (MWObject *,entity);
	else
		return;

	dlg.m_Title = "Attach Driver";
	dlg.m_Path = "Content\\AIPlayers";
	dlg.m_Extension = "data";

	if (dlg.DoModal() == IDOK)
	{
//		Entity* entity = (Entity*)TLDlg->m_ObjList.GetItemData(GetSelectedObject()), *AI_Entity;
		Check_Object(entity);

		Resource model_resource;
		NotationFile model_file(dlg.m_Path);
		Tool::Instance->ConstructDataList(&model_resource, &model_file);
		Check_Object(&model_resource);
		Verify(model_resource.DoesResourceExist());

		Entity::ClassID class_ID;
		class_ID = Entity::GetClassIDFromDataListID(model_resource.GetResourceID());
		Verify(class_ID != NullClassID);

		if (NonComAI::DefaultData->GetClassID () == class_ID)
		{
			if (!obj->IsDerivedFrom (NonCom::DefaultData))
			{
				MessageBox(NULL,"Can only attach a noncom ai to a noncom unit","AI Error",MB_OK);
				return;
			}
		}
		int pilotSkill = -1;
		int gunnerySkill = -1;
		int eliteSkill = -1;
		int minHeatSkill = -1;
		int maxHeatSkill = -1;
		int sensor = -1;
		int blind = -1;
		int longrange = -1;
		int shortrange = -1;

		int fred;

		Stuff::Scalar cells[MechWarrior4::DEFAULT_MEMORYCELL_VALUES];			// used in memory cell data
		for (fred=0;fred<DEFAULT_MEMORYCELL_VALUES;fred++)
			cells[fred] = 0;

		if (obj->m_AI)
		{
			Map::Instance->RemoveChild(obj->m_AI);
			NameTable::Instance->RemoveEntry(obj->m_AI->objectID);
		NameTable::Instance->IsValid();
			obj->m_AI->vehicle = NULL;
			obj->m_AI->SentenceToDeathRow ();
			obj->m_AI = NULL;
		}

		if (MechAI::DefaultData->GetClassID () == class_ID)
		{
			MechWarrior4::Mech* mech = Cast_Object(MechWarrior4::Mech*,entity);
			Check_Object(mech);


			MechAI::CreateMessage AI_Create_Message(
																						 sizeof(MechAI::CreateMessage),
																						 DefaultEventPriority,
																						 MechAI::CreateMessage::DefaultFlags,
																						 class_ID,
																						 MechAI::CreateMessage::DefaultFlags,
																						 model_resource.GetResourceID(),
																						 entity->GetLocalToWorld(),
																						 0.0f,
																						 Entity__ExecutionStateEngine::AlwaysExecuteState,
																						 NameTable::NullObjectID,
																						 Entity::DefaultAlignment,
																						 DropZone::Null,
																						 //			 NameTable::NullObjectID,
																						 entity->objectID,
																						 "",		// script name,  should be scripts\<filename>.abl
																						 cells,		// memory cell data
																						 pilotSkill,
																						 gunnerySkill,
																						 eliteSkill,
																						 minHeatSkill,
																						 maxHeatSkill,
																						 sensor,
																						 blind,
																						 longrange,
																						 shortrange,
																						 0,
																						 0,
																						 0
																						 );

			MemoryStream stream(&AI_Create_Message, AI_Create_Message.messageLength);
			ReplicatorID AI_id = Connection::Local->GetNextReplicatorID();
			AI_Entity = Entity::CreateEntity(&stream, &AI_id, false);
			Check_Object(AI_Entity);
			Verify(AI_Entity->IsDerivedFrom(MechAI::DefaultData));
		}
		else if (CombatAI::DefaultData->GetClassID () == class_ID)
		{
			Vehicle* vehicle = Cast_Object(Vehicle*,entity);
			Check_Object(vehicle);

			CombatAI::CreateMessage AI_Create_Message(
																							 sizeof(CombatAI::CreateMessage),
																							 DefaultEventPriority,
																							 CombatAI::CreateMessage::DefaultFlags,
																							 class_ID,
																							 CombatAI::DefaultFlags,
																							 model_resource.GetResourceID(),
																							 entity->GetLocalToWorld(),
																							 0.0f,
																							 Entity__ExecutionStateEngine::AlwaysExecuteState,
																							 NameTable::NullObjectID,
																							 Entity::DefaultAlignment,
																							 DropZone::Null,
																							 //			 NameTable::NullObjectID,
																							 entity->objectID,
																							 "",		// script name,  should be scripts\<filename>.abl
																							 cells,	// default memory cell values
																							 pilotSkill,
																							 gunnerySkill,
																							 eliteSkill,
																							 minHeatSkill,
																							 maxHeatSkill,
																						 sensor,
																						 blind,
																						 longrange,
																						 shortrange,
																							 0,
																							 0,
																							 0
																							 );

			MemoryStream stream(&AI_Create_Message, AI_Create_Message.messageLength);
			ReplicatorID AI_id = Connection::Local->GetNextReplicatorID();
			AI_Entity = Entity::CreateEntity(&stream, &AI_id, false);
			Check_Object(AI_Entity);
			Verify(AI_Entity->IsDerivedFrom(CombatAI::DefaultData));
		}
		else if (MoverAI::DefaultData->GetClassID () == class_ID)
		{
			MWMover* mover = Cast_Object(MWMover*,entity);
			Check_Object(mover);

			MoverAI::CreateMessage AI_Create_Message(
																							sizeof(MoverAI::CreateMessage),
																							DefaultEventPriority,
																							MoverAI::CreateMessage::DefaultFlags,
																							class_ID,
																							MoverAI::DefaultFlags,
																							model_resource.GetResourceID(),
																							entity->GetLocalToWorld(),
																							0.0f,
																							Entity__ExecutionStateEngine::AlwaysExecuteState,
																							NameTable::NullObjectID,
																							Entity::DefaultAlignment,
																							DropZone::Null,
																							//			 NameTable::NullObjectID,
																							entity->objectID,
																							"",		 // script name,  should be scripts\<filename>.abl
																							cells,	 // default memory cell values
																							pilotSkill,
																							gunnerySkill,
																							eliteSkill,
																							minHeatSkill,
																							maxHeatSkill,
																						 sensor,
																						 blind,
																						 longrange,
																						 shortrange,
																							0,
																							0,
																							0
																							);

			MemoryStream stream(&AI_Create_Message, AI_Create_Message.messageLength);
			ReplicatorID AI_id = Connection::Local->GetNextReplicatorID();
			AI_Entity = Entity::CreateEntity(&stream, &AI_id, false);
			Check_Object(AI_Entity);
			Verify(AI_Entity->IsDerivedFrom(MoverAI::DefaultData));
		}
		else if (PlaneAI::DefaultData->GetClassID () == class_ID)
		{
			MWMover* mover = Cast_Object(MWMover*,entity);
			Check_Object(mover);

			PlaneAI::CreateMessage AI_Create_Message(
																							sizeof(PlaneAI::CreateMessage),
																							DefaultEventPriority,
																							PlaneAI::CreateMessage::DefaultFlags,
																							class_ID,
																							PlaneAI::DefaultFlags,
																							model_resource.GetResourceID(),
																							entity->GetLocalToWorld(),
																							0.0f,
																							Entity__ExecutionStateEngine::AlwaysExecuteState,
																							NameTable::NullObjectID,
																							Entity::DefaultAlignment,
																							DropZone::Null,
																							//			 NameTable::NullObjectID,
																							entity->objectID,
																							"",		 // script name,  should be scripts\<filename>.abl
																							cells,	 // default memory cell values
																							pilotSkill,
																							gunnerySkill,
																							eliteSkill,
																							minHeatSkill,
																							maxHeatSkill,
																						 sensor,
																						 blind,
																						 longrange,
																						 shortrange,
																							0,
																							0,
																							0
																							);

			MemoryStream stream(&AI_Create_Message, AI_Create_Message.messageLength);
			ReplicatorID AI_id = Connection::Local->GetNextReplicatorID();
			AI_Entity = Entity::CreateEntity(&stream, &AI_id, false);
			Check_Object(AI_Entity);
			Verify(AI_Entity->IsDerivedFrom(PlaneAI::DefaultData));
		}
		else
		{
//			Vehicle* vehicle = Cast_Object(Vehicle*,entity);
			Check_Object(entity);

			AI::CreateMessage AI_Create_Message(
																				 sizeof(AI::CreateMessage),
																				 DefaultEventPriority,
																				 AI::CreateMessage::DefaultFlags,
																				 class_ID,
																				 AI::DefaultFlags,
																				 model_resource.GetResourceID(),
																				 entity->GetLocalToWorld(),
																				 0.0f,
																				 Entity__ExecutionStateEngine::AlwaysExecuteState,
																				 NameTable::NullObjectID,
																				 Entity::DefaultAlignment,
																				 DropZone::Null,
																				 //			 NameTable::NullObjectID,
																				 entity->objectID,
																				 "",		// script name,  should be scripts\<filename>.abl
																				 cells,	// default memory cell values
																				 pilotSkill,
																				 gunnerySkill,
																				 eliteSkill,
																				 minHeatSkill,
																				 maxHeatSkill,
																						 sensor,
																						 blind,
																						 longrange,
																						 shortrange,
																				 0,
																				 0,
																				 0
																				 );

			MemoryStream stream(&AI_Create_Message, AI_Create_Message.messageLength);
			ReplicatorID AI_id = Connection::Local->GetNextReplicatorID();
			AI_Entity = Entity::CreateEntity(&stream, &AI_id, false);
			Check_Object(AI_Entity);
			Verify(AI_Entity->IsDerivedFrom(AI::DefaultData));
		}
		Check_Object(EntityManager::Instance);
		EntityManager::Instance->GetNameSocket()->Remove(AI_Entity);
		int num=rand();
		AI_Entity->instanceName = theApp.resourceWindow->AssignObjectName(dlg.m_Name.GetBuffer(MAX_PATH), &num);
		EntityManager::Instance->GetNameSocket()->AddValue(AI_Entity, AI_Entity->instanceName);
		Check_Object(NameTable::Instance);
		NameTable::Instance->AddEntry(AI_Entity, NameTable::AIArray);
		NameTable::Instance->IsValid();
		Check_Object(Map::Instance);
		Map::Instance->AddChild(AI_Entity);

		AI_Entity->SetPropType(Entity::MissionPropType);
		AI_Entity->SyncMatrices(true);

		AI* ai;
		ai = Cast_Object(AI*,AI_Entity);

		ai->ConnectEntity(obj);

		obj->AttachAI(ai);
	}
}
