#include "RoofEditor.hpp"
#include "../components/CellGrid.hpp"
#include <imgui.h>

bool RoofEditor::renderComponent(flecs::entity entity, RoofComponent& roof)
{
    bool modified = false;
    
    if (ImGui::CollapsingHeader("Roof", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::Indent();
        
        const char* roofTypes[] = { "Flat", "Normal", "Normal2", "Cone", "Cylinder" };
        int typeIndex = static_cast<int>(roof.type);
        if (ImGui::Combo("Type", &typeIndex, roofTypes, IM_ARRAYSIZE(roofTypes))) {
            roof.type = static_cast<RoofComponent::Type>(typeIndex);
            modified = true;
        }
        
        int pos[3] = { roof.posX, roof.posY, roof.posZ };
        if (ImGui::InputInt3("Position (cells)", pos)) {
            roof.posX = pos[0];
            roof.posY = pos[1];
            roof.posZ = pos[2];
            modified = true;
        }
        
        int size[2] = { roof.sizeX, roof.sizeZ };
        if (ImGui::InputInt2("Size (cells)", size)) {
            roof.sizeX = std::max(1, size[0]);
            roof.sizeZ = std::max(1, size[1]);
            modified = true;
        }
        
        float offset[3] = { roof.offsetX, roof.offsetY, roof.offsetZ };
        if (ImGui::DragFloat3("Offset", offset, 0.1f)) {
            roof.offsetX = offset[0];
            roof.offsetY = offset[1];
            roof.offsetZ = offset[2];
            modified = true;
        }
        
        if (ImGui::DragFloat("Base Height", &roof.baseHeight, 0.1f, 0.1f, 100.0f)) {
            modified = true;
        }
        if (ImGui::DragFloat("Max Height", &roof.maxHeight, 0.1f, roof.baseHeight, 100.0f)) {
            modified = true;
        }
        
        ImGui::Unindent();
    }
    
    // Mark roof as dirty when modified
    if (modified) {
        roof.markDirty();
    }
    
    return modified;
}
