#include "SplitMeshNode.h"

SplitMeshNode::SplitMeshNode(wstring name, wstring modelName)
{
	_modelName = modelName;
}

bool SplitMeshNode::Initialise()
{
	bool currentStatus = true;

	_resourceManager = DirectXFramework::GetDXFramework()->GetResourceManager();
	_mesh = _resourceManager->GetMesh(_modelName);
	if (_mesh == nullptr)
	{
		currentStatus = false;
	}
	else
	{		
		Add(AddMeshNode(_mesh->GetRootNode()));		
	}

	if (currentStatus)
	{
		currentStatus = SceneGraph::Initialise();
	}

	return currentStatus;
}

SceneGraphPointer SplitMeshNode::AddMeshNode(shared_ptr<Node> node)
{
	unsigned int subMeshCount = (unsigned int)node->GetMeshCount();
	SceneGraphPointer currentNodeGraph = make_shared<SceneGraph>(node->GetName());

	/*string nameString;
	for (char x : node->GetName())
	{
		nameString += x;
	}
	
	fileOut << string(depth, '-') + " " + nameString << endl;*/

	// Loop through all submeshes in the mesh
	for (unsigned int i = 0; i < subMeshCount; i++)
	{
		unsigned int meshIndex = node->GetMesh(i);
		shared_ptr<SubMesh> subMesh = _mesh->GetSubMesh(meshIndex);
		shared_ptr<SubMeshNode> subNode = make_shared<SubMeshNode>(node->GetName() + L"_" + to_wstring(i), subMesh);
		subNode->SetWorldTransform(XMMatrixIdentity());
		currentNodeGraph->Add(subNode);
	}

	// Render the children
	unsigned int childrenCount = (unsigned int)node->GetChildrenCount();
	// Loop through all submeshes in the mesh, rendering them
	for (unsigned int i = 0; i < childrenCount; i++)
	{
		currentNodeGraph->Add(AddMeshNode(node->GetChild(i)));
	}

	return currentNodeGraph;
}