/*
 * Copyright (C) 2004, 2012 Clemens Fuchslocher
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

/*
 * The following source code below shows the usage of the SceneGraphBuilder
 * to create a scene graph with the following structure:
 *
 * |
 * `-- Group
 *    |-- Transform
 *    |   |-- Material
 *    |   `-- VertexArray
 *    `-- Transform
 *        |-- Material
 *        `-- VertexArray
 *
 * SceneGraphBuilder builder;
 *
 * builder.addNewNode(new Group());
 *
 * builder.addNewNode(new Transform());
 * builder.addNewNode(new Material());
 * builder.finishNode(); // Material
 * builder.addNewNode(new VertexArray());
 * builder.finishNode(); // VertexArray
 * builder.finishNode(); // Transform
 *
 * builder.addNewNode(new Transform());
 * builder.addNewNode(new Material());
 * builder.finishNode(); // Material
 * builder.addNewNode(new VertexArray());
 * builder.finishNode(); // VertexArray
 * builder.finishNode(); // Transform
 *
 * builder.finishNode(); // Group
 *
 * GraphNode *scene = builder.getSceneGraph();
 *
 */

#include <cstdlib>

#include "SceneGraphBuilder.h"


namespace nsSceneGraph {

SceneGraphBuilder::SceneGraphBuilder(void)
{
	DEBUG_MESSAGE("SceneGraphBuilder::SceneGraphBuilder()")

	/*
 	 * We need a dummy Group node in our stack because we don't
 	 * know if the VRML scene is containing a top level parent.
  	 */
	this->nodeStack.push(new Group());
	this->nodeStack.top()->setNodeName("dummyGroup");
}


SceneGraphBuilder::~SceneGraphBuilder(void)
{
	DEBUG_MESSAGE("SceneGraphBuilder::~SceneGraphBuilder()")

	std::map<const char *, GraphNode *>::iterator iter = this->defUseDictionary.begin();
	std::map<const char *, GraphNode *>::iterator end = this->defUseDictionary.end();
	for (; iter != end; iter++)
	{
		std::pair<const char *, GraphNode *> entry = *iter;
		free((char *) entry.first);
	}
	this->defUseDictionary.clear();
}


int SceneGraphBuilder::addNewNode(GraphNode *node)
{
	DEBUG_MESSAGE("SceneGraphBuilder::addNewNode()")

	this->nodeStack.push(node);

	return SCENE_GRAPH_BUILDER_OK;
}


int SceneGraphBuilder::finishNode(void)
{
	DEBUG_MESSAGE("SceneGraphBuilder::finishNode()")

	GraphNode *node = this->nodeStack.top();
	this->nodeStack.pop();

	if (this->nodeStack.empty())
	{
		std::cerr << __FILE__ << "[" << __LINE__ << "]: SceneGraphBuilder::finishNode(): nodeStack is empty." << std::endl;
		return SCENE_GRAPH_BUILDER_OK;
	}

	this->nodeStack.top()->addChild(node);

	return SCENE_GRAPH_BUILDER_OK;
}


GraphNode *SceneGraphBuilder::getCurrentNode(void)
{
	DEBUG_MESSAGE("SceneGraphBuilder::getCurrentNode()")

	return this->nodeStack.top();
}


GraphNode *SceneGraphBuilder::getSceneGraph(void)
{
	DEBUG_MESSAGE("SceneGraphBuilder::getSceneGraph()")

	GraphNode *scene = NULL;

	/*
	 * At this point only the dummy Group node should be on the
	 * nodeStack. If this is not the case something is wrong.
	 */
	if (this->nodeStack.size() != 1)
	{
		std::cerr << __FILE__ << "[" << __LINE__ << "]: SceneGraphBuilder::getSceneGraph(): nodeStack.size() != 1" << std::endl;
		return NULL;
	}

	// Is the dummy Group node really necessary?
	GraphNode *node = this->nodeStack.top();
	if (node->getChildren().size() == 1)
	{
		scene = node->getChildren().front();
		node->getChildren().clear();
		delete node;
	}
	else
	{
		scene = node;
	}
	nodeStack.pop();

	return scene;
}


int SceneGraphBuilder::addNodeReference(const char *name)
{
	DEBUG_MESSAGE("SceneGraphBuilder::addNodeReference()")

	GraphNode *node = this->nodeStack.top();
	if (node == NULL)
	{
		std::cerr << __FILE__ << "[" << __LINE__ << "]: SceneGraphBuilder::addNodeReference()" << std::endl;
		return SCENE_GRAPH_BUILDER_KO;
	}

	this->defUseDictionary[strdup(name)] = node;

	return SCENE_GRAPH_BUILDER_OK;
}


int SceneGraphBuilder::resolveNodeReference(const char *name)
{
	DEBUG_MESSAGE("SceneGraphBuilder::resolveNodeReference()")

	GraphNode *node = this->defUseDictionary[name];
	if (node == NULL)
	{
		std::cerr << __FILE__ << "[" << __LINE__ << "]: SceneGraphBuilder::resolveNodeReference()" << std::endl;
		return SCENE_GRAPH_BUILDER_KO;
	}

	delete this->nodeStack.top();
	this->nodeStack.pop();
	this->nodeStack.push(node->duplicate());

	return SCENE_GRAPH_BUILDER_OK;
}


}

