#include "pch.h"
#include "app.h"
#include "legacy_ui_overlay_services.h"
#include "node_button_custom.h"
#include "node_popup_menu.h"

namespace {

std::shared_ptr<NodePopupMenu> add_menu_popup(
    App& app,
    const char* template_id,
    glm::vec2 position,
    float rtl_anchor_width)
{
    const auto popup = pp::panopainter::add_legacy_popup_menu(
        app,
        template_id,
        position.x,
        position.y,
        rtl_anchor_width);
    if (!popup) {
        LOG("Popup menu '%s' failed: %s", template_id ? template_id : "<null>", popup.status().message);
        return nullptr;
    }
    return popup.value();
}

} // namespace

namespace pp::panopainter {

void bind_legacy_edit_menu(App& app)
{
    auto main = app.layout[app.main_id];

    if (auto* menu_edit = main->find<NodeButtonCustom>("menu-edit"))
    {
        menu_edit->on_click = [&app, menu_edit](Node*) {
            glm::vec2 pos = menu_edit->m_pos + glm::vec2(0, menu_edit->m_size.y);
            auto popup = add_menu_popup(app, "edit-menu", pos, menu_edit->m_size.x);
            if (!popup)
                return;
        };
    }
}

} // namespace pp::panopainter
