/*
 * Copyright (C) 2003-2004 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
 *
 */

#include "GraphNode.h"
#include "SceneGraph.h"


namespace nsSceneGraph {

SceneGraph::SceneGraph(void)
{
	this->scene = NULL;
}


SceneGraph::SceneGraph(GraphNode *scene)
{
	this->scene = scene;
}


SceneGraph::~SceneGraph(void)
{
	this->nodeTable.clear();
	delete this->scene;
}


void SceneGraph::setScene(GraphNode *scene)
{
	this->scene = scene;
}


GraphNode *SceneGraph::getScene(void)
{
	return this->scene;
}


void SceneGraph::nodeTableCreate(void)
{
	if (this->scene == NULL)
	{
		std::cerr << __FILE__ << '[' << __LINE__ << "]: SceneGraph::nodeTableCreate(): this->scene isn't initialized." << std::endl;
		return;
	}

	nodeTableCreateTravese(this->scene);
}


void SceneGraph::nodeTableCreateTravese(GraphNode *node)
{
	const char *nodeName = node->getNodeName();
	if (*nodeName != '\0')
	{
		this->nodeTable[nodeName] = node;
	}

	std::list<GraphNode *> children = node->getChildren();
	std::list<GraphNode *>::iterator iter = children.begin();
	std::list<GraphNode *>::iterator end = children.end();
	for (; iter != end; iter++)
	{
		nodeTableCreateTravese(*iter);
	}
}


GraphNode *SceneGraph::nodeTableLookUp(const char *nodeName)
{
	if (this->scene == NULL)
	{
		std::cerr << __FILE__ << '[' << __LINE__ << "]: SceneGraph::nodeTableLookUp(): this->scene isn't initialized." << std::endl;
		return NULL;
	}

	return this->nodeTable[nodeName];
}


int SceneGraph::action(SceneGraphVisitor *visitor)
{
	if (this->scene == NULL)
	{
		std::cerr << __FILE__ << '[' << __LINE__ << "]: SceneGraph::accept(): this->scene isn't initialized." << std::endl;
		return SG_KO;
	}

	return this->scene->action(visitor);
}


void SceneGraph::printGraph(void)
{
	if (this->scene == NULL)
	{
		std::cerr << __FILE__ << '[' << __LINE__ << "]: SceneGraph::printGraph(): this->scene isn't initialized." << std::endl;
		return;
	}

	this->scene->printGraph();
}


}

