#include "Light.hpp"
#include "Transform.hpp"
#include "../ui/ComponentRegistration.hpp"
#include "../ui/LightEditor.hpp"
#include "../systems/LightSystem.hpp"

// Register Light component
REGISTER_COMPONENT("Light", LightComponent, LightEditor)
{
	registry.registerComponent<LightComponent>(
		"Light",
		std::make_unique<LightEditor>(),
		// Adder
		[sceneMgr](flecs::entity e) {
			if (!e.has<LightComponent>()) {
				// Light requires Transform
				if (!e.has<TransformComponent>()) {
					// Auto-add transform if missing
					TransformComponent transform;
					transform.node = sceneMgr->getRootSceneNode()->createChildSceneNode();
					e.set<TransformComponent>(transform);
				}
				e.set<LightComponent>({});
			}
		},
		// Remover
		[sceneMgr](flecs::entity e) {
			if (e.has<LightComponent>()) {
				auto& light = e.get_mut<LightComponent>();
				// Clean up Ogre light
				if (light.light) {
					Ogre::SceneNode* parent = light.light->getParentSceneNode();
					if (parent) {
						parent->detachObject(light.light);
					}
					sceneMgr->destroyLight(light.light);
					light.light = nullptr;
				}
				e.remove<LightComponent>();
			}
		}
	);
}
