#include "pch.h"
#include "log.h"
#include "node_button.h"

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

void NodeButton::clone_children(Node* dest) const
{
    Node::clone_children(dest);
    NodeButton* n = static_cast<NodeButton*>(dest);
    n->m_border = (NodeBorder*)n->m_children[0].get();
    n->m_text = (NodeText*)n->m_border->m_children[0].get();
}

void NodeButton::clone_copy(Node* dest) const
{
    Node::clone_copy(dest);
    NodeButton* n = static_cast<NodeButton*>(dest);
    //n->m_border = (NodeBorder*)m_border->clone();
    //n->m_text = (NodeText*)m_text->clone();
    n->color_normal = color_normal;
    n->color_hover = color_hover;
    n->color_down = color_down;
    n->color_active = color_active;
    //n->on_click = on_click;
    n->m_mouse_ignore = false;
}

void NodeButton::init()
{
    Node::init();
    m_border = new NodeBorder();
    m_text = new NodeText();
    add_child(m_border);
    m_border->add_child(m_text);
    m_border->init();
    m_border->m_color = color_normal;
    m_text->init();
    m_border->SetAlign(YGAlignCenter);
    m_border->SetJustify(YGJustifyCenter);
    m_border->m_mouse_ignore = false;
    m_mouse_ignore = false;
}

void NodeButton::create()
{
    Node::create();
    m_border->create();
    m_text->create();
    m_border->m_mouse_ignore = false;
    m_mouse_ignore = false;
}

void NodeButton::loaded()
{
    Node::loaded();
    m_border->m_thinkness = 1;
    m_border->m_border_color = glm::vec4(0, 0, 0, 1);
    m_border->m_color = color_normal;
    m_border->m_mouse_ignore = false;
    m_mouse_ignore = false;
}

void NodeButton::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
    Node::parse_attributes(ka, attr);
    switch (ka)
    {
    case kAttribute::Color:
        m_border->parse_attributes(ka, attr);
        color_normal = m_border->m_color;
        color_hover  = glm::clamp(m_border->m_color + glm::vec4(.1, .1, .1, 0), {0,0,0,0}, {1,1,1,1});
        color_down   = glm::clamp(m_border->m_color + glm::vec4(.2, .2, .2, 0), {0,0,0,0}, {1,1,1,1});
        break;
    case kAttribute::Padding:
    case kAttribute::Width:
    case kAttribute::Height:
    case kAttribute::Thickness:
    case kAttribute::BorderColor:
        m_border->parse_attributes(ka, attr);
        break;
    case kAttribute::Text:
    case kAttribute::FontFace:
    case kAttribute::FontSize:
        m_text->parse_attributes(ka, attr);
        break;
    default:
//        Node::parse_attributes(ka, attr);
        break;
    }
    //         m_border->parse_attributes(ka, attr);
    //         m_text->parse_attributes(ka, attr);
}

void NodeButton::set_active(bool active)
{
    if (m_active == active)
        return;
    m_active = active;
    m_border->m_color = m_active ? color_active : (m_mouse_inside ? color_hover : color_normal);
}

void NodeButton::set_color(const glm::vec4& c)
{
    color_normal = c;
    m_border->m_color = color_normal;
}

kEventResult NodeButton::handle_event(Event* e)
{
    Node::handle_event(e);
    switch (e->m_type)
    {
    case kEventType::MouseEnter:
        m_border->m_color = color_hover;
        break;
    case kEventType::MouseLeave:
        m_border->m_color = m_active ? color_active : color_normal;
        break;
    case kEventType::MouseDownL:
        m_border->m_color = m_active ? color_active : color_down;
        break;
    case kEventType::MouseUpL:
        m_border->m_color = m_active ? color_active : color_normal;
        if (m_mouse_inside && on_click != nullptr)
            on_click(this);
        break;
    case kEventType::MouseCancel:
        m_border->m_color = m_active ? color_active : color_normal;
        break;
    default:
        return kEventResult::Available;
        break;
    }
    return kEventResult::Consumed;
}
