#ifndef EDITSCENE_LODSETTINGS_HPP
#define EDITSCENE_LODSETTINGS_HPP
#pragma once

#include <Ogre.h>
#include <vector>
#include <string>

/**
 * LOD Level configuration - represents one LOD level
 */
struct LodLevelConfig {
	// Distance at which to switch to this LOD level
	float distance = 100.0f;
	
	// Reduction method
	enum class ReductionMethod {
		Proportional,  // 0.0-1.0 percentage of vertices to remove
		Constant,      // Exact vertex count to remove
		CollapseCost   // Collapse cost threshold
	};
	ReductionMethod reductionMethod = ReductionMethod::Proportional;
	
	// Reduction value (meaning depends on method)
	float reductionValue = 0.5f;  // 50% reduction by default
	
	// Optional: manual mesh name (if using manual LOD)
	std::string manualMeshName;
	
	bool useManualMesh = false;
};

/**
 * LodSettingsComponent - Shareable LOD configuration
 * 
 * This component can be attached to a "settings" entity and referenced
 * by multiple entities with LodComponent for shared LOD settings.
 */
struct LodSettingsComponent {
	// Unique identifier for this LOD settings (for referencing across scenes)
	std::string settingsId;
	
	// Strategy for determining LOD level based on distance
	enum class Strategy {
		Distance,        // Distance from camera
		PixelCount,      // Screen-space pixel count
		EdgePixelCount   // Screen-space edge pixel count
	};
	Strategy strategy = Strategy::Distance;
	
	// LOD levels (from highest quality to lowest)
	// Level 0 is always the original mesh
	std::vector<LodLevelConfig> lodLevels;
	
	// Advanced options
	bool useCompression = true;           // Compress index buffers
	bool useVertexNormals = true;         // Use normals for quality
	bool preventPunchingHoles = false;    // Prevent destroying triangles
	bool preventBreakingLines = false;    // Prevent destroying lines
	bool useBackgroundQueue = false;      // Generate LODs in background
	
	float outsideWeight = 0.0f;           // Weight for outside faces (0 = disabled)
	float outsideWalkAngle = 0.0f;        // Angle for outside walking (-1 to 1)
	
	// Flag to track if settings have been modified
	bool dirty = true;
	
	// Version counter for tracking changes
	uint32_t version = 1;
	
	void markDirty() { 
		dirty = true; 
		version++;
	}
	
	// Helper to add a proportional reduction level
	void addProportionalLevel(float distance, float reductionPercent) {
		LodLevelConfig level;
		level.distance = distance;
		level.reductionMethod = LodLevelConfig::ReductionMethod::Proportional;
		level.reductionValue = reductionPercent;
		lodLevels.push_back(level);
		markDirty();
	}
	
	// Helper to add a constant reduction level
	void addConstantLevel(float distance, int verticesToRemove) {
		LodLevelConfig level;
		level.distance = distance;
		level.reductionMethod = LodLevelConfig::ReductionMethod::Constant;
		level.reductionValue = static_cast<float>(verticesToRemove);
		lodLevels.push_back(level);
		markDirty();
	}
	
	// Helper to create default LOD levels for a mesh
	void createDefaultLevels(float baseDistance = 100.0f) {
		lodLevels.clear();
		// LOD 1: 50% reduction at base distance
		addProportionalLevel(baseDistance, 0.5f);
		// LOD 2: 75% reduction at 2x distance
		addProportionalLevel(baseDistance * 2.0f, 0.75f);
		markDirty();
	}
};

#endif // EDITSCENE_LODSETTINGS_HPP
