#include "GameObject.hpp"
#include "Event.hpp"
#include "Transform.hpp"

GameObject::GameObject() {
	OnInitialiseSelf = Event<void()>();
	OnInitialise = Event<void()>();
	OnEnable = Event<void()>();
	OnDisable = Event<void()>();
	transform = std::make_shared<Transform>(this);
	components.push_back(transform);
}
GameObject::~GameObject() {
	if (OnDisable.IsValid())OnDisable();
	components.clear();
	children.clear();
}

bool GameObject::IsActive() {
	return active;
}

void GameObject::ToggleActive() {
	active = !active;
	if (active) {
		if (!OnEnable.IsValid())return;
		OnEnable();
	}
	else {
		if (!OnDisable.IsValid())return;
		OnDisable();
	}
}

std::shared_ptr<Transform> GameObject::GetTransform() {
	return transform;
}

const std::vector<std::shared_ptr<GameObject>>& GameObject::GetChildren() {
	return children;
}

std::string GameObject::GetName() { return name; }

void GameObject::InitialiseSelf() {
	if (!active || !OnInitialiseSelf.IsValid()) return;
	OnInitialiseSelf();
	for (auto child : children)child->InitialiseSelf();
}

void GameObject::Initialise() {
	if (!active || !OnInitialise.IsValid()) return;
	OnInitialise();
	for (auto child : children)child->Initialise();
}

void GameObject::setId(int id)
{
	this->id = id;
}

int GameObject::getId()
{
	return this->id;
}

void GameObject::setActive(bool active)
{
	this->active = active;
}

void GameObject::setName(std::string name)
{
	this->name = name;
}
