#pragma once

#include <SDL3/SDL.h>

#include <cstdint>
#include <string>
#include <unordered_map>

class Text {
   public:
    Text(const char* fnt_file, const char* gif_file);
    ~Text();

    // Pinta texto sobre un buffer ARGB de 320x200
    void draw(Uint32* pixel_data, int x, int y, const char* text, Uint32 color) const;
    void drawCentered(Uint32* pixel_data, int y, const char* text, Uint32 color) const;

    // Calcula ancho en píxeles d'un text
    [[nodiscard]] auto width(const char* text) const -> int;
    [[nodiscard]] auto charHeight() const -> int { return box_height_; }

   private:
    struct GlyphInfo {
        int x, y;  // posició en el bitmap
        int w;     // ample visual
    };

    int box_width_{0};
    int box_height_{0};
    int columns_{0};
    int cell_spacing_{0};
    int row_spacing_{0};

    Uint8* bitmap_{nullptr};  // píxels 8-bit del GIF de la font
    int bitmap_width_{0};
    int bitmap_height_{0};

    std::unordered_map<uint32_t, GlyphInfo> glyphs_;

    static auto nextCodepoint(const char*& ptr) -> uint32_t;

    void loadFont(const char* fnt_file);
    void loadBitmap(const char* gif_file);

    static constexpr int SCREEN_WIDTH = 320;
    static constexpr int SCREEN_HEIGHT = 200;
};
