#include "pch.h"
#include "legacy_ui_node_style.h"

#include "app.h"
#include "node.h"

namespace pp::panopainter {

void legacy_ui_node_set_width(Node& node, float value)
{
    YGNodeStyleSetWidth(node.y_node, value);
    node.m_size.x = value;
    node.auto_width = value == YGUndefined;
    node.app_redraw();
}

void legacy_ui_node_set_width_percent(Node& node, float value)
{
    YGNodeStyleSetWidthPercent(node.y_node, value);
    node.auto_width = value == YGUndefined;
    node.app_redraw();
}

void legacy_ui_node_set_height(Node& node, float value)
{
    YGNodeStyleSetHeight(node.y_node, value);
    node.m_size.y = value;
    node.auto_height = value == YGUndefined;
    node.app_redraw();
}

void legacy_ui_node_set_height_percent(Node& node, float value)
{
    YGNodeStyleSetHeightPercent(node.y_node, value);
    node.auto_height = value == YGUndefined;
    node.app_redraw();
}

void legacy_ui_node_set_size(Node& node, float w, float h)
{
    legacy_ui_node_set_width(node, w);
    legacy_ui_node_set_height(node, h);
    node.m_size = { w, h };
    node.app_redraw();
}

void legacy_ui_node_set_size(Node& node, glm::vec2 value)
{
    legacy_ui_node_set_size(node, value.x, value.y);
}

void legacy_ui_node_set_min_size(Node& node, float w, float h)
{
    legacy_ui_node_set_min_width(node, w);
    legacy_ui_node_set_min_height(node, h);
}

void legacy_ui_node_set_min_size(Node& node, glm::vec2 value)
{
    legacy_ui_node_set_min_size(node, value.x, value.y);
}

void legacy_ui_node_set_max_size(Node& node, float w, float h)
{
    legacy_ui_node_set_max_width(node, w);
    legacy_ui_node_set_max_height(node, h);
}

void legacy_ui_node_set_max_size(Node& node, glm::vec2 value)
{
    legacy_ui_node_set_max_size(node, value.x, value.y);
}

void legacy_ui_node_set_max_width(Node& node, float value)
{
    YGNodeStyleSetMaxWidth(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_max_width_percent(Node& node, float value)
{
    YGNodeStyleSetMaxWidthPercent(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_max_height(Node& node, float value)
{
    YGNodeStyleSetMaxHeight(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_max_height_percent(Node& node, float value)
{
    YGNodeStyleSetMaxHeightPercent(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_min_width(Node& node, float value)
{
    YGNodeStyleSetMinWidth(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_min_width_percent(Node& node, float value)
{
    YGNodeStyleSetMinWidthPercent(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_min_height(Node& node, float value)
{
    YGNodeStyleSetMinHeight(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_min_height_percent(Node& node, float value)
{
    YGNodeStyleSetMinHeightPercent(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_padding(Node& node, float t, float r, float b, float l)
{
    YGNodeStyleSetPadding(node.y_node, YGEdgeTop, t);
    YGNodeStyleSetPadding(node.y_node, YGEdgeRight, r);
    YGNodeStyleSetPadding(node.y_node, YGEdgeBottom, b);
    YGNodeStyleSetPadding(node.y_node, YGEdgeLeft, l);
    node.app_redraw();
}

glm::vec4 legacy_ui_node_get_padding(const Node& node)
{
    float t = YGNodeLayoutGetPadding(node.y_node, YGEdgeTop);
    float r = YGNodeLayoutGetPadding(node.y_node, YGEdgeRight);
    float b = YGNodeLayoutGetPadding(node.y_node, YGEdgeBottom);
    float l = YGNodeLayoutGetPadding(node.y_node, YGEdgeLeft);
    return { t, r, b, l };
}

void legacy_ui_node_set_margin(Node& node, float t, float r, float b, float l)
{
    YGNodeStyleSetMargin(node.y_node, YGEdgeTop, t);
    YGNodeStyleSetMargin(node.y_node, YGEdgeRight, r);
    YGNodeStyleSetMargin(node.y_node, YGEdgeBottom, b);
    YGNodeStyleSetMargin(node.y_node, YGEdgeLeft, l);
    node.app_redraw();
}

glm::vec4 legacy_ui_node_get_margin(const Node& node)
{
    float t = YGNodeLayoutGetMargin(node.y_node, YGEdgeTop);
    float r = YGNodeLayoutGetMargin(node.y_node, YGEdgeRight);
    float b = YGNodeLayoutGetMargin(node.y_node, YGEdgeBottom);
    float l = YGNodeLayoutGetMargin(node.y_node, YGEdgeLeft);
    return { t, r, b, l };
}

void legacy_ui_node_set_position(Node& node, float l, float t)
{
    YGNodeStyleSetPosition(node.y_node, YGEdgeTop, t);
    YGNodeStyleSetPosition(node.y_node, YGEdgeLeft, l);
    node.m_pos = { l, t };
    node.app_redraw();
}

void legacy_ui_node_set_position(Node& node, float l, float t, float r, float b)
{
    YGNodeStyleSetPosition(node.y_node, YGEdgeTop, t);
    YGNodeStyleSetPosition(node.y_node, YGEdgeRight, r);
    YGNodeStyleSetPosition(node.y_node, YGEdgeBottom, b);
    YGNodeStyleSetPosition(node.y_node, YGEdgeLeft, l);
    node.app_redraw();
}

void legacy_ui_node_set_position(Node& node, glm::vec2 value)
{
    legacy_ui_node_set_position(node, value.x, value.y);
    node.m_pos = value;
    node.app_redraw();
}

float legacy_ui_node_get_width(const Node& node)
{
    return YGNodeLayoutGetWidth(node.y_node);
}

float legacy_ui_node_get_height(const Node& node)
{
    return YGNodeLayoutGetHeight(node.y_node);
}

glm::vec2 legacy_ui_node_get_position(const Node& node)
{
    return { YGNodeLayoutGetLeft(node.y_node), YGNodeLayoutGetTop(node.y_node) };
}

glm::vec2 legacy_ui_node_get_size(const Node& node)
{
    return { legacy_ui_node_get_width(node), legacy_ui_node_get_height(node) };
}

YGDirection legacy_ui_node_get_rtl(const Node& node)
{
    return YGNodeStyleGetDirection(node.y_node);
}

void legacy_ui_node_set_flex_grow(Node& node, float value)
{
    YGNodeStyleSetFlexGrow(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_flex_shrink(Node& node, float value)
{
    YGNodeStyleSetFlexShrink(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_flex_dir(Node& node, YGFlexDirection value)
{
    YGNodeStyleSetFlexDirection(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_flex_wrap(Node& node, YGWrap value)
{
    YGNodeStyleSetFlexWrap(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_justify(Node& node, YGJustify value)
{
    YGNodeStyleSetJustifyContent(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_align(Node& node, YGAlign value)
{
    YGNodeStyleSetAlignItems(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_positioning(Node& node, YGPositionType value)
{
    YGNodeStyleSetPositionType(node.y_node, value);
    node.app_redraw();
}

void legacy_ui_node_set_aspect_ratio(Node& node, float ar)
{
    YGNodeStyleSetAspectRatio(node.y_node, ar);
    node.app_redraw();
}

void legacy_ui_node_set_rtl(Node& node, YGDirection dir)
{
    YGNodeStyleSetDirection(node.y_node, dir);
    node.app_redraw();
}

bool legacy_ui_node_get_visibility(const Node& node)
{
    return node.m_display;
}

void legacy_ui_node_set_visibility(Node& node, bool visible)
{
    App::I->ui_task([&node, visible]
    {
        if (node.m_display && !visible)
        {
            int idx = node.m_parent->get_child_index(&node);
            YGNodeRemoveChild(node.m_parent->y_node, node.y_node);
            node.y_placeholder = YGNodeNew();
            YGNodeInsertChild(node.m_parent->y_node, node.y_placeholder, idx);
        }
        else if (!node.m_display && visible)
        {
            int count = YGNodeGetChildCount(node.m_parent->y_node);
            for (int i = 0; i < count; i++)
            {
                if (YGNodeGetChild(node.m_parent->y_node, i) == node.y_placeholder)
                {
                    YGNodeRemoveChild(node.m_parent->y_node, node.y_placeholder);
                    YGNodeInsertChild(node.m_parent->y_node, node.y_node, i);
                    YGNodeFree(node.y_placeholder);
                    node.y_placeholder = nullptr;
                    break;
                }
            }
        }
    });
    node.m_display = visible;
    node.app_redraw();
}

void legacy_ui_node_toggle_visibility(Node& node)
{
    node.SetVisibility(!node.m_display);
}

} // namespace pp::panopainter

void Node::SetSize(float w, float h)
{
    pp::panopainter::legacy_ui_node_set_size(*this, w, h);
}

void Node::SetSize(glm::vec2 value)
{
    pp::panopainter::legacy_ui_node_set_size(*this, value);
}

void Node::SetMinSize(float w, float h)
{
    pp::panopainter::legacy_ui_node_set_min_size(*this, w, h);
}

void Node::SetMinSize(glm::vec2 value)
{
    pp::panopainter::legacy_ui_node_set_min_size(*this, value);
}

void Node::SetMaxSize(float w, float h)
{
    pp::panopainter::legacy_ui_node_set_max_size(*this, w, h);
}

void Node::SetMaxSize(glm::vec2 value)
{
    pp::panopainter::legacy_ui_node_set_max_size(*this, value);
}

void Node::SetPosition(const glm::vec2 pos)
{
    pp::panopainter::legacy_ui_node_set_position(*this, pos);
}

void Node::SetWidth(float value)
{
    pp::panopainter::legacy_ui_node_set_width(*this, value);
}

void Node::SetWidthP(float value)
{
    pp::panopainter::legacy_ui_node_set_width_percent(*this, value);
}

void Node::SetHeight(float value)
{
    pp::panopainter::legacy_ui_node_set_height(*this, value);
}

void Node::SetHeightP(float value)
{
    pp::panopainter::legacy_ui_node_set_height_percent(*this, value);
}

void Node::SetMaxWidth(float value)
{
    pp::panopainter::legacy_ui_node_set_max_width(*this, value);
}

void Node::SetMaxWidthP(float value)
{
    pp::panopainter::legacy_ui_node_set_max_width_percent(*this, value);
}

void Node::SetMaxHeight(float value)
{
    pp::panopainter::legacy_ui_node_set_max_height(*this, value);
}

void Node::SetMaxHeightP(float value)
{
    pp::panopainter::legacy_ui_node_set_max_height_percent(*this, value);
}

void Node::SetMinWidth(float value)
{
    pp::panopainter::legacy_ui_node_set_min_width(*this, value);
}

void Node::SetMinWidthP(float value)
{
    pp::panopainter::legacy_ui_node_set_min_width_percent(*this, value);
}

void Node::SetMinHeight(float value)
{
    pp::panopainter::legacy_ui_node_set_min_height(*this, value);
}

void Node::SetMinHeightP(float value)
{
    pp::panopainter::legacy_ui_node_set_min_height_percent(*this, value);
}

void Node::SetPadding(float t, float r, float b, float l)
{
    pp::panopainter::legacy_ui_node_set_padding(*this, t, r, b, l);
}

glm::vec4 Node::GetPadding() const
{
    return pp::panopainter::legacy_ui_node_get_padding(*this);
}

void Node::SetMargin(float t, float r, float b, float l)
{
    pp::panopainter::legacy_ui_node_set_margin(*this, t, r, b, l);
}

glm::vec4 Node::GetMargin() const
{
    return pp::panopainter::legacy_ui_node_get_margin(*this);
}

void Node::SetPosition(float l, float t)
{
    pp::panopainter::legacy_ui_node_set_position(*this, l, t);
}

void Node::SetPosition(float l, float t, float r, float b)
{
    pp::panopainter::legacy_ui_node_set_position(*this, l, t, r, b);
}

void Node::SetFlexGrow(float value)
{
    pp::panopainter::legacy_ui_node_set_flex_grow(*this, value);
}

void Node::SetFlexShrink(float value)
{
    pp::panopainter::legacy_ui_node_set_flex_shrink(*this, value);
}

void Node::SetFlexDir(YGFlexDirection value)
{
    pp::panopainter::legacy_ui_node_set_flex_dir(*this, value);
}

void Node::SetFlexWrap(YGWrap value)
{
    pp::panopainter::legacy_ui_node_set_flex_wrap(*this, value);
}

void Node::SetJustify(YGJustify value)
{
    pp::panopainter::legacy_ui_node_set_justify(*this, value);
}

void Node::SetAlign(YGAlign value)
{
    pp::panopainter::legacy_ui_node_set_align(*this, value);
}

void Node::SetPositioning(YGPositionType value)
{
    pp::panopainter::legacy_ui_node_set_positioning(*this, value);
}

void Node::SetAspectRatio(float ar)
{
    pp::panopainter::legacy_ui_node_set_aspect_ratio(*this, ar);
}

void Node::SetRTL(YGDirection dir)
{
    pp::panopainter::legacy_ui_node_set_rtl(*this, dir);
}

bool Node::GetVisibility()
{
    return pp::panopainter::legacy_ui_node_get_visibility(*this);
}

void Node::SetVisibility(bool visible)
{
    pp::panopainter::legacy_ui_node_set_visibility(*this, visible);
}

void Node::ToggleVisibility()
{
    pp::panopainter::legacy_ui_node_toggle_visibility(*this);
}

glm::vec2 Node::GetPosition()
{
    return pp::panopainter::legacy_ui_node_get_position(*this);
}

float Node::GetWidth()
{
    return pp::panopainter::legacy_ui_node_get_width(*this);
}

float Node::GetHeight()
{
    return pp::panopainter::legacy_ui_node_get_height(*this);
}

glm::vec2 Node::GetSize()
{
    return pp::panopainter::legacy_ui_node_get_size(*this);
}

YGDirection Node::GetRTL()
{
    return pp::panopainter::legacy_ui_node_get_rtl(*this);
}
