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

bool FurnitureTemplateEditor::renderComponent(flecs::entity entity, FurnitureTemplateComponent& furniture)
{
    bool modified = false;
    
    if (ImGui::CollapsingHeader("Furniture Template", ImGuiTreeNodeFlags_DefaultOpen)) {
        ImGui::Indent();
        
        char nameBuf[128] = {0};
        strncpy(nameBuf, furniture.templateName.c_str(), sizeof(nameBuf) - 1);
        if (ImGui::InputText("Template Name", nameBuf, sizeof(nameBuf))) {
            furniture.templateName = nameBuf;
            modified = true;
        }
        
        char meshBuf[128] = {0};
        strncpy(meshBuf, furniture.meshName.c_str(), sizeof(meshBuf) - 1);
        if (ImGui::InputText("Mesh", meshBuf, sizeof(meshBuf))) {
            furniture.meshName = meshBuf;
            modified = true;
        }
        
        char matBuf[128] = {0};
        strncpy(matBuf, furniture.materialName.c_str(), sizeof(matBuf) - 1);
        if (ImGui::InputText("Material", matBuf, sizeof(matBuf))) {
            furniture.materialName = matBuf;
            modified = true;
        }
        
        // Tags
        std::string tagsStr;
        for (size_t i = 0; i < furniture.tags.size(); ++i) {
            if (i > 0) tagsStr += ", ";
            tagsStr += furniture.tags[i];
        }
        char tagsBuf[256] = {0};
        strncpy(tagsBuf, tagsStr.c_str(), sizeof(tagsBuf) - 1);
        if (ImGui::InputText("Tags", tagsBuf, sizeof(tagsBuf))) {
            furniture.tags.clear();
            std::string newTags(tagsBuf);
            size_t start = 0;
            size_t end = newTags.find(',');
            while (end != std::string::npos) {
                std::string tag = newTags.substr(start, end - start);
                tag.erase(0, tag.find_first_not_of(" \t"));
                tag.erase(tag.find_last_not_of(" \t") + 1);
                if (!tag.empty()) furniture.tags.push_back(tag);
                start = end + 1;
                end = newTags.find(',', start);
            }
            std::string lastTag = newTags.substr(start);
            lastTag.erase(0, lastTag.find_first_not_of(" \t"));
            lastTag.erase(lastTag.find_last_not_of(" \t") + 1);
            if (!lastTag.empty()) furniture.tags.push_back(lastTag);
            modified = true;
        }
        
        ImGui::Checkbox("Requires Wall", &furniture.requiresWall);
        ImGui::Checkbox("Requires Floor", &furniture.requiresFloor);
        ImGui::Checkbox("Blocks Path", &furniture.blocksPath);
        
        float offset[3] = { furniture.offsetX, furniture.offsetY, furniture.offsetZ };
        if (ImGui::DragFloat3("Offset", offset, 0.01f)) {
            furniture.offsetX = offset[0];
            furniture.offsetY = offset[1];
            furniture.offsetZ = offset[2];
            modified = true;
        }
        
        if (ImGui::DragFloat("Weight", &furniture.weight, 0.1f, 0.0f, 100.0f)) {
            modified = true;
        }
        
        ImGui::Unindent();
    }
    
    return modified;
}
