#ifndef EDITSCENE_ANIMATIONTREEEDITOR_HPP
#define EDITSCENE_ANIMATIONTREEEDITOR_HPP
#pragma once
#include "ComponentEditor.hpp"
#include "../components/AnimationTree.hpp"
#include <OgreSceneManager.h>
#include <vector>

/**
 * Editor for AnimationTreeComponent.
 *
 * Provides a recursive tree view for editing the animation hierarchy
 * and state-machine current-state selectors for preview.
 */
class AnimationTreeEditor : public ComponentEditor<AnimationTreeComponent> {
public:
	explicit AnimationTreeEditor(Ogre::SceneManager *sceneMgr);

	const char *getName() const override { return "Animation Tree"; }

	/* Deferred edit operations — safe to call during UI rendering
	 * since they are applied only after the tree is fully drawn. */
	struct TreeEditOp {
		enum OpType { AddChild, RemoveNode, MoveNode } type;
		AnimationTreeNode *targetNode = nullptr;
		size_t childIndex = 0;
		Ogre::String childType;
		int moveDelta = 0;
	};

	void queueAddChild(AnimationTreeNode *parent,
			   const Ogre::String &type);
	void queueRemoveNode(AnimationTreeNode *parent, size_t childIndex);
	void queueMoveNode(AnimationTreeNode *parent, size_t childIndex,
			   int delta);

protected:
	bool renderComponent(flecs::entity entity,
			     AnimationTreeComponent &at) override;

private:
	void renderTree(AnimationTreeNode &node,
			AnimationTreeNode *parent,
			AnimationTreeNode *&selectedNode,
			AnimationTreeNode *&selectedParent,
			int &id);
	void renderProperties(AnimationTreeNode *node,
			      AnimationTreeComponent &at,
			      flecs::entity entity);
	void renderStatePreview(AnimationTreeComponent &at);
	void applyEditOps(AnimationTreeComponent &at);
	std::vector<Ogre::String> getAvailableAnimations(
		flecs::entity entity);

	Ogre::SceneManager *m_sceneMgr;
	std::vector<TreeEditOp> m_editOps;
};

#endif // EDITSCENE_ANIMATIONTREEEDITOR_HPP
