#include "lightnode.h"
#include "scenetree.h"
#include <SparrowRenderer/light.h>
#include <glm/mat3x3.hpp>

void LightNode::setSceneTree(SceneTree *tree)
{
    GraphicalNode::setSceneTree(tree);
    if(m_scene != nullptr)
        m_scene->registerLightType(m_light->getFlags());
}

void LightNode::update()
{
    if(m_transformChanged)
    {
        glm::mat4 combinedTransform(m_parentTransform * m_transform);

        switch(m_light->getType())
        {
        case Light::DIRECTIONNAL :
            {
                DirectionnalLight *l = (DirectionnalLight*)m_light;
                l->setDir(glm::mat3(combinedTransform) * l->getDir());
                if(l->isShadowCaster())
                    l->updateShadowMap(m_scene);
            }
            break;

        case Light::POINT :
            {
                PointLight *l = (PointLight*)m_light;
                l->setPos(l->getPos() + glm::vec3(combinedTransform[3]));
            }
            break;

        case Light::SPOT :
            // TODO
            break;

        case Light::AMBIENT :
        default:
            break;
        }
    }
}
