#include "pw/scene/transform.hpp"
#include "pw/scene/node.hpp"

#include <iostream>

namespace pw {

node::node()
{
}

node::node(const node &node)
	: _children(node.children())
	, _components(node.components())
	, _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()
{
	// create new node
	node::ref new_node = std::make_shared<node>();

	// take parent nodepath ...
	if (!this->is_root()) new_node->_path = this->_path;

	// add itself
	new_node->_path.push_back(this);

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

	// return
	return new_node;
}

void node::remove_child(ref child_node)
{
    for (node::ref_array::iterator it = _children.begin();
         it != _children.end();
         it++)
    {
        if (child_node == *it) it = _children.erase(it);
    }
//    auto it = _children.end();

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

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

//
// components
//
void node::add_component(component::ref new_component)
{
	// assumption is that only transform is a "singular" node
	_components.push_back(new_component);
}

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

    for (component::array::iterator it = _components.begin();
         it != _components.end();
         it++)
    {
        if (*it == c) it = _components.erase(it);
    }


//	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;
}




}
