#include "core/app_about.h"

#include "imgui.h"

#include <string>

namespace fn_ui {

namespace {

std::string g_project     = "fn_registry app";
std::string g_version     = "";
std::string g_description = "";
bool        g_open        = false;

} // namespace

void about_window_set_info(const char* project,
                           const char* version,
                           const char* description) {
    if (project)     g_project     = project;
    if (version)     g_version     = version;
    if (description) g_description = description;
}

bool about_window_is_open()        { return g_open; }
void about_window_set_open(bool v) { g_open = v; }
void about_window_toggle()         { g_open = !g_open; }

bool about_window_menu_item(const char* label) {
    if (ImGui::MenuItem(label)) {
        g_open = true;
        return true;
    }
    return false;
}

void about_window_render() {
    if (!g_open) return;

    ImGui::SetNextWindowSize(ImVec2(420, 220), ImGuiCond_FirstUseEver);
    if (!ImGui::Begin("About", &g_open, ImGuiWindowFlags_NoCollapse)) {
        ImGui::End();
        return;
    }

    ImGui::TextUnformatted(g_project.c_str());

    if (!g_version.empty()) {
        ImGui::SameLine();
        ImGui::TextDisabled("v%s", g_version.c_str());
    }

    if (!g_description.empty()) {
        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();
        ImGui::TextWrapped("%s", g_description.c_str());
    }

    ImGui::Spacing();
    ImGui::Separator();
    ImGui::TextDisabled("fn_registry");

    ImGui::End();
}

} // namespace fn_ui
