#include "transform_node.h"
#include "utils/typeclass_helpers.h"

START_REFLECT(TransformNode);
REFLECT_TYPECLASS(TransformNode, SceneNodeEntity);
REFLECT_TYPECLASS(TransformNode, Transformable);
REFLECT_TYPECLASS(TransformNode, Drop);
END_REFLECT(TransformNode);

impl_Drop_for(TransformNode,
    DestroyTransformNode
)
impl_Transformable_for(TransformNode)
impl_SceneNodeEntity_defaults(TransformNode)
impl_SceneNodeEntity_for(TransformNode,
    TransformNodeEnterTree,
    TransformNodeExitTree,
    TransformNode_default_tick
)

SceneNode *CreateTransformNode() {
    TransformNode *self = new(TransformNode);
    self->dirty_bit = 1;
    self->node = NULL;
    self->parent_transformable = tc_null(Transformable);
    self->global_transform = TransformIdentity();
    self->transform = TransformIdentity();

    return CreateSceneNode(TransformNode_as_SceneNodeEntity(self));
}

void DestroyTransformNode(TransformNode *self) {
    free(self);
}

void TransformNodeEnterTree(TransformNode *self) {
    if(self->node->parent != NULL && TC_MIRRORS(self->node->parent->entity, Transformable))
        self->parent_transformable = TC_CAST(self->node->parent->entity, Transformable);
}

void TransformNodeExitTree(TransformNode *self) {
    self->parent_transformable = tc_null(Transformable);
}

Transform *TransformNodeGetTransform(TransformNode *self) {
    return &self->transform;
}

Transform *TransformNodeGetGlobalTransform(TransformNode *self) {
    return &self->global_transform;
}
