#include "raylib.h"
#include "test_object.h"
#include "core/camera_node.h"
#include "core/engine_global.h"
#include "core/engine_loop.h"
#include "core/scene.h"
#include "core/transform_node.h"
#include "core/mesh_render_entity.h"
#include "utils/debug.h"

//! Create camera node with a transformable parent.
SceneNode *CreateCameraScene() {
    SceneNode *camera_parent = CreateTransformNode();
    SceneNodeAddChild(camera_parent, CreateCameraNode());
    // set camera parent offset
    Transformable transformable = TC_CAST(camera_parent->entity, Transformable);
    Transform transform = transformable.tc->get_transform(transformable.data);
    transform.translation = (Vector3){3.f, 4.f, -10.f};
    transform.rotation = QuaternionFromEuler(7.5f * DEG2RAD, 0.f, 0.f);
    transformable.tc->set_transform(transformable.data, transform);
    return camera_parent;
}

//! create a model with a camera attached as a child of the root of the scene.
SceneNode *CreateModelScene() {
    SceneNode *model_parent = CreateTransformNode();
    // create the rest of the scene with a mesh and a functionality node
    SceneNodeAddChild(model_parent, CreateMeshRenderEntity("spacefighter"));
    SceneNodeAddChild(model_parent, CreateTestObject());
    // move the renderer's parent transform
    Transformable transformable = TC_CAST(model_parent->entity, Transformable);
    Transform transform = transformable.tc->get_global_transform(transformable.data);
    transform.translation.y += 10;
    transformable.tc->set_global_transform(transformable.data, transform);
    return model_parent;
}

Scene *CreateInitialScene() {
    SceneNode *root = CreateTransformNode();
    SceneNode *model = CreateModelScene();
    SceneNodeAddChild(root, model);
    SceneNodeAddChild(model, CreateCameraScene());
    return CreateScene(root);
}

int main() {
    InitializeEngine();
    RunGame(CreateInitialScene());
    UNREACHABLE("main: End of main function reached. This is an error, call ShutDown() when exiting instead.");
}
