#include "AnimationTree.hpp"
#include "../ui/ComponentRegistration.hpp"
#include "../ui/AnimationTreeEditor.hpp"
#include "Transform.hpp"

static AnimationTreeComponent createDefaultTree()
{
	AnimationTreeComponent at;

	at.root.type = "output";
	at.root.speed = 1.0f;

	AnimationTreeNode sm;
	sm.type = "stateMachine";
	sm.name = "main";
	sm.fadeSpeed = 7.5f;

	AnimationTreeNode state;
	state.type = "state";
	state.name = "idle";

	AnimationTreeNode anim;
	anim.type = "animation";
	anim.animationName = "idle";

	state.children.push_back(anim);
	sm.children.push_back(state);
	at.root.children.push_back(sm);

	return at;
}

REGISTER_COMPONENT_GROUP("Animation Tree", "Rendering",
				 AnimationTreeComponent, AnimationTreeEditor)
{
	registry.registerComponent<AnimationTreeComponent>(
		"Animation Tree", "Rendering",
		std::make_unique<AnimationTreeEditor>(sceneMgr),
		/* Adder */
		[sceneMgr](flecs::entity e) {
			if (!e.has<TransformComponent>()) {
				TransformComponent transform;
				transform.node =
					sceneMgr->getRootSceneNode()
						->createChildSceneNode();
				e.set<TransformComponent>(transform);
			}
			AnimationTreeComponent at = createDefaultTree();
			at.dirty = true;
			e.set<AnimationTreeComponent>(at);
		},
		/* Remover */
		[sceneMgr](flecs::entity e) {
			(void)sceneMgr;
			e.remove<AnimationTreeComponent>();
		});
}
