
#include "ElementProxyHeaders.hpp"

//----------------------------------------------------------------------------
//
bool
	SetExplicitLightingModeProcess::SetExplicitLightingMode(ChildProxy *child, MLRState::LightingMode newLightingMode)
{
	Check_Object(this);
	Check_Object(child);

	if (child->IsDerivedFrom(ElementGroupProxy::DefaultData))
	{
		ElementGroupProxy *group = Cast_Object(ElementGroupProxy*, child);
		return SetExplicitLightingMode(group, newLightingMode);
	}
	else if (child->IsDerivedFrom(ElementPolygonMeshProxy::DefaultData))
	{
		ElementPolygonMeshProxy *mesh = Cast_Object(ElementPolygonMeshProxy*, child);
		return SetExplicitLightingMode(mesh, newLightingMode);
	}
	else if (child->IsDerivedFrom(ElementListProxy::DefaultData))
	{
		return true;
	}
	STOP(("Shouldn't get here..."));
	SetExplicitLightingModeCallback(child);
	return false;
}

//----------------------------------------------------------------------------
//
bool
	SetExplicitLightingModeProcess::SetExplicitLightingMode(ElementGroupProxy* group, MLRState::LightingMode newLightingMode)
{
	Check_Object(this);
	Check_Object(group);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitLightingModeCallback(group);
	if (!continueProcess)
		return true;

	//
	//--------------------
	// Kill the group name
	//--------------------
	//
	group->SetName(NULL);

	//
	//-----------------------------
	// Run optimize on the children
	//-----------------------------
	//
	unsigned child_count = group->GetChildCount();
	ChildProxy *child = group->UseFirstChildProxy();
	for (unsigned i=0; i<child_count; ++i)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (SetExplicitLightingMode(child, newLightingMode))
			child->DetachReference();
		child = next;
		if (!continueProcess)
			break;
	}

	//
	//-------------------------------------------
	// Make sure to discard any remaining proxies
	//-------------------------------------------
	//
	if (child)
		child->DetachReference();

	return false;
}

//----------------------------------------------------------------------------
//
bool
SetExplicitLightingModeProcess::SetExplicitLightingMode(ElementPolygonMeshProxy* mesh, MLRState::LightingMode newLightingMode)
{
	Check_Object(this);
	Check_Object(mesh);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitLightingModeCallback(mesh);
	if (!continueProcess)
		return true;

	//
	// Set the Lighting mode
	//
	unsigned unique_combinations = mesh->GetPrimitiveCount();
	for (int i = 0; i < unique_combinations; i++)
	{
		MLR_I_PMesh *primitive = mesh->GetPrimitive(i);
		Check_Object(primitive);
		MLRState state = primitive->GetReferenceState();
		state.SetLightingMode(newLightingMode);
		primitive->SetReferenceState(state);
	}
	return true;
}

//----------------------------------------------------------------------------
//
void
	SetExplicitLightingModeProcess::SetExplicitLightingMode(ElementSceneProxy* scene, MLRState::LightingMode newLightingMode)
{
	Check_Object(this);
	Check_Object(scene);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitLightingModeCallback(scene);
	if (!continueProcess)
		return;

	//
	//-----------------------------
	// Run optimize on the children
	//-----------------------------
	//
	ChildProxy *child = scene->UseFirstChildProxy();
	while (child)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (SetExplicitLightingMode(child, newLightingMode))
			child->DetachReference();
		child = next;
		if (!continueProcess)
			break;
	}

	//
	//-------------------------------------------
	// Make sure to discard any remaining proxies
	//-------------------------------------------
	//
	if (child)
		child->DetachReference();

}
