#ifndef EDITSCENE_BEHAVIOR_TREE_EDITOR_HPP
#define EDITSCENE_BEHAVIOR_TREE_EDITOR_HPP
#pragma once

#include "ComponentEditor.hpp"
#include "../components/BehaviorTree.hpp"

/**
 * Editor for BehaviorTreeComponent.
 *
 * Provides a recursive tree view for editing behavior tree hierarchy.
 */
class BehaviorTreeEditor : public ComponentEditor<BehaviorTreeComponent> {
public:
	const char *getName() const override
	{
		return "Behavior Tree";
	}

	struct TreeEditOp {
		enum OpType { AddChild, RemoveNode, MoveNode, AddLuaNode } type;
		BehaviorTreeNode *targetNode = nullptr;
		size_t childIndex = 0;
		Ogre::String childType;
		Ogre::String luaNodeName;
		int moveDelta = 0;
	};

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

protected:
	bool renderComponent(flecs::entity entity,
			     BehaviorTreeComponent &bt) override;

private:
	void renderTree(BehaviorTreeNode &node, BehaviorTreeNode *parent,
			BehaviorTreeNode *&selectedNode,
			BehaviorTreeNode *&selectedParent, int &id);
	void renderProperties(BehaviorTreeNode *node);
	void applyEditOps(BehaviorTreeComponent &bt);

	std::vector<TreeEditOp> m_editOps;
};

#endif // EDITSCENE_BEHAVIOR_TREE_EDITOR_HPP
