#include "StaticGeometryEditor.hpp"
#include "../components/StaticGeometryMember.hpp"
#include "../components/LodSettings.hpp"
#include <imgui.h>
#include <OgreStaticGeometry.h>

bool StaticGeometryEditor::renderComponent(flecs::entity entity, StaticGeometryComponent &region)
{
    bool changed = false;
    
    ImGui::Text("Region ID: %s", region.regionId.empty() ? "(none)" : region.regionId.c_str());
    
    // Region name
    char nameBuf[256];
    strncpy(nameBuf, region.regionName.c_str(), sizeof(nameBuf) - 1);
    nameBuf[sizeof(nameBuf) - 1] = '\0';
    if (ImGui::InputText("Region Name", nameBuf, sizeof(nameBuf))) {
        region.regionName = nameBuf;
        region.markDirty();
        changed = true;
    }
    
    ImGui::Separator();
    
    // Check LOD consistency
    std::string regionLodId;
    bool lodConsistent = true;
    int lodMemberCount = 0;
    
    entity.world().query<StaticGeometryMemberComponent>().each([&](flecs::entity e, StaticGeometryMemberComponent& member) {
        if (member.regionEntity == entity && !member.lodSettingsId.empty()) {
            lodMemberCount++;
            if (regionLodId.empty()) {
                regionLodId = member.lodSettingsId;
            } else if (regionLodId != member.lodSettingsId) {
                lodConsistent = false;
            }
        }
    });
    
    if (lodMemberCount > 0) {
        if (lodConsistent) {
            ImGui::TextColored(ImVec4(0, 1, 0, 1), "LOD: Consistent (%s)", regionLodId.c_str());
        } else {
            ImGui::TextColored(ImVec4(1, 0, 0, 1), "LOD: INCONSISTENT!");
            ImGui::TextWrapped("All members must use the same LOD Settings!");
        }
    }
    
    ImGui::Separator();
    
    // Build status
    if (region.built) {
        ImGui::TextColored(ImVec4(0, 1, 0, 1), "Status: Built (v%d)", region.buildVersion);
        ImGui::Text("Members: %zu", region.memberEntityIds.size());
    } else if (region.dirty) {
        ImGui::TextColored(ImVec4(1, 1, 0, 1), "Status: Needs Rebuild");
    } else {
        ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1), "Status: Not Built");
    }
    
    ImGui::Separator();
    
    // Settings
    if (ImGui::Checkbox("Cast Shadows", &region.castShadows)) {
        region.markDirty();
        changed = true;
    }
    
    if (ImGui::DragFloat("Rendering Distance", &region.renderingDistance, 10.0f, 0.0f, 10000.0f)) {
        region.markDirty();
        changed = true;
    }
    if (region.renderingDistance == 0.0f) {
        ImGui::SameLine();
        ImGui::TextDisabled("(infinite)");
    }
    
    if (ImGui::DragFloat("Region Dimensions", &region.regionDimensions, 10.0f, 100.0f, 10000.0f)) {
        region.markDirty();
        changed = true;
    }
    
    ImGui::Separator();
    
    // Manual rebuild button
    if (ImGui::Button("Rebuild Now", ImVec2(120, 0))) {
        region.markDirty();
        changed = true;
    }
    ImGui::SameLine();
    if (ImGui::Button("Clear Geometry", ImVec2(120, 0))) {
        region.built = false;
        region.dirty = true;
        changed = true;
    }
    
    return changed;
}
