/**
 * @file mesh_controller.h
 * @author Piotr Krygier (piotrkrygier@everyonencancode.xyz)
 * @brief Graphics context for Red Scarf Engine
 * @version 0.1
 * @date 2025-09-23
 *
 * @copyright Copyright (c) 2025
 *
 */
#include <SDL3/SDL_log.h>
#include <stdbool.h>
#include <string.h>

#include "src/graphics_context.h"
#include "src/vulkan_commons.h"
#include "utilities/commons.h"
#include "utilities/entity.h"
#include "utilities/errors_common.h"
#include "vulkan_buffers.h"

rse_err_t mesh_create(struct graphics_context_t* context,
                      struct vertex_t* vertices,
                      uint16_t* indices,
                      size_t vertices_count,
                      size_t indices_count,
                      rse_id_t* mesh_id)
{
    rse_err_t status = RSE_ERROR_NO_ERROR;

    if (mesh_id == NULL) {
        SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Provided mesh id handle is NULL");
        return RSE_ERROR_NULL_POINTER;
    }

    *mesh_id = context->mesh_data.mesh_count;

    if (context->mesh_data.mesh_count > MAX_MESH_NUMBER) {
        SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Could not allocate new mesh, reached maximum.");
        return RSE_ERROR_INTERNAL_ERROR;
    }

    STATUS_CHECK(add_vertices(context, *mesh_id, vertices_count, vertices, indices_count, indices));
    context->mesh_data.mesh_count++;

    return status;
}

rse_err_t mesh_create_instance(struct graphics_context_t* context,
                               entity_t entity,
                               struct instance_data_t instance_data)
{
    if (context->mesh_data.mesh_bucket[entity].instances_count >= MAX_INSTANCE_NUMBER) {
        SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Could not allocate new mesh, reached maximum.");
        return RSE_ERROR_INTERNAL_ERROR;
    }

    return mesh_add_instance(context, entity, &instance_data);
}
