#include "pw/scene/node.hpp"

namespace pw {

node::node(const std::string &name)
	: _name(name)
{
}

node::node(const node &node)
	: _children(node.children())
	, _name(node._name)
{
}

node::ref node::clone(const unsigned short &copymode) const
{
	return nullptr;
}

std::string node::name() const
{
	return _name;
}

void node::set_name(const std::string &name)
{
	_name = name;
}

node::ref node::add_child(ref anode)
{
	// remove old nodepath
	anode->_path.clear();

	// take parent nodepath ...
	if (!this->is_root()) anode->_path = this->_path;
	// add itself
	anode->_path.push_back(this);

	// add as child
	_children.push_back(anode);

	// return
	return anode;
}

void node::remove_child(ref child_node)
{
	node::ref_array::iterator it = _children.end();

	while ((it = std::find(_children.begin(),_children.end(),child_node)) != _children.end()) {
		_children.erase(it);
	}
}

node::~node()
{
	_children.clear();
}

//
// components
//
void node::add_component(component::ref c)
{
	_components.push_back(c);
}

void node::remove_component(component::ref c)
{
	component::array::iterator it = _components.end();

	while ((it = std::find(_components.begin(),_components.end(),c)) != _components.end()) {
		(*it)->_node = nullptr;
		_components.erase(it);
	}
}

const node::ptr_array &node::path() const
{
	return _path;
}

void node::traversal()
{
	for (auto c : _components) c->visit(this);
}

const component::array& node::components() const
{
	return _components;
}




}
