#include "MAXProxyHeaders.hpp"

//
//############################################################################
//##########################    MAXIndex    ###########################
//############################################################################
//

MemoryBlock*
	MAXIndex::AllocatedMemory = NULL;
MAXIndex::ClassData*
	MAXIndex::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MAXIndex::InitializeClass()
{
	Verify(!AllocatedMemory);
	AllocatedMemory =
		new MemoryBlock(
			sizeof(MAXIndex),
			10,
			10,
			"MAXIndex"
		);
	Register_Object(AllocatedMemory);

	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			MAXIndexClassID,
			"MAXIndex",
			IndexProxy::DefaultData
		);
	Register_Object(DefaultData);
}

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

	Unregister_Object(AllocatedMemory);
	delete AllocatedMemory;
	AllocatedMemory = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MAXIndex::Destroy()
{
	Check_Object(this);
	Verify(referenceCount == 1);
	DetachReference();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MAXIndex::MAXIndex(
	MAXPolygon *polygon,
	MAXVertex *vertex
):
	Proxies::IndexProxy(DefaultData, polygon, vertex)
{
	Check_Pointer(this);
	Check_Object(polygon);
	Check_Object(vertex);
	Check_Object(this);
}

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Proxies::IndexProxy*
	MAXIndex::UseNextIndexProxy()
{
	Check_Object(this);
	Check_Object(vertexProxy);

	MAXVertex *vertex = GetVertexProxy();
	Check_Object(vertex);
	int current_index = vertex->GetVertexIndex();

	MAXPolygonMesh *mesh_proxy = vertex->GetPolygonMeshProxy();
	Check_Pointer(mesh_proxy);

	int number_verts = mesh_proxy->GetNumVertices();

	if (current_index+1 < number_verts)
	{
		vertex = MAXVertex::MakeProxy(vertex->GetPolygonMeshProxy(),
									current_index+1,
									0,
									mesh_proxy->GetVertex(current_index+1),-1);
		Register_Object(vertex);
		Proxies::IndexProxy *proxy = MakeProxy(GetPolygonProxy(), vertex);
		Register_Object(proxy);
		Verify(vertex->GetReferenceCount() == 2);
		vertex->DetachReference();
		return proxy;

	}
	return NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Proxies::IndexProxy*
	MAXIndex::UsePreviousIndexProxy()
{
	Check_Object(this);
	Check_Object(vertexProxy);

	MAXVertex *vertex = GetVertexProxy();
	Check_Object(vertex);
	int current_index = vertex->GetVertexIndex();

	MAXPolygonMesh *mesh_proxy = vertex->GetPolygonMeshProxy();
	Check_Pointer(mesh_proxy);

	int number_verts = mesh_proxy->GetNumVertices();

	if (current_index-1 > -1)
	{
		vertex = MAXVertex::MakeProxy(vertex->GetPolygonMeshProxy(),
									current_index-1,
									0,
									mesh_proxy->GetVertex(current_index-1),-1);
		Register_Object(vertex);
		Proxies::IndexProxy *proxy = MakeProxy(GetPolygonProxy(), vertex);
		Register_Object(proxy);
		Verify(vertex->GetReferenceCount() == 2);
		vertex->DetachReference();
		return proxy;

	}
	return NULL;
}
