#include "backgroundnode.h"

#include "engine.h"
#include "scene/scenetree.h"

#include "glm/vec2.hpp"
#include "glm/vec3.hpp"

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

BackGroundNode::BackGroundNode(glm::vec2 dimension, glm::vec3 color, float opacity,float depth):
    m_dimension(dimension),
    m_color(color),
    m_color_updated(false),
    m_opacity(opacity),
    m_opacity_updated(false)
{
    Mesh* mesh = new Mesh("background");
    mesh->addRectangle2D(glm::vec2(0),dimension);
    PBRMaterial *mat = new PBRMaterial();
    mat->albedo = color;
    mat->opacity = opacity;
    mesh->setMaterial(mat);
    mesh->setDepth(depth);
    mesh->initGL();
    m_mesh = new MeshNode(mesh);
    addChild(m_mesh);
}

void BackGroundNode::update(){
    if(m_color_updated){
        PBRMaterial* mat = (PBRMaterial*) m_mesh->getGeometryNode()->mesh->getMaterial();
        mat->albedo = m_color;
        m_color_updated = false;
    }
    if(m_opacity_updated){
        PBRMaterial* mat = (PBRMaterial*) m_mesh->getGeometryNode()->mesh->getMaterial();
        mat->opacity = m_opacity;
        m_opacity_updated = false;
    }
    GUINode::update();
}

void BackGroundNode::setColor(glm::vec3 color){
    m_color = color;
    m_color_updated = true;
}

void BackGroundNode::setOpacity(float opacity){
    m_opacity = opacity;
    m_opacity_updated = true;
}

glm::vec2 BackGroundNode::getDimension(){
    return m_dimension;
}
