//
// Created by slinky on 5/11/26.
//

#include "UIEntityInspector.h"

#include "Components.h"
#include "imgui.h"
#include "glm/gtc/type_ptr.hpp"

void UIEntityInspector::update(EditorContext &ctx) {
    ImGui::Begin("Inspector");

    if (ctx.selectedEntity == entt::null)
    {
        ImGui::TextDisabled("No entity selected");
        ImGui::End();
        return;
    }

    Scene* scene = ctx.scene;
    auto entity = ctx.selectedEntity;

    if (scene->has_component<Components::Tag>(entity))
    {
        auto& [name] = scene->fetch_component<Components::Tag>(entity);

        char buffer[256]{};
        memset(buffer, 0, sizeof(buffer));

        strncpy(buffer, name.c_str(), sizeof(buffer) - 1);

        if (ImGui::InputText("Tag", buffer, sizeof(buffer)))
        {
            name = std::string(buffer);
        }
    }

    ImGui::Separator();

    if (scene->has_component<Components::Transform>(entity))
    {
        auto& transform = scene->fetch_component<Components::Transform>(entity);
        ImGui::Text("Transform");

        if (ImGui::DragFloat3("Position", glm::value_ptr(transform.position), 0.1f))
        {
            transform.dirty = true;
        }

        if (ImGui::DragFloat3("Rotation", glm::value_ptr(transform.rotation), 0.05f))
        {
            transform.dirty = true;
        }

        if (ImGui::DragFloat3("Scale", glm::value_ptr(transform.scale), 0.1f))
        {
            transform.dirty = true;
        }
    }

    ImGui::End();
}
