#include "LightEditor.hpp"
#include <imgui.h>

void LightEditor::renderColorEdit(const char* label, Ogre::ColourValue& color)
{
	float col[4] = { color.r, color.g, color.b, color.a };
	if (ImGui::ColorEdit4(label, col)) {
		color.r = col[0];
		color.g = col[1];
		color.b = col[2];
		color.a = col[3];
	}
}

bool LightEditor::renderComponent(flecs::entity entity, LightComponent &light)
{
	bool modified = false;

	if (ImGui::CollapsingHeader("Light", ImGuiTreeNodeFlags_DefaultOpen)) {
		ImGui::Indent();

		// Light type selector
		const char* lightTypes[] = { "Point", "Directional", "Spotlight" };
		int currentType = static_cast<int>(light.lightType);
		if (ImGui::Combo("Type", &currentType, lightTypes, IM_ARRAYSIZE(lightTypes))) {
			light.lightType = static_cast<LightComponent::LightType>(currentType);
			light.markDirty();
			modified = true;
		}

		// Colors
		ImGui::Separator();
		ImGui::Text("Colors");
		
		Ogre::ColourValue diffuse = light.diffuseColor;
		renderColorEdit("Diffuse", light.diffuseColor);
		if (light.diffuseColor != diffuse) {
			light.markDirty();
			modified = true;
		}
		
		Ogre::ColourValue specular = light.specularColor;
		renderColorEdit("Specular", light.specularColor);
		if (light.specularColor != specular) {
			light.markDirty();
			modified = true;
		}

		// Intensity
		ImGui::Separator();
		float intensity = light.intensity;
		if (ImGui::DragFloat("Intensity", &light.intensity, 0.1f, 0.0f, 10.0f)) {
			light.markDirty();
			modified = true;
		}

		// Cast shadows
		bool castShadows = light.castShadows;
		if (ImGui::Checkbox("Cast Shadows", &light.castShadows)) {
			light.markDirty();
			modified = true;
		}

		// Type-specific properties
		ImGui::Separator();
		
		if (light.lightType == LightComponent::LightType::Point ||
		    light.lightType == LightComponent::LightType::Spotlight) {
			ImGui::Text("Attenuation");
			
			if (ImGui::DragFloat("Range", &light.range, 1.0f, 0.1f, 10000.0f)) {
				light.markDirty();
				modified = true;
			}
			
			if (ImGui::DragFloat("Constant", &light.constantAttenuation, 0.1f, 0.0f, 1.0f)) {
				light.markDirty();
				modified = true;
			}
			
			if (ImGui::DragFloat("Linear", &light.linearAttenuation, 0.001f, 0.0f, 1.0f, "%.4f")) {
				light.markDirty();
				modified = true;
			}
			
			if (ImGui::DragFloat("Quadratic", &light.quadraticAttenuation, 0.0001f, 0.0f, 1.0f, "%.6f")) {
				light.markDirty();
				modified = true;
			}
		}

		if (light.lightType == LightComponent::LightType::Directional ||
		    light.lightType == LightComponent::LightType::Spotlight) {
			ImGui::Separator();
			ImGui::Text("Direction");
			float dir[3] = { light.direction.x, light.direction.y, light.direction.z };
			if (ImGui::DragFloat3("Direction", dir, 0.1f)) {
				light.direction = Ogre::Vector3(dir[0], dir[1], dir[2]).normalisedCopy();
				light.markDirty();
				modified = true;
			}
		}

		if (light.lightType == LightComponent::LightType::Spotlight) {
			ImGui::Separator();
			ImGui::Text("Spotlight Settings");
			
			if (ImGui::SliderFloat("Inner Angle", &light.spotlightInnerAngle, 0.0f, 90.0f)) {
				// Ensure inner <= outer
				if (light.spotlightInnerAngle > light.spotlightOuterAngle) {
					light.spotlightOuterAngle = light.spotlightInnerAngle;
				}
				light.markDirty();
				modified = true;
			}
			
			if (ImGui::SliderFloat("Outer Angle", &light.spotlightOuterAngle, 0.0f, 90.0f)) {
				// Ensure outer >= inner
				if (light.spotlightOuterAngle < light.spotlightInnerAngle) {
					light.spotlightInnerAngle = light.spotlightOuterAngle;
				}
				light.markDirty();
				modified = true;
			}
			
			if (ImGui::DragFloat("Falloff", &light.spotlightFalloff, 0.1f, 0.0f, 10.0f)) {
				light.markDirty();
				modified = true;
			}
		}

		ImGui::Unindent();
	}

	return modified;
}
