#include "pch.h"
#include "legacy_ui_overlay_services.h"
#include "log.h"
#include "legacy_ui_gl_dispatch.h"
#include "node_border.h"
#include "shader.h"

Plane NodeBorder::m_plane;

NodeBorder::NodeBorder()
{
    m_mouse_ignore = false;
}
void NodeBorder::static_init()
{
    m_plane.create<1>(1, 1);
}
Node* NodeBorder::clone_instantiate() const
{
    return new NodeBorder();
}
void NodeBorder::clone_copy(Node* dest) const
{
    Node::clone_copy(dest);
    NodeBorder* n = static_cast<NodeBorder*>(dest);
    n->m_color = m_color;
    n->m_border_color = m_border_color;
    n->m_thinkness = m_thinkness;
    n->m_mouse_ignore = false;
}
void NodeBorder::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
    Node::parse_attributes(ka, attr);
    switch (ka)
    {
    case kAttribute::Color:
    {
        glm::vec4 pad;
        int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
        if (n == 1)
            m_color = glm::vec4(pad.x, pad.x, pad.x, 1);
        else
            m_color = pad;
        break;
    }
    case kAttribute::BorderColor:
    {
        glm::vec4 pad;
        int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
        if (n == 1)
            m_border_color = glm::vec4(pad.x);
        else
            m_border_color = pad;
        break;
    }
    case kAttribute::Thickness:
        m_thinkness = attr->FloatValue();
        break;
    default:
        break;
    }
}
void NodeBorder::draw()
{
    if (m_color.a > 0.f)
    {
        pp::legacy::ui_gl::set_blend_enabled(m_color.a < 1.f, "NodeBorder");
        pp::panopainter::configure_legacy_ui_color_shader(m_mvp, m_color);
        m_plane.draw_fill();
        pp::legacy::ui_gl::set_blend_enabled(false, "NodeBorder");
    }

    if (m_thinkness > 0 && m_border_color.a > 0.f)
    {
        //glLineWidth(m_thinkness);
        pp::legacy::ui_gl::set_blend_enabled(m_border_color.a < 1.f, "NodeBorder");
        pp::panopainter::configure_legacy_ui_color_shader(m_mvp, m_border_color);
        m_plane.draw_stroke();
        pp::legacy::ui_gl::set_blend_enabled(false, "NodeBorder");
    }
}
