#include "pch.h"
#include "app.h"
#include "app_core/document_export.h"
#include "app_core/file_menu.h"
#include "legacy_app_shell_services.h"
#include "legacy_ui_overlay_services.h"
#include "node_button_custom.h"
#include "node_popup_menu.h"

namespace {

void apply_document_export_menu_plan(App& app, pp::app::DocumentExportMenuKind kind)
{
    (void)pp::panopainter::apply_legacy_document_export_menu_plan(app, kind);
}

void apply_file_menu_plan(App& app, pp::app::FileMenuCommand command)
{
    pp::panopainter::apply_legacy_file_menu_command(app, command);
}

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();
}

void close_legacy_overlay_handle_ignoring_status(
    Node& anchor,
    pp::ui::NodeHandle overlay) noexcept
{
    (void)pp::panopainter::close_legacy_overlay_node(anchor, overlay);
}

} // namespace

namespace pp::panopainter {

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

    if (auto* menu_file = main->find<NodeButtonCustom>("menu-file"))
    {
        menu_file->on_click = [&app, menu_file](Node*) {
            auto* popup_root = app.layout[app.main_id];
            if (!popup_root) {
                return;
            }
            const auto open_checked_menu_popup = [popup_root](App& app_ref, const char* id, glm::vec2 pos, float width)
                -> std::pair<std::shared_ptr<NodePopupMenu>, pp::ui::NodeHandle>
            {
                auto popup = add_menu_popup(app_ref, id, pos, width);
                if (!popup) {
                    return {};
                }
                pp::panopainter::detach_legacy_node_from_parent(*popup);
                auto popup_overlay = pp::panopainter::open_legacy_overlay_node_with_handle(*popup_root, popup);
                if (!popup_overlay) {
                    pp::panopainter::destroy_legacy_node(*popup);
                    return {};
                }
                return { popup, popup_overlay.value() };
            };

            glm::vec2 pos = menu_file->m_pos + glm::vec2(0, menu_file->m_size.y);
            const auto [popup, popup_handle] = open_checked_menu_popup(app, "file-menu", pos, menu_file->m_size.x);
            if (!popup)
                return;

            if (auto b = popup->find<NodeButtonCustom>("file-newdoc"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::new_document);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-import"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::import_image);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-open"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::open_project);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-browse"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::browse_cloud);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-save"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::save);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-save-as"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::save_as);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-save-ver"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::save_version);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-export"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::export_jpeg);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-export-tick"))
                b->on_click = [&app, b, popup_root, popup_handle, open_checked_menu_popup](Node*) {
                    glm::vec2 pos = b->m_pos + glm::vec2(b->m_size.x, 0);
                    const auto [subpopup, subpopup_handle] = open_checked_menu_popup(app, "file-submenu-export", pos, b->m_size.x);
                    if (!subpopup)
                        return;
                    subpopup->find<NodeButtonCustom>("file-submenu-export-png")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::png);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-layers")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::layers);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-cube")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::cube_faces);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-depth")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::depth);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-anim")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::animation_frames);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-anim-mp4")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::animation_mp4);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                    subpopup->find<NodeButtonCustom>("file-submenu-export-timelapse")->on_click = [&app, popup_root, popup_handle, subpopup_handle](Node*) {
                        apply_document_export_menu_plan(app, pp::app::DocumentExportMenuKind::timelapse);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                        close_legacy_overlay_handle_ignoring_status(*popup_root, subpopup_handle);
                    };
                };
            if (auto b = popup->find<NodeButtonCustom>("file-share"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::share);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-resize"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::resize);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-cloud-upload"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::cloud_upload);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
            if (auto b = popup->find<NodeButtonCustom>("file-cloud-browse"))
                b->on_click = [&app, popup_root, popup_handle](Node*) {
                    apply_file_menu_plan(app, pp::app::FileMenuCommand::cloud_browse);
                    close_legacy_overlay_handle_ignoring_status(*popup_root, popup_handle);
                };
        };
    }
}

} // namespace pp::panopainter
