#include "ProceduralTexture.hpp"
#include "../ui/ComponentRegistration.hpp"
#include "../ui/ProceduralTextureEditor.hpp"

// Register ProceduralTexture component
REGISTER_COMPONENT_GROUP("Procedural Texture", "Material", ProceduralTextureComponent, ProceduralTextureEditor)
{
    registry.registerComponent<ProceduralTextureComponent>(
        "Procedural Texture", "Material",
        std::make_unique<ProceduralTextureEditor>(),
        // Adder
        [sceneMgr](flecs::entity e) {
            if (!e.has<ProceduralTextureComponent>()) {
                e.set<ProceduralTextureComponent>({});
            }
        },
        // Remover
        [sceneMgr](flecs::entity e) {
            if (e.has<ProceduralTextureComponent>()) {
                auto& texture = e.get_mut<ProceduralTextureComponent>();
                // Clean up Ogre texture - wrap in try/catch since TextureManager may be shutting down
                if (texture.ogreTexture) {
                    try {
                        if (Ogre::TextureManager::getSingletonPtr()) {
                            Ogre::TextureManager::getSingleton().remove(texture.ogreTexture);
                        }
                    } catch (...) {}
                    texture.ogreTexture.reset();
                }
                e.remove<ProceduralTextureComponent>();
            }
        }
    );
}
