#include "pch.h"
#include "log.h"
#include "legacy_ui_overlay_services.h"
#include "node_message_box.h"
#include "layout.h"

Node* NodeMessageBox::clone_instantiate() const
{
    return new NodeMessageBox();
}

void NodeMessageBox::init()
{
    SetPosition(0, 0);
    SetWidthP(100);
    SetHeightP(100);
    SetPositioning(YGPositionTypeAbsolute);
    add_child_file("data/dialogs/message-box.xml", "message-box");
    m_title = find<NodeText>("title");
    m_message = find<NodeText>("message");
    btn_ok = find<NodeButton>("btn-ok");
    btn_ok->on_click = [&](Node*) {
        if (on_submit)
            on_submit(this);
    };
    btn_cancel = find<NodeButton>("btn-cancel");
    on_submit = btn_cancel->on_click = pp::panopainter::legacy_destroy_node_callback(*this);
    m_capture_children = false; // don't capture children events on mouse_capture
}

kEventResult NodeMessageBox::handle_event(Event* e)
{
    Node::handle_event(e);
    auto ke = (KeyEvent*)e;
    switch (e->m_type)
    {
    case kEventType::KeyDown:
        break;
    case kEventType::KeyUp:
        if (ke->m_key == kKey::KeyEnter && on_submit)
            on_submit(this);
        break;
    case kEventType::KeyChar:
        break;
    default:
        return kEventResult::Available;
        break;
    }
    return kEventResult::Consumed;
}

void NodeMessageBox::added(Node* parent)
{
    Node::added(parent);
    if (added_to_root())
        mouse_capture();
}
