#include "buttonnode.h"
#include "buttonshape.h"
#include "backgroundnode.h"

#include <iostream>

#include "scene/scenetree.h"

#include "SparrowInput/input.h"

#include "SparrowRenderer/sparrowrenderer.h"
#include "SparrowRenderer/mesh.h"
#include "SparrowRenderer/pbrmaterial.h"

#include "sparrowshell/sparrowshell.h"
#include "tools/font.h"
#include "resourcemanager.h"

ButtonNode::ButtonNode(ButtonShape* shape):
   m_shape(shape)
{
    addChild(m_shape->getBackGround());
    m_label = new LabelNode();
    addChild(m_label);
}

BackGroundNode* ButtonNode::getBackGround()
{
    return m_shape->getBackGround();
}

glm::vec2 ButtonNode::getDimension(){
    return m_shape->getDimension();
}

LabelNode* ButtonNode::getLabel(){
    return m_label;
}

void ButtonNode::update()
{
    Input* input = getEngine().getInput();

    if(m_label->wasUpdated()){
        m_label->setPosition(m_shape->getDimension()/glm::vec2(2,2) - m_label->getDimension()/glm::vec2(2,2));
    }

    for (auto action : input->getActions())
    {
        if (action.action == m_action){
            if (m_shape->hover(this->getPosition(),input->getPosition()))
                m_callback->exec();
        }
    }
    GUINode::update();
}
