//
// Created by Vicente Ferrari Smith on 01.03.26.
//

#include <print>
#include "texture.h"
#include "graphics.h"

TextureManager::TextureManager() {

}

Texture TextureManager::load(const std::string& path) {
    if (textures.contains(path)) return textures[path];

    int w, h, ch;
    unsigned char* data = stbi_load(path.c_str(), &w, &h, &ch, STBI_rgb_alpha);
    if (!data) {
        std::println("Failed to load texture: {}", path);
        return {};
    }

    // Tell the renderer to make the GPU version
    Texture res;
    res.width = w;
    res.height = h;
    res.channels = STBI_rgb_alpha;
    res.srgb = true;
    upload_texture(w, h, data, &res);

    stbi_image_free(data);

    res.id = path;
    res.path = path;
    res.uploaded = true;

    textures[path] = res;
    // path_to_id[path] = id;

    return res; // This is the textureID for your sprites
}
