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

#include <stdio.h>

#include <iomanip>
#include <iostream>
#include <typeinfo>

#include "SceneGraph.h"
#include "SceneGraphVisitor.h"
#include "GlActionVisitor.h"
#include "GraphNode.h"


namespace nsSceneGraph {

GraphNode::GraphNode(NodeType type, const char *typeName)
{
	DEBUG_MESSAGE("GraphNode::GraphNode()")

	this->type = type;
	this->typeName = typeName;
	this->nodeName = "";
	this->compiled = false;
	this->compiledDisplayList = 0;
	this->hide = false;
}


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

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

	this->children.clear();
}


bool GraphNode::isNodeCompiled(void)
{
	return this->compiled;
}


void GraphNode::setCompiledStatus(bool status)
{
	this->compiled = status;

	if (this->compiled == false)
	{
		glDeleteLists(this->getCompiledDisplayList(), 1);
	}
}


void GraphNode::setCompiledDisplayList(GLuint list)
{
	this->compiledDisplayList = list;
}


GLuint GraphNode::getCompiledDisplayList(void)
{
	return this->compiledDisplayList;
}


bool GraphNode::isNodeHidden(void)
{
	return this->hide;
}


void GraphNode::hideNode(void)
{
	this->hide = true;
}

void GraphNode::unhideNode(void)
{
	this->hide = false;
}


void GraphNode::addChild(GraphNode *node)
{
	this->children.push_back(node);
}


std::list<GraphNode *>& GraphNode::getChildren(void)
{
	return this->children;
}


NodeType GraphNode::getType(void)
{
	return this->type;
}


const char *GraphNode::getTypeName(void)
{
	return this->typeName.c_str();
}


void GraphNode::setNodeName(const char *name)
{
	this->nodeName = name;
}


const char *GraphNode::getNodeName(void)
{
	return this->nodeName.c_str();
}


void GraphNode::printGraph(int indent)
{
	std::cout << std::setw(indent * 4) << "" << std::ios::right << this->getTypeName() << ": " << this->getNodeName() << std::endl;
	std::cout << std::setw(indent * 4) << "" << std::ios::right << "{" << std::endl;
	indent++;

	std::list<GraphNode *>::iterator iter = this->children.begin();
	std::list<GraphNode *>::iterator end = this->children.end();
	for (; iter != end; iter++)
	{
		if (!(*iter)->isNodeHidden())
		{
			(*iter)->printGraph(indent);
		}
	}

	indent--;
	std::cout << std::setw(indent * 4) << "" << std::ios::right << "}" << std::endl;
}


int GraphNode::action(SceneGraphVisitor *visitor)
{
	DEBUG_MESSAGE("GraphNode::accept()");

	// Using RTTI this way is ugly like hell.
	bool glActionVisitor = (typeid(*visitor) == typeid(GlActionVisitor));
	if (glActionVisitor)
	{
		if (this->isNodeHidden())
		{
			return SG_OK;
		}
	}

	std::list<GraphNode *>::iterator iter = this->children.begin();
	std::list<GraphNode *>::iterator end = this->children.end();
	for (; iter != end; iter++)
	{
		if (glActionVisitor)
		{
			if ((*iter)->isNodeHidden())
			{
				continue;
			}
		}

		(*iter)->action(visitor);
	}

	return SG_OK;
}


}

