#include "pch.h"
#include "log.h"
#include "node_combobox.h"
#include "legacy_ui_overlay_services.h"
#include "node_popup_menu.h"

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

void NodeComboBox::clone_copy(Node* dest) const
{
    NodeButton::clone_copy(dest);
    NodeComboBox* n = static_cast<NodeComboBox*>(dest);
    n->m_data = m_data;
    n->m_items = m_items;
    n->m_current_index = m_current_index;
    n->m_selected_child_index = m_selected_child_index;
}

void NodeComboBox::loaded()
{
    NodeButton::loaded();
    m_text->set_text(m_data[m_current_index].c_str());
    m_selected_child_index = m_current_index;
    on_click = [this](Node* target) {
        auto popup = std::make_shared<NodePopupMenu>();
        popup->set_manager(m_manager);
        popup->init();
        popup->create();
        popup->loaded();
        const auto popup_overlay = pp::panopainter::open_legacy_overlay_node_with_handle(*this, popup);
        if (!popup_overlay) {
            return;
        }
        const auto popup_handle = popup_overlay.value();
        m_items.clear();
        int popup_child_index = -1;
        for (int i = 0; i < m_data.size(); i++)
        {
            if (m_data[i] == "-")
            {
                auto n = popup->add_child<NodeBorder>();
                n->SetHeight(5.f);
                n->SetWidthP(100.f);
                n->m_color = {0, 0, 0, 1};
            }
            else
            {
                auto btn = popup->add_child<NodeButton>();
                btn->m_text->set_text(m_data[i].c_str());
                btn->m_border->SetWidthP(100.f);
                btn->m_border->SetHeight(GetHeight());
                popup_child_index++;
                int index = (int)m_items.size();
                if (index == m_current_index)
                    m_selected_child_index = popup_child_index;
                m_items.push_back(m_data[i]);
                const int clicked_child_index = popup_child_index;
                btn->on_click = [this, popup_handle, index, clicked_child_index](Node* target) {
                    m_current_index = index;
                    m_selected_child_index = clicked_child_index;
                    m_text->set_text(m_items[index].c_str());
                    if (on_select)
                        on_select(target, index);
                    const auto close_status =
                        pp::panopainter::close_legacy_overlay_node(*this, popup_handle);
                    (void)close_status;
                };
            }
        }
        float offset = 0;
        for (int i = 0; i <= m_selected_child_index; i++)
            offset += (m_data[i] == "-") ? 5.f : GetHeight();
        float height = m_items.size() * GetHeight() + (m_data.size() - m_items.size()) * 5.f; // add items and separators
        glm::vec2 pos = m_pos + glm::vec2(0, m_size.y - offset);
        auto screen = root()->m_size;
        if ((pos.y + height) > screen.y) pos.y = screen.y - height;
        if (pos.y < 0) pos.y = 0;
        popup->SetPositioning(YGPositionTypeAbsolute);
        popup->SetPosition(pos.x, pos.y);
        popup->SetSize(m_size.x, YGUndefined);
        popup->SetFlexGrow(1.f);
        popup->update();
        root()->update();
        pp::panopainter::activate_legacy_popup_overlay(*popup);
    };
}

void NodeComboBox::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
    NodeButton::parse_attributes(ka, attr);
    switch (ka)
    {
    case kAttribute::ComboList:
    {
        m_data = split(attr->Value(), ',');
        m_items.clear();
        for (auto& i : m_data)
            if (i != "-") m_items.push_back(i);
        break;
    }
    case kAttribute::Default:
        m_current_index = attr->IntValue();
        break;
    default:
        break;
    }
}

void NodeComboBox::set_index(int index)
{
    m_current_index = index;
    m_text->set_text(m_items[index].c_str());
    //if (on_select)
    //    on_select(this, index);
}

float NodeComboBox::get_float(int index) const noexcept
{
    return std::stof(m_data[index]);
}

float NodeComboBox::get_float() const noexcept
{
    return std::stof(m_data[m_current_index]);
}

int NodeComboBox::get_int() const noexcept
{
    return std::stoi(m_data[m_current_index]);
}
