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

bool PrimitiveEditor::renderComponent(flecs::entity entity, PrimitiveComponent &primitive)
{
	bool modified = false;
	
	if (ImGui::CollapsingHeader("Primitive", ImGuiTreeNodeFlags_DefaultOpen)) {
		ImGui::Indent();
		
		// Type selector
		const char* typeNames[] = {"None", "Box", "Plane"};
		int currentType = static_cast<int>(primitive.type);
		
		if (ImGui::Combo("Type", &currentType, typeNames, IM_ARRAYSIZE(typeNames))) {
			primitive.type = static_cast<PrimitiveComponent::Type>(currentType);
			primitive.markDirty();
			modified = true;
		}
		
		ImGui::Separator();
		
		// Type-specific parameters
		switch (primitive.type) {
			case PrimitiveComponent::Type::Box: {
				ImGui::Text("Box Dimensions:");
				if (ImGui::DragFloat("Size X", &primitive.boxSizeX, 0.01f, 0.001f, 100.0f)) {
					primitive.markDirty();
					modified = true;
				}
				if (ImGui::DragFloat("Size Y", &primitive.boxSizeY, 0.01f, 0.001f, 100.0f)) {
					primitive.markDirty();
					modified = true;
				}
				if (ImGui::DragFloat("Size Z", &primitive.boxSizeZ, 0.01f, 0.001f, 100.0f)) {
					primitive.markDirty();
					modified = true;
				}
				break;
			}
			case PrimitiveComponent::Type::Plane: {
				ImGui::Text("Plane Dimensions:");
				if (ImGui::DragFloat("Size X", &primitive.planeSizeX, 0.01f, 0.001f, 100.0f)) {
					primitive.markDirty();
					modified = true;
				}
				if (ImGui::DragFloat("Size Y", &primitive.planeSizeY, 0.01f, 0.001f, 100.0f)) {
					primitive.markDirty();
					modified = true;
				}
				ImGui::Separator();
				ImGui::Text("Segments:");
				if (ImGui::DragInt("Segments X", &primitive.planeSegmentsX, 1, 1, 100)) {
					primitive.markDirty();
					modified = true;
				}
				if (ImGui::DragInt("Segments Y", &primitive.planeSegmentsY, 1, 1, 100)) {
					primitive.markDirty();
					modified = true;
				}
				break;
			}
			default:
				ImGui::TextDisabled("Select a primitive type to configure");
				break;
		}
		
		ImGui::Separator();
		
		// Status
		if (primitive.dirty) {
			ImGui::TextColored(ImVec4(1, 1, 0, 1), "Status: Modified (needs rebuild)");
		} else {
			ImGui::TextColored(ImVec4(0, 1, 0, 1), "Status: Up to date");
		}
		
		ImGui::TextDisabled("Note: Add this entity as child of a TriangleBuffer entity");
		
		ImGui::Unindent();
	}
	
	return modified;
}
