#ifndef EDITSCENE_LODSYSTEM_HPP
#define EDITSCENE_LODSYSTEM_HPP
#pragma once

#include <flecs.h>
#include <Ogre.h>
#include <OGRE/MeshLodGenerator/OgreMeshLodGenerator.h>

/**
 * LOD System - manages LOD generation for meshes
 * 
 * This system:
 * 1. Tracks entities with LodComponent and RenderableComponent
 * 2. Applies LOD settings from LodSettingsComponent when meshes are loaded
 * 3. Regenerates LOD when settings change
 */
class EditorLodSystem {
public:
	EditorLodSystem(flecs::world& world, Ogre::SceneManager* sceneMgr);
	~EditorLodSystem();

	// Initialize the LOD generator
	void initialize();

	// Update - process pending LOD operations
	void update();

	// Force regenerate LOD for all meshes using specific settings
	void regenerateLodForSettings(flecs::entity settingsEntity);

	// Check if a mesh has LOD generated
	bool hasLodGenerated(Ogre::MeshPtr mesh) const;

private:
	// Apply LOD to a renderable entity
	void applyLod(flecs::entity entity, 
	              class LodComponent& lod,
	              class RenderableComponent& renderable);

	// Convert our config to Ogre::LodConfig
	void buildOgreLodConfig(Ogre::MeshPtr mesh,
	                        const class LodSettingsComponent& settings,
	                        Ogre::LodConfig& outConfig);

	flecs::world& m_world;
	Ogre::SceneManager* m_sceneMgr;
	
	// The Ogre LOD generator
	std::unique_ptr<Ogre::MeshLodGenerator> m_lodGenerator;
	
	// Query for entities with LOD and renderable
	flecs::query<class LodComponent, class RenderableComponent> m_lodQuery;
	
	// Query for settings entities
	flecs::query<class LodSettingsComponent> m_settingsQuery;
	
	bool m_initialized = false;
};

#endif // EDITSCENE_LODSYSTEM_HPP
