#include "stdafx.h"

#include "MW4GameEd.h"
#include "InsertPathNodeCommand.h"
#include <Adept\Player.hpp>
#include <Adept\Interface.hpp>
#include <Adept\Entity.hpp>
#include <Adept\EntityManager.hpp>
#include <Adept\Map.hpp>
#include "TransDlg.h"
#include "DisplayWindow.h"
#include "OverviewWindow.h"
#include "InstanceWindow.h"
#include "ResourceWindow.h"
#include "Selection.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
extern CMW4GameEdApp theApp;

InsertPathNodeCommand::InsertPathNodeCommand(EditorPathNode *beforeMe)
				: Command()
{
	Check_Object(beforeMe);

	Entity* entity = theApp.resourceWindow->CreateNewInstance("Content\\Editor\\Reserved\\PathNode\\PathNode.data",
															  "PathNode","PathNode");
	SPEW(("scottjan","Hey! These should probably not be hardcoded"));
	
	if (entity)
	{
		insertedNode = Cast_Object(EditorPathNode*,entity);
		beforeMe->InsertNode(insertedNode);
		insertedNode->SetLinkColor(Stuff::RGBAColor(1.0f,1.0f,0.0f,0.0f));

		insertedNode->ShowLink(true);
		insertedNode->GetElement()->SetVolumeCullMode();
		insertedNode->GetElement()->Sync();

 		Time till = gos_GetElapsedTime();
		insertedNode->PostCollisionExecute(till);

		CSelectionList::Instance->ClearSelectionList();
		CSelectionList::Instance->AddSelection(entity);
		CSelectionList::Instance->RemoveCollision();
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
InsertPathNodeCommand::~InsertPathNodeCommand()
{
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void InsertPathNodeCommand::Undo()
{
	Check_Object(insertedNode);

	CSelectionList::Instance->ClearSelectionList();

	EditorPathNode* n = insertedNode->nextnode;
	while (n)
	{
		n->index--;
		n = n->nextnode;
	}

	insertedNode->pathSlot.GetCurrent()->pathSlot.GetCurrent()->ErasePoint (insertedNode->index);

	if (insertedNode->prevnode)
	{
		insertedNode->prevnode->nextnode = insertedNode->nextnode;
		insertedNode->prevnode->UpdateLinkPointData();
	}
	if (insertedNode->nextnode)
	{
		insertedNode->nextnode->prevnode = insertedNode->prevnode;
		insertedNode->nextnode->UpdateLinkPointData();
	}

	insertedNode->SentenceToDeathRow();
	theApp.overviewWindow->DrawWindow();

	Check_Pointer(theApp.instanceWindow);
	theApp.instanceWindow->RemoveInstance(insertedNode);
	theApp.displayWindow->translationDlg->CheckObject(insertedNode);
}






//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
InsertBoundaryNodeCommand::InsertBoundaryNodeCommand(EditorBoundaryNode *beforeMe)
				: Command()
{
	Check_Object(beforeMe);

	Entity* tmpentity = theApp.resourceWindow->CreateNewInstance("Content\\Editor\\Reserved\\BoundaryNode\\BoundaryNode.data","MovementPath","MovementPath");
	EditorBoundaryNode *entity = Cast_Object(EditorBoundaryNode*,tmpentity);

	SPEW(("scottjan","Hey! I am an idiot. You guys will never make this thing work"));
	
	if (entity)
	{
		insertedNode = Cast_Object(EditorBoundaryNode*,entity);
		insertedNode->myBoundary=beforeMe->myBoundary;
		insertedNode->firstBoundaryNode=beforeMe->firstBoundaryNode;
		beforeMe->InsertNode(insertedNode);
		insertedNode->SetLinkColor(Stuff::RGBAColor(1.0f,1.0f,1.0f,0.0f));

		insertedNode->ShowLink(true);
		insertedNode->GetElement()->SetVolumeCullMode();
		insertedNode->GetElement()->Sync();

 		Time till = gos_GetElapsedTime();
		insertedNode->PostCollisionExecute(till);

		CSelectionList::Instance->ClearSelectionList();
		CSelectionList::Instance->AddSelection(entity);
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
InsertBoundaryNodeCommand::~InsertBoundaryNodeCommand()
{
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void InsertBoundaryNodeCommand::Undo()
{
	Check_Object(insertedNode);

	CSelectionList::Instance->ClearSelectionList();

	EditorBoundaryNode* n = insertedNode->nextnode;
	while (n->index != 0)
	{
		n->index--;
		n = n->nextnode;
	}

	if (insertedNode->prevnode)
	{
		insertedNode->prevnode->nextnode = insertedNode->nextnode;
		insertedNode->prevnode->UpdateLinkPointData();
	}
	if (insertedNode->nextnode)
	{
		insertedNode->nextnode->prevnode = insertedNode->prevnode;
		insertedNode->nextnode->UpdateLinkPointData();
	}

	insertedNode->myBoundary->SetLength(insertedNode->myBoundary->GetLength() - 1);

	Point3D point;
	n = insertedNode->firstBoundaryNode;
	do
	{
		point = n->GetLocalToWorld();
		(*(insertedNode->myBoundary))[n->index].x = point.x;
		(*(insertedNode->myBoundary))[n->index].y = point.z;
		n = n->nextnode;
	}
	while (n->index != 0);

	insertedNode->SentenceToDeathRow();
	theApp.overviewWindow->DrawWindow();

	Check_Pointer(theApp.instanceWindow);
	theApp.instanceWindow->RemoveInstance(insertedNode);
	theApp.displayWindow->translationDlg->CheckObject(insertedNode);
}
