#include "AdeptHeaders.hpp"

#include "VideoHeaders.hpp"
#include <MLR\MLRShape.hpp>
#include "Attribute.hpp"

using namespace MidLevelRenderer;

//############################################################################
//###############################  SlidingShapeComponent  ################################
//############################################################################

//
// Class Data Support
//
Component::ClassData*
	SlidingShapeComponent::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	SlidingShapeComponent::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			SlidingShapeComponentClassID,
			"Adept::SlidingShapeComponent",
			BaseClass::DefaultData,
			0,
			NULL
		);
	Check_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	SlidingShapeComponent::TerminateClass()
{
	Check_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SlidingShapeComponent::SlidingShapeComponent(
	ClassData *class_data,
	MemoryStream *stream,
	VideoComponentWeb *owning_web,
	Entity *watched_entity
):
	ShapeComponent(
		class_data,
		stream,
		owning_web
	)
{
	Check_Object(this);

	//
	//--------------------------------
	// Find the uv transform attribute
	//--------------------------------
	//
	int attribute_id;
	*stream >> attribute_id;
	m_entity = watched_entity;
	Check_Object(m_entity);
	m_attribute = m_entity->GetAttributeEntry(attribute_id);
	Check_Object(m_attribute);
	Verify(m_attribute->attributeType == AffineMatrix4DClassID);
	
	//
	//--------------
	// Find the mesh
	//--------------
	//
	ElementRenderer::Element *shape =
		Cast_Object(ElementRenderer::Element*, GetElement());
	MidLevelRenderer::MLRShape *mlr_shape = shape->GetMLRShape();
	Check_Object(mlr_shape);
	if (mlr_shape->GetNum() != 1)
		STOP(("Shape must have 1 mesh!"));
	MidLevelRenderer::MLRPrimitiveBase *prim = mlr_shape->Find(0);
	Check_Object(prim);
	
	//
	//-----------------------
	// Set up the UV pointers
	//-----------------------
	//
	int count;
	prim->GetTexCoordData(&m_originalUVs, &count);
	
	if (!prim->dataStore)
		prim->dataStore = new DataStorage;
	Check_Pointer(prim->dataStore);
	prim->dataStore->texCoords.SetLength (count);
	m_UVs = &prim->dataStore->texCoords;
	prim->SetTexCoordData(m_UVs->GetData(), count);
	
	Check_Pointer(prim->dataStore);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	SlidingShapeComponent::SkipStreamData(MemoryStream *stream)
{
	Check_Object(this);
	Check_Object(stream);

	BaseClass::SkipStreamData(stream);
	int attribute_id;
	*stream >> attribute_id;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SlidingShapeComponent*
	SlidingShapeComponent::Create(
		MemoryStream *stream,
		VideoComponentWeb *owning_web,
		Entity *entity
	)
{
	Check_Object(stream);
	Check_Object(owning_web);
	Component *component = DoesComponentExist(stream, owning_web);
	SlidingShapeComponent *object;
	if (component)
	{
		object = static_cast<SlidingShapeComponent*>(component);
		Check_Object(object);
		object->SkipStreamData(stream);
	}
	else
	{
		object = new SlidingShapeComponent(DefaultData, stream, owning_web, entity);
		Check_Object(object);
	}
	return object;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	SlidingShapeComponent::Execute()
{
	Check_Object(this);
	Check_Object(m_entity);

	AffineMatrix4D matrix;
	m_attribute->GetValue(m_entity, &matrix);
	Check_Object(&matrix);

	Stuff::Scalar maxUV = MidLevelRenderer::MLRState::GetMaxUV();
	for (int i=0; i<m_UVs->GetLength(); ++i)
	{
		Stuff::Scalar tempX = m_originalUVs[i].x*matrix(0,0) + m_originalUVs[i].y*matrix(1,0) + matrix(3,0);
		Stuff::Scalar tempY = m_originalUVs[i].x*matrix(0,1) + m_originalUVs[i].y*matrix(1,1) + matrix(3,1);

		if (maxUV) {
			(*m_UVs)[i].x = (Stuff::Scalar)::fmod(tempX, maxUV);
			(*m_UVs)[i].y = (Stuff::Scalar)::fmod(tempY, maxUV);
		}

		else {
			(*m_UVs)[i].x = tempX;
			(*m_UVs)[i].y = tempY;
		}
	}
}

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