#include "pch.h"
#include "legacy_ui_overlay_services.h"

#include "app.h"
#include "node.h"
#include "node_input_box.h"
#include "node_message_box.h"
#include "node_popup_menu.h"
#include "node_progress_bar.h"

namespace pp::panopainter {

void initialize_legacy_overlay_node(App& app, Node& node)
{
    node.set_manager(&app.layout);
    node.init();
    node.create();
    node.loaded();
}

void destroy_legacy_node(Node& node)
{
    node.destroy();
}

void detach_legacy_node_from_parent(Node& node)
{
    node.remove_from_parent();
}

void close_legacy_dialog_node(Node& node)
{
    destroy_legacy_node(node);
}

void release_legacy_mouse_capture(Node& node) noexcept
{
    node.mouse_release();
}

void configure_legacy_popup_overlay(Node& node) noexcept
{
    node.m_mouse_ignore = false;
    node.m_flood_events = true;
    node.m_capture_children = false;
}

void activate_legacy_popup_overlay(Node& node) noexcept
{
    configure_legacy_popup_overlay(node);
    node.mouse_capture();
}

void close_legacy_popup_overlay(Node& node) noexcept
{
    release_legacy_mouse_capture(node);
    destroy_legacy_node(node);
}

void close_legacy_dialog_and_hide_keyboard(App& app, Node& node)
{
    close_legacy_dialog_node(node);
    app.hideKeyboard();
}

void close_legacy_popup_panel(
    Node& node,
    const std::function<void(Node*)>& on_close)
{
    release_legacy_mouse_capture(node);
    if (node.m_parent) {
        detach_legacy_node_from_parent(node);
    }
    if (on_close) {
        on_close(&node);
    }
}

pp::foundation::Status attach_legacy_overlay_node(
    App& app,
    const std::shared_ptr<Node>& node) noexcept
{
    if (!node) {
        return pp::foundation::Status::invalid_argument("legacy overlay node is null");
    }

    auto* root = app.layout[app.main_id];
    if (!root) {
        return pp::foundation::Status::invalid_argument("legacy overlay root is missing");
    }

    root->add_child(node);
    return pp::foundation::Status::success();
}

pp::foundation::Status attach_legacy_overlay_node_to_root(
    Node& anchor,
    const std::shared_ptr<Node>& node) noexcept
{
    if (!node) {
        return pp::foundation::Status::invalid_argument("legacy overlay node is null");
    }

    auto* root = anchor.root();
    if (!root) {
        return pp::foundation::Status::invalid_argument("legacy overlay root is missing");
    }

    root->add_child(node);
    return pp::foundation::Status::success();
}

pp::foundation::Result<std::shared_ptr<NodePopupMenu>> add_legacy_popup_menu(
    App& app,
    const char* template_id,
    float x,
    float y,
    float rtl_anchor_width) noexcept
{
    if (!template_id) {
        return pp::foundation::Result<std::shared_ptr<NodePopupMenu>>::failure(
            pp::foundation::Status::invalid_argument("legacy popup template id is null"));
    }

    auto* template_root = app.layout[const_hash(template_id)];
    if (!template_root || template_root->m_children.empty()) {
        return pp::foundation::Result<std::shared_ptr<NodePopupMenu>>::failure(
            pp::foundation::Status::invalid_argument("legacy popup template is missing"));
    }

    auto popup = template_root->m_children[0]->clone<NodePopupMenu>();
    if (!popup) {
        return pp::foundation::Result<std::shared_ptr<NodePopupMenu>>::failure(
            pp::foundation::Status::invalid_argument("legacy popup clone failed"));
    }

    popup->update();
    if (auto* root = app.layout[app.main_id]) {
        if (YGNodeStyleGetDirection(root->y_node) == YGDirectionRTL) {
            x = x - popup->m_size.x + rtl_anchor_width;
        }
    }
    popup->SetPositioning(YGPositionTypeAbsolute);
    popup->SetPosition(x, y);

    const auto status = attach_legacy_overlay_node(app, popup);
    if (!status.ok()) {
        return pp::foundation::Result<std::shared_ptr<NodePopupMenu>>::failure(status);
    }

    return pp::foundation::Result<std::shared_ptr<NodePopupMenu>>::success(popup);
}

std::shared_ptr<NodeProgressBar> create_legacy_progress_dialog_overlay(
    App& app,
    const pp::app::AppProgressDialogPlan& plan)
{
    auto progress = make_legacy_overlay_node<NodeProgressBar>(app);
    progress->m_progress->SetWidthP(plan.progress_fraction);
    progress->m_title->set_text(plan.title.c_str());
    progress->m_total = plan.total;
    progress->m_count = plan.count;
    (void)attach_legacy_overlay_node(app, progress);
    return progress;
}

std::shared_ptr<NodeMessageBox> create_legacy_message_dialog_overlay(
    App& app,
    const pp::app::AppMessageDialogPlan& plan)
{
    auto message = make_legacy_overlay_node<NodeMessageBox>(app);
    message->m_title->set_text(plan.title.c_str());
    message->m_message->set_text(plan.message.c_str());
    message->btn_ok->m_text->set_text(plan.ok_caption.c_str());
    if (plan.show_cancel)
        message->btn_cancel->m_text->set_text(plan.cancel_caption.c_str());
    else
        close_legacy_dialog_node(*message->btn_cancel);
    (void)attach_legacy_overlay_node(app, message);
    return message;
}

std::shared_ptr<NodeInputBox> create_legacy_input_dialog_overlay(
    App& app,
    const pp::app::AppInputDialogPlan& plan)
{
    auto input = make_legacy_overlay_node<NodeInputBox>(app);
    input->m_title->set_text(plan.title.c_str());
    input->m_field_name->set_text(plan.field_name.c_str());
    input->btn_ok->m_text->set_text(plan.ok_caption.c_str());
    (void)attach_legacy_overlay_node(app, input);
    return input;
}

} // namespace pp::panopainter
