#include <gear/graphics/models.h>
#include <log.c/log.h>

g_model* g_model_create(g_vertex_vec* vertices, g_index_vec* indices) {
    g_model* model = calloc(1, sizeof(g_model));
    if(model != NULL) {
        log_info("Model allocated successfully");
        log_debug("Model pointer: %p", model);

        if(vertices != NULL) {
            for(sz i = 0; i < vertices->size; i++) {
                sl_vec_push(model->vertices, vertices->data[i]);
            }

            model->vertex_buffer = sg_make_buffer(&(sg_buffer_desc){
                .type = SG_BUFFERTYPE_VERTEXBUFFER,
                .data = SL_SG_RANGE(model->vertices)
            });
        }

        if(indices != NULL) {
            for(sz i = 0; i < indices->size; i++) {
                sl_vec_push(model->indices, indices->data[i]);
            }

            model->index_buffer = sg_make_buffer(&(sg_buffer_desc){
                .type = SG_BUFFERTYPE_INDEXBUFFER,
                .data = SL_SG_RANGE(model->indices)
            });
        }
    } else {
        log_fatal("Failed to allocate model");
        exit(EXIT_FAILURE);
    }

    return model;
}
