#include "badge.h"
#include "tokens.h"
#include "imgui.h"

static ImVec4 badge_bg(BadgeVariant v) {
    using namespace fn_tokens::colors;
    switch (v) {
        case BadgeVariant::Success: return success;
        case BadgeVariant::Warning: return warning;
        case BadgeVariant::Error:   return error;
        case BadgeVariant::Info:    return info;
        case BadgeVariant::Outline: return ImVec4(0, 0, 0, 0);
        case BadgeVariant::Default:
        default:                    return surface_hover;
    }
}

void badge(const char* text, BadgeVariant variant) {
    ImVec4 bg = badge_bg(variant);
    ImVec4 tx = (variant == BadgeVariant::Outline || variant == BadgeVariant::Default)
        ? fn_tokens::colors::text
        : ImVec4(1.0f, 1.0f, 1.0f, 1.0f);

    const float pad_x = fn_tokens::spacing::sm;
    const float pad_y = fn_tokens::spacing::xs * 0.5f;
    ImVec2 text_size = ImGui::CalcTextSize(text);
    ImVec2 cursor = ImGui::GetCursorScreenPos();
    ImVec2 p_min = cursor;
    ImVec2 p_max = ImVec2(cursor.x + text_size.x + pad_x * 2.0f,
                          cursor.y + text_size.y + pad_y * 2.0f);

    ImDrawList* dl = ImGui::GetWindowDrawList();
    if (variant == BadgeVariant::Outline) {
        dl->AddRect(p_min, p_max, ImGui::GetColorU32(fn_tokens::colors::border),
                    fn_tokens::radius::md);
    } else {
        dl->AddRectFilled(p_min, p_max, ImGui::GetColorU32(bg),
                          fn_tokens::radius::md);
    }
    dl->AddText(ImVec2(cursor.x + pad_x, cursor.y + pad_y),
                ImGui::GetColorU32(tx), text);

    ImGui::Dummy(ImVec2(p_max.x - p_min.x, p_max.y - p_min.y));
}
