#pragma once
#include "core/pixel_format/pixel.h"
#include "misc/mapped_file.h"
#include <unordered_map>

class renderer_texture_array;
struct stbtt_fontinfo;

struct ch_atlas_item {
    float tex_u; // 图集中的U位置
    float tex_v; // 图集中的V位置
    uint8_t tex_z; // 图集索引
    float u_size; // U方向尺寸
    float v_size; // V方向尺寸

    int32_t x_offset; // SDF纹理的X偏移量
    int32_t y_offset; // SDF纹理的Y偏移量

    int32_t left; // 字形的左边界
    int32_t right; // 字形的右边界
    int32_t top; // 字形的顶部
    int32_t bottom; // 字形的底部
    [[nodiscard]] int32_t get_width() const { return right - left; }
    [[nodiscard]] int32_t get_height() const { return bottom - top; }

    int32_t x_advance; // 字形的X进度
    int32_t left_side_bearing; // 字形的左侧轴承
};

struct measured_ch {
    ch_atlas_item const* item;
    Eigen::Vector2f offset;
    float size_scale;
};

class aorii_text {
    struct font_data {
        font_data() = default;
        font_data(font_data&& other) noexcept {
            file = std::move(other.file);

            font = other.font;
            space_width = other.space_width;
            tab_width = other.tab_width;
            ascent = other.ascent;
            descent = other.descent;
            line_gap = other.line_gap;

            other.font = nullptr;
            other.space_width = 0;
            other.tab_width = 0;
            other.ascent = 0;
            other.descent = 0;
            other.line_gap = 0;
        }
        ~font_data();

        stbtt_fontinfo* font{};
        mapped_file file;
        int32_t space_width{};
        int32_t tab_width{};
        int32_t ascent{}; // 字体从基线到顶部的高度
        int32_t descent{}; // 基线到字体底部的高度
        int32_t line_gap{}; // 行间距
    };
public:
    aorii_text();
    ~aorii_text();

    static bool init_freetype();
    static void destroy_freetype();

    bool initialize(const std::wstring& in_font_path);
    bool add_font(const std::wstring& font_path);
    ch_atlas_item const* get_atlas_item(char32_t ch);

    // 预缓存一组字符
    bool precache_characters(const std::u32string& characters);

    // 预缓存常用字符集
    bool precache_common_characters();

    // 获取缓存状态
    [[nodiscard]] size_t get_cached_character_count() const { return character_map.size(); }

    // 获取字体字符数量
    [[nodiscard]] uint32_t get_font_char_count() const;

    [[nodiscard]] renderer_texture_array* get_texture_array() const { return texture_array; }
    [[nodiscard]] uint32_t get_texture_size() const;

    [[nodiscard]] float get_kerning(char32_t ch1, char32_t ch2) const;
    [[nodiscard]] std::vector<measured_ch> measure_text(const std::u32string& text, float height);
private:
    ch_atlas_item const* cache_ch_to_atlas(char32_t ch);
    std::vector<font_data> fonts;
    const float pixel_height = 64;
    const int padding = 5;

    renderer_texture_array* texture_array;

    uint32_t current_x;
    uint32_t current_y;
    uint32_t next_row_y = 0;  // 跟踪当前行的最大高度
    uint8_t current_texture_index;

    std::unordered_map<char32_t, ch_atlas_item> character_map;

    // 常用字符集定义
    static const std::u32string COMMON_ASCII;             // ASCII字符
    static const std::u32string COMMON_CHINESE;           // 常用汉字
    static const std::u32string COMMON_PUNCTUATION;       // 常用标点符号
    static const std::u32string COMMON_NUMBERS;           // 数字
};
